libc/winsup/cygwin/fhandler_random.cc
Christopher Faylor 56a1971526 Throughout use "have_execed" macro rather than "hExeced" global handle.
Throughout rename _PROC_* to _CH_*.
* child_info.h: Include "pinfo.h".
(child_info_types): Rename _PROC_* -> _CH_* to avoid confusion with similarly
named constants.
(_PROC_*): Delete unneeded aliases.
(PROC_*): Ditto.
(CURR_CHILD_INFO_MAGIC): Ditto.
(cchildren): Define using "pinfo_minimal".
(child_info::set_saw_ctrl_c): Move to
(child_info_spawn::set_saw_ctrl_c): Here.
(child_info_spawn::lock): New field.
(child_info_spawn::hExeced): Ditto.
(child_info_spawn::ev): Ditto.
(child_info_spawn::~child_info_spawn): Move to sigproc.cc.
(child_info_spawn::child_info_spawn): Ditto.
(child_info_spawn::cleanup): Declare new function.
(child_info_spawn::set_saw_ctrl_c): Move to this class.  Set flag only when
execed and return true when we have set the flag.
(child_info_spawn::child_info_spawn::signal_myself_exited): New function.
(child_info_spawn::wait_for_myself): Ditto.
(child_info_spawn::has_execed_cygwin): Ditto.
(child_info_spawn::has_execed): Ditto.  Replaces "hExeced" test.
(child_info_spawn::operator HANDLE&): New operator.
(child_info_spawn::worker): Define old "spawn_guts" as class member.
(ch_spawn): Declare.
(have_execed): Define.
(have_execed_cygwin): Ditto.
* cygheap.h: Update comment.
* dcrt0.cc (get_cygwin_startup_info): Use _CH_* enums.
(child_info_spawn::handle_spawn): Ditto.
(dll_crt0_0): Ditto.
(multiple_cygwin_problem): Ditto.
* exceptions.cc (chExeced): Delete obsolete declaration.
(ctrl_c_handler): Reference set_saw_ctrl_c via new ch_spawn global.
* globals.cc (hExeced): Delete.
* pinfo.cc (pinfo::thisproc): Refer to cygheap as ::cygheap for consistency in
handle naming when -DDEBUGGING.
(pinfo::init): Accommodate case where myself.h is known but h0 is passed in.
(pinfo::pinfo): New constructor for setting up a pinfo passed in by previous
exec'or.
(pinfo::proc_waiter): Don't handle subprocess if we're in the process of
exiting due to an exec of a cygwin process.  Don't close rd_proc_pipe here.
Close it when we actually are finished with the process.  Use new
ch_spawn.signal_myself_exited function to let exec stub know that subprocess
has exited.
(pinfo::wait): Clarify debugging output.
(pinfo::release): Use "close_h" to close all handles to avoid races.
(winpids::add): Assume that elements of the array do not need to be zeroed and
are properly initialized or suffer problems on pinfo::release.  Don't close
hProcess since release does that now.
* pinfo.h: Update comment.
(pinfo_minimal): Move some elements from pinfo here so that child_info_spawn
can use them.
(pinfo): Inherit from pinfo_minimal.
(pinfo::pinfo): Modify to accommodate new pinfo_minimal.
(pinfo::allow_remove): New function.
* sigproc.cc (proc_subproc): Use boolean values for true/false.  Implement
PROC_EXEC_CLEANUP.
(proc_terminate): Set ppid = 1 since the procs list will only be iterated when
the process has not execed.  Don't do any cleanup here since it is now handled
in pinfo::release.
(sigproc_init): Initialize sync_proc_subproc earlier.
(child_info::child_info): Assume that all important fields are properly
initialized and avoid memset().
(child_info_spawn::child_info_spawn): Specifically test for execing and then
set up appropriate fields in the struct.
(child_info_spawn::cleanup): Define new function.
(child_info_spawn::record_children): Specifically test for being execed here.
Fill in pinfo_minimal part of children array.
(child_info_spawn::reattach_children): Use constructor to duplicate information
for previous exec'or.  Add more debugging output.
(remove_proc): Force deletion of thread when exiting due to exec.  Rely on
pinfo::cleanup in release.
* sigproc.h (PROC_EXEC_CLEANUP): New enum.
(PROC_DETACHED_CHILD): Delete.
* spawn.cc (chExeced): Delete.
(child_info_spawn::worker): Rename from spawn_guts.  Use elements of
child_info_spawn throughout rather than ch.whatever.  Use ::cygheap to refer to
global rather than element of child_info.  Use wait_for_myself() rather than
waitpid().  Call child_info_spawn::cleanup on function return.
(spawnve): Reflect movement of spawn_guts functionality into
child_info_spawn::worker.
* syscalls.cc (popen): Ditto.
* winsup.h (spawn_guts): Delete declaration.
2011-11-14 01:29:49 +00:00

191 lines
4.5 KiB
C++

/* fhandler_random.cc: code to access /dev/random and /dev/urandom
Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2009, 2011 Red Hat, Inc.
Written by Corinna Vinschen (vinschen@cygnus.com)
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "winsup.h"
#include <unistd.h>
#include "cygerrno.h"
#include "path.h"
#include "fhandler.h"
#include "sync.h"
#include "dtable.h"
#include "cygheap.h"
#include "child_info.h"
#define RANDOM 8
#define URANDOM 9
#define PSEUDO_MULTIPLIER (6364136223846793005LL)
#define PSEUDO_SHIFTVAL (21)
fhandler_dev_random::fhandler_dev_random ()
: fhandler_base (), crypt_prov ((HCRYPTPROV) NULL)
{
}
int
fhandler_dev_random::open (int flags, mode_t)
{
set_flags ((flags & ~O_TEXT) | O_BINARY);
nohandle (true);
set_open_status ();
dummy_offset = 0;
return 1;
}
bool
fhandler_dev_random::crypt_gen_random (void *ptr, size_t len)
{
if (!crypt_prov
&& !CryptAcquireContextW (&crypt_prov, NULL, MS_DEF_PROV_W, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)
&& !CryptAcquireContextW (&crypt_prov, NULL, MS_DEF_PROV_W, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET
| CRYPT_NEWKEYSET))
{
debug_printf ("%E = CryptAquireContext()");
return false;
}
if (!CryptGenRandom (crypt_prov, len, (BYTE *)ptr))
{
debug_printf ("%E = CryptGenRandom()");
return false;
}
return true;
}
int
fhandler_dev_random::pseudo_write (const void *ptr, size_t len)
{
/* Use buffer to mess up the pseudo random number generator. */
for (size_t i = 0; i < len; ++i)
pseudo = (pseudo + ((unsigned char *)ptr)[i]) * PSEUDO_MULTIPLIER + 1;
return len;
}
ssize_t __stdcall
fhandler_dev_random::write (const void *ptr, size_t len)
{
if (!len)
return 0;
if (!ptr)
{
set_errno (EINVAL);
return -1;
}
/* Limit len to a value <= 512 since we don't want to overact.
Copy to local buffer because CryptGenRandom violates const. */
unsigned char buf[512];
size_t limited_len = len <= 512 ? len : 512;
memcpy (buf, ptr, limited_len);
/* Mess up system entropy source. Return error if device is /dev/random. */
if (!crypt_gen_random (buf, limited_len) && dev () == FH_RANDOM)
{
__seterrno ();
return -1;
}
/* Mess up the pseudo random number generator. */
pseudo_write (buf, limited_len);
return len;
}
int
fhandler_dev_random::pseudo_read (void *ptr, size_t len)
{
/* Use pseudo random number generator as fallback entropy source.
This multiplier was obtained from Knuth, D.E., "The Art of
Computer Programming," Vol 2, Seminumerical Algorithms, Third
Edition, Addison-Wesley, 1998, p. 106 (line 26) & p. 108 */
for (size_t i = 0; i < len; ++i)
{
pseudo = pseudo * PSEUDO_MULTIPLIER + 1;
((unsigned char *)ptr)[i] = (pseudo >> PSEUDO_SHIFTVAL) & UCHAR_MAX;
}
return len;
}
void __stdcall
fhandler_dev_random::read (void *ptr, size_t& len)
{
if (!len)
return;
if (!ptr)
{
set_errno (EINVAL);
len = (size_t) -1;
return;
}
if (crypt_gen_random (ptr, len))
return;
/* If device is /dev/urandom, use pseudo number generator as fallback.
Don't do this for /dev/random since it's intended for uses that need
very high quality randomness. */
if (dev () == FH_URANDOM)
{
len = pseudo_read (ptr, len);
return;
}
__seterrno ();
len = (size_t) -1;
}
_off64_t
fhandler_dev_random::lseek (_off64_t off, int whence)
{
/* As on Linux, fake being able to set an offset. The fact that neither
reading nor writing changes the dummy offset is also the same as on
Linux (tested with kernel 2.6.23). */
_off64_t new_off;
switch (whence)
{
case SEEK_SET:
new_off = off;
break;
case SEEK_CUR:
new_off = dummy_offset + off;
break;
default:
set_errno (EINVAL);
return (_off64_t) -1;
}
if (new_off < 0)
{
set_errno (EINVAL);
return (_off64_t) -1;
}
return dummy_offset = new_off;
}
int
fhandler_dev_random::close ()
{
if (!have_execed && crypt_prov)
while (!CryptReleaseContext (crypt_prov, 0)
&& GetLastError () == ERROR_BUSY)
Sleep (10);
return 0;
}
int
fhandler_dev_random::dup (fhandler_base *child, int)
{
fhandler_dev_random *fhr = (fhandler_dev_random *) child;
fhr->crypt_prov = (HCRYPTPROV)NULL;
return 0;
}