Don't use safe_new but new throughout. Fix copyright dates
throughout. * Makefile.in: Accomodate all new files and name changes. Add a *.d dependency. (sbindir): Add. (etcdir): Drop in favor of more appropriate sysconfdir definition. (sysconfdir): Add. (CXXFLAGS): Add -MMD flag. Add SYSCONFDIR definition. (.SUFFIXES): Add. (install): Add action items. (libclean): New target. (fullclean): Ditto. * bsd_helper.cc: New file. * bsd_helper.h: Ditto. * bsd_log.cc: Ditto. * bsd_log.h: Ditto. * bsd_mutex.cc: Ditto. * bsd_mutex.h: Ditto. * client.cc: Rearrange to build as less as possible if __INSIDE_CYGWIN__. (client_request::handle_request): Add Message Queue and Semaphore handling. * cygserver.cc: Rearrange to build as less as possible if __INSIDE_CYGWIN__. Use new debug/log/panic logging functions. (DEF_CONFIG_FILE): New definition for configuration file. Use throughout. (getfunc): Remove. (__cygserver__printf): Remove. (client_request_attach_tty::serve): Return error if impersonation fails. (print_usage): Pump up help message. (print_version): Add output of default configuration file. (main): Accommodate new options. Allow overwrite of threading options from config file. Call several new initialization functions. Drop printing dots. Don't define SIGHANDLE inline. * cygserver.conf: New file. * cygserver_process.h: Rename to process.h. * cygserver_transport.h: Rename to transport.h. * cygserver_transport_pipes.h: Rename to transport_pipes.h. * cygserver_transport_sockets.h: Rename to transport_sockets.h. * msg.cc: Rewrite. * sem.cc: Rewrite. * shm.cc: Rewrite. * sysv_msg.cc: New file, derived from FreeBSD version 1.52. * sysv_sem.cc: New file, derived from FreeBSD version 1.66. * sysv_shm.cc: New file, derived from FreeBSD version 1.89. * threaded_queue.cc: Rearrange to build as less as possible if __INSIDE_CYGWIN__. * transport.cc (transport_layer_base::impersonate_client): Define bool. (transport_layer_base::revert_to_self): Ditto. * transport.h (transport_layer_base::impersonate_client): Declare bool. (transport_layer_base::revert_to_self): Ditto. * transport_pipes.cc (transport_layer_pipes::transport_layer_pipes): Don't call init_security. (init_security): Remove. (transport_layer_pipes::accept): Use global sec_all_nih. (transport_layer_pipes::connect): Ditto. (transport_layer_pipes::impersonate_client): Define bool. (transport_layer_pipes::revert_to_self): Ditt. * transport_pipes.h (transport_layer_pipes::impersonate_client): Declare bool. (transport_layer_pipes::revert_to_self): Ditto. * woutsup.h: Include bsd compatibility headers. (SIGHANDLE): Add definition. (__cygserver__printf): Remove definition. (__noop_printf): Ditto. (debug_printf): Define using debug. (syscall_printf): Define using log. (system_printf): Ditto. Drop all other _printf definitions.
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
/* cygserver.cc
|
||||
|
||||
Copyright 2001, 2002 Red Hat Inc.
|
||||
Copyright 2001, 2002, 2003 Red Hat Inc.
|
||||
|
||||
Written by Egor Duda <deo@logos-m.ru>
|
||||
|
||||
@ -10,6 +10,7 @@ This software is a copyrighted work licensed under the terms of the
|
||||
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
||||
details. */
|
||||
|
||||
#ifdef __OUTSIDE_CYGWIN__
|
||||
#include "woutsup.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
@ -27,100 +28,21 @@ details. */
|
||||
#include "cygwin_version.h"
|
||||
|
||||
#include "cygserver.h"
|
||||
#include "cygserver_process.h"
|
||||
#include "cygserver_transport.h"
|
||||
#include "process.h"
|
||||
#include "transport.h"
|
||||
|
||||
#include "cygserver_ipc.h"
|
||||
#include "cygserver_msg.h"
|
||||
#include "cygserver_sem.h"
|
||||
|
||||
#define DEF_CONFIG_FILE "" SYSCONFDIR "/cygserver.conf"
|
||||
|
||||
// Version string.
|
||||
static const char version[] = "$Revision$";
|
||||
|
||||
/*
|
||||
* Support function for the XXX_printf () macros in "woutsup.h".
|
||||
* Copied verbatim from "strace.cc".
|
||||
*/
|
||||
static int
|
||||
getfunc (char *in_dst, const char *func)
|
||||
{
|
||||
const char *p;
|
||||
const char *pe;
|
||||
char *dst = in_dst;
|
||||
for (p = func; (pe = strchr (p, '(')); p = pe + 1)
|
||||
if (isalnum ((int)pe[-1]) || pe[-1] == '_')
|
||||
break;
|
||||
else if (isspace ((int)pe[-1]))
|
||||
{
|
||||
pe--;
|
||||
break;
|
||||
}
|
||||
if (!pe)
|
||||
pe = strchr (func, '\0');
|
||||
for (p = pe; p > func; p--)
|
||||
if (p != pe && *p == ' ')
|
||||
{
|
||||
p++;
|
||||
break;
|
||||
}
|
||||
if (*p == '*')
|
||||
p++;
|
||||
while (p < pe)
|
||||
*dst++ = *p++;
|
||||
|
||||
*dst++ = ':';
|
||||
*dst++ = ' ';
|
||||
*dst = '\0';
|
||||
|
||||
return dst - in_dst;
|
||||
}
|
||||
|
||||
/*
|
||||
* Support function for the XXX_printf () macros in "woutsup.h".
|
||||
*/
|
||||
extern "C" void
|
||||
__cygserver__printf (const char *const function, const char *const fmt, ...)
|
||||
{
|
||||
const DWORD lasterror = GetLastError ();
|
||||
const int lasterrno = errno;
|
||||
|
||||
va_list ap;
|
||||
|
||||
char *const buf = (char *) alloca (BUFSIZ);
|
||||
|
||||
assert (buf);
|
||||
|
||||
int len = 0;
|
||||
|
||||
if (function)
|
||||
len += getfunc (buf, function);
|
||||
|
||||
va_start (ap, fmt);
|
||||
len += vsnprintf (buf + len, BUFSIZ - len, fmt, ap);
|
||||
va_end (ap);
|
||||
|
||||
len += snprintf (buf + len, BUFSIZ - len, "\n");
|
||||
|
||||
const int actual = (len > BUFSIZ ? BUFSIZ : len);
|
||||
|
||||
write (2, buf, actual);
|
||||
|
||||
errno = lasterrno;
|
||||
SetLastError (lasterror);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef DEBUGGING
|
||||
|
||||
int __stdcall
|
||||
__set_errno (const char *func, int ln, int val)
|
||||
{
|
||||
debug_printf ("%s:%d val %d", func, ln, val);
|
||||
return _impure_ptr->_errno = val;
|
||||
}
|
||||
|
||||
#endif /* DEBUGGING */
|
||||
|
||||
GENERIC_MAPPING access_mapping;
|
||||
|
||||
static BOOL
|
||||
static bool
|
||||
setup_privileges ()
|
||||
{
|
||||
BOOL rc, ret_val;
|
||||
@ -130,15 +52,14 @@ setup_privileges ()
|
||||
rc = OpenProcessToken (GetCurrentProcess () , TOKEN_ALL_ACCESS , &hToken) ;
|
||||
if (!rc)
|
||||
{
|
||||
system_printf ("error opening process token (%lu)", GetLastError ());
|
||||
ret_val = FALSE;
|
||||
goto out;
|
||||
debug ("error opening process token (%lu)", GetLastError ());
|
||||
return false;
|
||||
}
|
||||
rc = LookupPrivilegeValue (NULL, SE_DEBUG_NAME, &sPrivileges.Privileges[0].Luid);
|
||||
if (!rc)
|
||||
{
|
||||
system_printf ("error getting privilege luid (%lu)", GetLastError ());
|
||||
ret_val = FALSE;
|
||||
debug ("error getting privilege luid (%lu)", GetLastError ());
|
||||
ret_val = false;
|
||||
goto out;
|
||||
}
|
||||
sPrivileges.PrivilegeCount = 1 ;
|
||||
@ -146,9 +67,8 @@ setup_privileges ()
|
||||
rc = AdjustTokenPrivileges (hToken, FALSE, &sPrivileges, 0, NULL, NULL) ;
|
||||
if (!rc)
|
||||
{
|
||||
system_printf ("error adjusting privilege level. (%lu)",
|
||||
GetLastError ());
|
||||
ret_val = FALSE;
|
||||
debug ("error adjusting privilege level. (%lu)", GetLastError ());
|
||||
ret_val = false;
|
||||
goto out;
|
||||
}
|
||||
|
||||
@ -157,7 +77,7 @@ setup_privileges ()
|
||||
access_mapping.GenericExecute = 0;
|
||||
access_mapping.GenericAll = FILE_READ_DATA | FILE_WRITE_DATA;
|
||||
|
||||
ret_val = TRUE;
|
||||
ret_val = true;
|
||||
|
||||
out:
|
||||
CloseHandle (hToken);
|
||||
@ -181,7 +101,7 @@ check_and_dup_handle (HANDLE from_process, HANDLE to_process,
|
||||
0, bInheritHandle,
|
||||
DUPLICATE_SAME_ACCESS))
|
||||
{
|
||||
system_printf ("error getting handle(%u) to server (%lu)",
|
||||
log (LOG_ERR, "error getting handle(%u) to server (%lu)",
|
||||
(unsigned int)from_handle, GetLastError ());
|
||||
goto out;
|
||||
}
|
||||
@ -205,7 +125,7 @@ check_and_dup_handle (HANDLE from_process, HANDLE to_process,
|
||||
| DACL_SECURITY_INFORMATION),
|
||||
sd, sizeof (sd_buf), &bytes_needed))
|
||||
{
|
||||
system_printf ("error getting handle SD (%lu)", GetLastError ());
|
||||
log (LOG_ERR, "error getting handle SD (%lu)", GetLastError ());
|
||||
goto out;
|
||||
}
|
||||
|
||||
@ -214,14 +134,14 @@ check_and_dup_handle (HANDLE from_process, HANDLE to_process,
|
||||
if (!AccessCheck (sd, from_process_token, access, &access_mapping,
|
||||
&ps, &ps_len, &access, &status))
|
||||
{
|
||||
system_printf ("error checking access rights (%lu)",
|
||||
log (LOG_ERR, "error checking access rights (%lu)",
|
||||
GetLastError ());
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (!status)
|
||||
{
|
||||
system_printf ("access to object denied");
|
||||
log (LOG_ERR, "access to object denied");
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
@ -230,11 +150,11 @@ check_and_dup_handle (HANDLE from_process, HANDLE to_process,
|
||||
to_process, to_handle_ptr,
|
||||
access, bInheritHandle, 0))
|
||||
{
|
||||
system_printf ("error getting handle to client (%lu)", GetLastError ());
|
||||
log (LOG_ERR, "error getting handle to client (%lu)", GetLastError ());
|
||||
goto out;
|
||||
}
|
||||
|
||||
// verbose: debug_printf ("Duplicated %p to %p", from_handle, *to_handle_ptr);
|
||||
debug ("Duplicated %p to %p", from_handle, *to_handle_ptr);
|
||||
|
||||
ret_val = 0;
|
||||
|
||||
@ -259,7 +179,7 @@ client_request_attach_tty::serve (transport_layer_base *const conn,
|
||||
|
||||
if (!wincap.has_security ())
|
||||
{
|
||||
syscall_printf ("operation only supported on systems with security");
|
||||
log (LOG_NOTICE, "operation only supported on systems with security");
|
||||
error_code (EINVAL);
|
||||
msglen (0);
|
||||
return;
|
||||
@ -267,7 +187,7 @@ client_request_attach_tty::serve (transport_layer_base *const conn,
|
||||
|
||||
if (msglen () != sizeof (req))
|
||||
{
|
||||
syscall_printf ("bad request body length: expecting %lu bytes, got %lu",
|
||||
log (LOG_ERR, "bad request body length: expecting %lu bytes, got %lu",
|
||||
sizeof (req), msglen ());
|
||||
error_code (EINVAL);
|
||||
msglen (0);
|
||||
@ -276,54 +196,65 @@ client_request_attach_tty::serve (transport_layer_base *const conn,
|
||||
|
||||
msglen (0); // Until we fill in some fields.
|
||||
|
||||
// verbose: debug_printf ("pid %ld:(%p,%p) -> pid %ld",
|
||||
// req.master_pid, req.from_master, req.to_master,
|
||||
// req.pid);
|
||||
debug ("pid %ld:(%p,%p) -> pid %ld", req.master_pid, req.from_master,
|
||||
req.to_master, req.pid);
|
||||
|
||||
// verbose: debug_printf ("opening process %ld", req.master_pid);
|
||||
debug ("opening process %ld", req.master_pid);
|
||||
|
||||
const HANDLE from_process_handle =
|
||||
OpenProcess (PROCESS_DUP_HANDLE, FALSE, req.master_pid);
|
||||
|
||||
if (!from_process_handle)
|
||||
{
|
||||
system_printf ("error opening `from' process, error = %lu",
|
||||
log (LOG_ERR, "error opening `from' process, error = %lu",
|
||||
GetLastError ());
|
||||
error_code (EACCES);
|
||||
return;
|
||||
}
|
||||
|
||||
// verbose: debug_printf ("opening process %ld", req.pid);
|
||||
debug ("opening process %ld", req.pid);
|
||||
|
||||
const HANDLE to_process_handle =
|
||||
OpenProcess (PROCESS_DUP_HANDLE, FALSE, req.pid);
|
||||
|
||||
if (!to_process_handle)
|
||||
{
|
||||
system_printf ("error opening `to' process, error = %lu",
|
||||
log (LOG_ERR, "error opening `to' process, error = %lu",
|
||||
GetLastError ());
|
||||
CloseHandle (from_process_handle);
|
||||
error_code (EACCES);
|
||||
return;
|
||||
}
|
||||
|
||||
// verbose: debug_printf ("Impersonating client");
|
||||
conn->impersonate_client ();
|
||||
debug ("Impersonating client");
|
||||
if (!conn->impersonate_client ())
|
||||
{
|
||||
CloseHandle (from_process_handle);
|
||||
CloseHandle (to_process_handle);
|
||||
error_code (EACCES);
|
||||
return;
|
||||
}
|
||||
|
||||
HANDLE token_handle = NULL;
|
||||
|
||||
// verbose: debug_printf ("about to open thread token");
|
||||
debug ("about to open thread token");
|
||||
const DWORD rc = OpenThreadToken (GetCurrentThread (),
|
||||
TOKEN_QUERY,
|
||||
TRUE,
|
||||
&token_handle);
|
||||
|
||||
// verbose: debug_printf ("opened thread token, rc=%lu", rc);
|
||||
conn->revert_to_self ();
|
||||
debug ("opened thread token, rc=%lu", rc);
|
||||
if (!conn->revert_to_self ())
|
||||
{
|
||||
CloseHandle (from_process_handle);
|
||||
CloseHandle (to_process_handle);
|
||||
error_code (EACCES);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!rc)
|
||||
{
|
||||
system_printf ("error opening thread token, error = %lu",
|
||||
log (LOG_ERR, "error opening thread token, error = %lu",
|
||||
GetLastError ());
|
||||
CloseHandle (from_process_handle);
|
||||
CloseHandle (to_process_handle);
|
||||
@ -348,7 +279,7 @@ client_request_attach_tty::serve (transport_layer_base *const conn,
|
||||
from_master,
|
||||
&req.from_master, TRUE) != 0)
|
||||
{
|
||||
system_printf ("error duplicating from_master handle, error = %lu",
|
||||
log (LOG_ERR, "error duplicating from_master handle, error = %lu",
|
||||
GetLastError ());
|
||||
error_code (EACCES);
|
||||
}
|
||||
@ -360,7 +291,7 @@ client_request_attach_tty::serve (transport_layer_base *const conn,
|
||||
to_master,
|
||||
&req.to_master, TRUE) != 0)
|
||||
{
|
||||
system_printf ("error duplicating to_master handle, error = %lu",
|
||||
log (LOG_ERR, "error duplicating to_master handle, error = %lu",
|
||||
GetLastError ());
|
||||
error_code (EACCES);
|
||||
}
|
||||
@ -369,7 +300,7 @@ client_request_attach_tty::serve (transport_layer_base *const conn,
|
||||
CloseHandle (to_process_handle);
|
||||
CloseHandle (token_handle);
|
||||
|
||||
debug_printf ("%lu(%lu, %lu) -> %lu(%lu,%lu)",
|
||||
debug ("%lu(%lu, %lu) -> %lu(%lu,%lu)",
|
||||
req.master_pid, from_master, to_master,
|
||||
req.pid, req.from_master, req.to_master);
|
||||
|
||||
@ -382,7 +313,7 @@ client_request_get_version::serve (transport_layer_base *, process_cache *)
|
||||
assert (!error_code ());
|
||||
|
||||
if (msglen ())
|
||||
syscall_printf ("unexpected request body ignored: %lu bytes", msglen ());
|
||||
log (LOG_ERR, "unexpected request body ignored: %lu bytes", msglen ());
|
||||
|
||||
msglen (sizeof (version));
|
||||
|
||||
@ -401,7 +332,7 @@ public:
|
||||
|
||||
virtual ~server_request ()
|
||||
{
|
||||
safe_delete (_conn);
|
||||
delete _conn;
|
||||
}
|
||||
|
||||
virtual void process ()
|
||||
@ -455,8 +386,8 @@ server_submission_loop::request_loop ()
|
||||
*/
|
||||
if (!SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_HIGHEST + 1))
|
||||
if (!SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_HIGHEST))
|
||||
debug_printf ("failed to raise accept thread priority, error = %lu",
|
||||
GetLastError ());
|
||||
debug ("failed to raise accept thread priority, error = %lu",
|
||||
GetLastError ());
|
||||
|
||||
while (_running)
|
||||
{
|
||||
@ -464,7 +395,7 @@ server_submission_loop::request_loop ()
|
||||
transport_layer_base *const conn = _transport->accept (&recoverable);
|
||||
if (!conn && !recoverable)
|
||||
{
|
||||
system_printf ("fatal error on IPC transport: closing down");
|
||||
log (LOG_ERR, "fatal error on IPC transport: closing down");
|
||||
return;
|
||||
}
|
||||
// EINTR probably implies a shutdown request; so back off for a
|
||||
@ -474,26 +405,25 @@ server_submission_loop::request_loop ()
|
||||
if (!conn && errno == EINTR)
|
||||
{
|
||||
if (!SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_NORMAL))
|
||||
debug_printf ("failed to reset thread priority, error = %lu",
|
||||
GetLastError ());
|
||||
debug ("failed to reset thread priority, error = %lu",
|
||||
GetLastError ());
|
||||
|
||||
Sleep (0);
|
||||
if (!SetThreadPriority (GetCurrentThread (),
|
||||
THREAD_PRIORITY_HIGHEST + 1))
|
||||
if (!SetThreadPriority (GetCurrentThread (),
|
||||
THREAD_PRIORITY_HIGHEST))
|
||||
debug_printf ("failed to raise thread priority, error = %lu",
|
||||
GetLastError ());
|
||||
debug ("failed to raise thread priority, error = %lu",
|
||||
GetLastError ());
|
||||
}
|
||||
if (conn)
|
||||
_queue->add (safe_new (server_request, conn, _cache));
|
||||
_queue->add (new server_request (conn, _cache));
|
||||
}
|
||||
}
|
||||
|
||||
client_request_shutdown::client_request_shutdown ()
|
||||
: client_request (CYGSERVER_REQUEST_SHUTDOWN)
|
||||
{
|
||||
// verbose: syscall_printf ("created");
|
||||
}
|
||||
|
||||
void
|
||||
@ -502,7 +432,7 @@ client_request_shutdown::serve (transport_layer_base *, process_cache *)
|
||||
assert (!error_code ());
|
||||
|
||||
if (msglen ())
|
||||
syscall_printf ("unexpected request body ignored: %lu bytes", msglen ());
|
||||
log (LOG_ERR, "unexpected request body ignored: %lu bytes", msglen ());
|
||||
|
||||
/* FIXME: link upwards, and then this becomes a trivial method call to
|
||||
* only shutdown _this queue_
|
||||
@ -530,12 +460,33 @@ handle_signal (const int signum)
|
||||
static void
|
||||
print_usage (const char *const pgm)
|
||||
{
|
||||
printf ("Usage: %s [OPTIONS]\n", pgm);
|
||||
printf (" -c, --cleanup-threads number of cleanup threads to use\n");
|
||||
printf (" -h, --help output usage information and exit\n");
|
||||
printf (" -r, --request-threads number of request threads to use\n");
|
||||
printf (" -s, --shutdown shutdown the daemon\n");
|
||||
printf (" -v, --version output version information and exit\n");
|
||||
log (LOG_NOTICE, "Usage: %s [OPTIONS]\n"
|
||||
"Configuration option:\n"
|
||||
" -f, --config-file <file> Use <file> as config file. Default is\n"
|
||||
"\n"
|
||||
"Performance options:\n"
|
||||
" -c, --cleanup-threads <num> Number of cleanup threads to use.\n"
|
||||
" -r, --request-threads <num> Number of request threads to use.\n"
|
||||
"\n"
|
||||
"Logging options:\n"
|
||||
" -d, --debug Log debug messages to stderr.\n"
|
||||
" -e, --stderr Log to stderr (default if stderr is a tty).\n"
|
||||
" -E, --no-stderr Don't log to stderr (see -y, -Y options).\n"
|
||||
" " DEF_CONFIG_FILE "\n"
|
||||
" -l, --log-level <level> Verbosity of logging (1..7). Default: 6\n"
|
||||
" -y, --syslog Log to syslog (default if stderr is no tty).\n"
|
||||
" -Y, --no-syslog Don't log to syslog (See -e, -E options).\n"
|
||||
"\n"
|
||||
"Support options:\n"
|
||||
" -m, --no-sharedmem Don't start XSI Shared Memory support.\n"
|
||||
" -q, --no-msgqueues Don't start XSI Message Queue support.\n"
|
||||
" -s, --no-semaphores Don't start XSI Semaphore support.\n"
|
||||
"\n"
|
||||
"Miscellaneous:\n"
|
||||
" -S, --shutdown Shutdown the daemon.\n"
|
||||
" -h, --help Output usage information and exit.\n"
|
||||
" -v, --version Output version information and exit."
|
||||
, pgm);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -543,7 +494,7 @@ print_usage (const char *const pgm)
|
||||
*/
|
||||
|
||||
static void
|
||||
print_version (const char *const pgm)
|
||||
print_version ()
|
||||
{
|
||||
char *vn = NULL;
|
||||
|
||||
@ -578,10 +529,12 @@ print_version (const char *const pgm)
|
||||
cygwin_version.mount_registry,
|
||||
cygwin_version.dll_build_date);
|
||||
|
||||
printf ("%s (cygwin) %s\n", pgm, vn);
|
||||
printf ("API version %s\n", buf);
|
||||
printf ("Copyright 2001, 2002 Red Hat, Inc.\n");
|
||||
printf ("Compiled on %s\n", __DATE__);
|
||||
log (LOG_INFO, "(cygwin) %s\n"
|
||||
"API version %s\n"
|
||||
"Copyright 2001, 2002, 2003 Red Hat, Inc.\n"
|
||||
"Compiled on %s\n"
|
||||
"Default configuration file is %s",
|
||||
vn, buf, __DATE__, DEF_CONFIG_FILE);
|
||||
|
||||
free (vn);
|
||||
}
|
||||
@ -595,79 +548,127 @@ main (const int argc, char *argv[])
|
||||
{
|
||||
const struct option longopts[] = {
|
||||
{"cleanup-threads", required_argument, NULL, 'c'},
|
||||
{"debug", no_argument, NULL, 'd'},
|
||||
{"stderr", no_argument, NULL, 'e'},
|
||||
{"no-stderr", no_argument, NULL, 'E'},
|
||||
{"config-file", required_argument, NULL, 'f'},
|
||||
{"help", no_argument, NULL, 'h'},
|
||||
{"log-level", required_argument, NULL, 'l'},
|
||||
{"no-sharedmem", no_argument, NULL, 'm'},
|
||||
{"no-msgqueues", no_argument, NULL, 'q'},
|
||||
{"request-threads", required_argument, NULL, 'r'},
|
||||
{"shutdown", no_argument, NULL, 's'},
|
||||
{"no-semaphores", no_argument, NULL, 's'},
|
||||
{"shutdown", no_argument, NULL, 'S'},
|
||||
{"version", no_argument, NULL, 'v'},
|
||||
{"syslog", no_argument, NULL, 'y'},
|
||||
{"no-syslog", no_argument, NULL, 'Y'},
|
||||
{0, no_argument, NULL, 0}
|
||||
};
|
||||
|
||||
const char opts[] = "c:hr:sv";
|
||||
const char opts[] = "c:deEf:hl:mqr:sSvyY";
|
||||
|
||||
int cleanup_threads = 2;
|
||||
int request_threads = 10;
|
||||
long cleanup_threads = 0;
|
||||
long request_threads = 0;
|
||||
bool shutdown = false;
|
||||
const char *config_file = DEF_CONFIG_FILE;
|
||||
bool force_config_file = false;
|
||||
tun_bool_t option_log_stderr = TUN_UNDEF;
|
||||
tun_bool_t option_log_syslog = TUN_UNDEF;
|
||||
|
||||
const char *pgm = NULL;
|
||||
char *c = NULL;
|
||||
|
||||
if (!(pgm = strrchr (*argv, '\\')) && !(pgm = strrchr (*argv, '/')))
|
||||
pgm = *argv;
|
||||
/* Check if we have a terminal. If so, default to stderr logging,
|
||||
otherwise default to syslog logging. This must be done early
|
||||
to allow default logging already in option processing state. */
|
||||
openlog ("cygserver", LOG_PID, LOG_KERN);
|
||||
if (isatty (2))
|
||||
log_stderr = TUN_TRUE;
|
||||
else
|
||||
pgm++;
|
||||
|
||||
wincap.init ();
|
||||
if (wincap.has_security ())
|
||||
setup_privileges ();
|
||||
log_syslog = TUN_TRUE;
|
||||
|
||||
int opt;
|
||||
|
||||
opterr = 0;
|
||||
while ((opt = getopt_long (argc, argv, opts, longopts, NULL)) != EOF)
|
||||
switch (opt)
|
||||
{
|
||||
case 'c':
|
||||
cleanup_threads = atoi (optarg);
|
||||
if (cleanup_threads <= 0)
|
||||
{
|
||||
fprintf (stderr,
|
||||
"%s: number of cleanup threads must be positive\n",
|
||||
pgm);
|
||||
exit (1);
|
||||
}
|
||||
c = NULL;
|
||||
cleanup_threads = strtol (optarg, &c, 10);
|
||||
if (cleanup_threads <= 0 || cleanup_threads > 16 || (c && *c))
|
||||
panic ("Number of cleanup threads must be between 1 and 16");
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
log_debug = TUN_TRUE;
|
||||
break;
|
||||
|
||||
case 'e':
|
||||
option_log_stderr = TUN_TRUE;
|
||||
break;
|
||||
|
||||
case 'E':
|
||||
option_log_stderr = TUN_FALSE;
|
||||
break;
|
||||
|
||||
case 'f':
|
||||
config_file = optarg;
|
||||
force_config_file = true;
|
||||
break;
|
||||
|
||||
case 'h':
|
||||
print_usage (pgm);
|
||||
print_usage (getprogname ());
|
||||
return 0;
|
||||
|
||||
case 'l':
|
||||
c = NULL;
|
||||
log_level = strtoul (optarg, &c, 10);
|
||||
if (!log_level || log_level > 7 || (c && *c))
|
||||
panic ("Log level must be between 1 and 7");
|
||||
break;
|
||||
|
||||
case 'm':
|
||||
support_sharedmem = TUN_FALSE;
|
||||
break;
|
||||
|
||||
case 'q':
|
||||
support_msgqueues = TUN_FALSE;
|
||||
break;
|
||||
|
||||
case 'r':
|
||||
request_threads = atoi (optarg);
|
||||
if (request_threads <= 0)
|
||||
{
|
||||
fprintf (stderr,
|
||||
"%s: number of request threads must be positive\n",
|
||||
pgm);
|
||||
exit (1);
|
||||
}
|
||||
c = NULL;
|
||||
request_threads = strtol (optarg, &c, 10);
|
||||
if (request_threads <= 0 || request_threads > 64 || (c && *c))
|
||||
panic ("Number of request threads must be between 1 and 64");
|
||||
break;
|
||||
|
||||
case 's':
|
||||
support_semaphores = TUN_FALSE;
|
||||
break;
|
||||
|
||||
case 'S':
|
||||
shutdown = true;
|
||||
break;
|
||||
|
||||
case 'v':
|
||||
print_version (pgm);
|
||||
print_version ();
|
||||
return 0;
|
||||
|
||||
case 'y':
|
||||
option_log_syslog = TUN_TRUE;
|
||||
break;
|
||||
|
||||
case 'Y':
|
||||
option_log_syslog = TUN_FALSE;
|
||||
break;
|
||||
|
||||
case '?':
|
||||
fprintf (stderr, "Try `%s --help' for more information.\n", pgm);
|
||||
exit (1);
|
||||
panic ("unknown option -- %c\n"
|
||||
"Try `%s --help' for more information.", optopt, getprogname ());
|
||||
}
|
||||
|
||||
if (optind != argc)
|
||||
{
|
||||
fprintf (stderr, "%s: too many arguments\n", pgm);
|
||||
exit (1);
|
||||
}
|
||||
panic ("Too many arguments");
|
||||
|
||||
if (shutdown)
|
||||
{
|
||||
@ -679,71 +680,76 @@ main (const int argc, char *argv[])
|
||||
client_request_shutdown req;
|
||||
|
||||
if (req.make_request () == -1 || req.error_code ())
|
||||
{
|
||||
fprintf (stderr, "%s: shutdown request failed: %s\n",
|
||||
pgm, strerror (req.error_code ()));
|
||||
exit (1);
|
||||
}
|
||||
panic("Shutdown request failed: %s", strerror (req.error_code ()));
|
||||
|
||||
// FIXME: It would be nice to wait here for the daemon to exit.
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define SIGHANDLE(SIG) \
|
||||
do \
|
||||
{ \
|
||||
struct sigaction act; \
|
||||
\
|
||||
act.sa_handler = &handle_signal; \
|
||||
act.sa_mask = 0; \
|
||||
act.sa_flags = 0; \
|
||||
\
|
||||
if (sigaction (SIG, &act, NULL) == -1) \
|
||||
{ \
|
||||
system_printf ("failed to install handler for " #SIG ": %s", \
|
||||
strerror (errno)); \
|
||||
exit (1); \
|
||||
} \
|
||||
} while (false)
|
||||
|
||||
SIGHANDLE (SIGHUP);
|
||||
SIGHANDLE (SIGINT);
|
||||
SIGHANDLE (SIGTERM);
|
||||
|
||||
print_version (pgm);
|
||||
setbuf (stdout, NULL);
|
||||
printf ("daemon starting up");
|
||||
tunable_param_init (config_file, force_config_file);
|
||||
|
||||
loginit (option_log_stderr, option_log_syslog);
|
||||
|
||||
log (LOG_INFO, "daemon starting up");
|
||||
|
||||
if (!cleanup_threads)
|
||||
TUNABLE_INT_FETCH ("kern.srv.cleanup_threads", &cleanup_threads);
|
||||
if (!cleanup_threads)
|
||||
cleanup_threads = 2;
|
||||
|
||||
if (!request_threads)
|
||||
TUNABLE_INT_FETCH ("kern.srv.request_threads", &request_threads);
|
||||
if (!request_threads)
|
||||
request_threads = 10;
|
||||
|
||||
if (support_sharedmem == TUN_UNDEF)
|
||||
TUNABLE_BOOL_FETCH ("kern.srv.sharedmem", &support_sharedmem);
|
||||
if (support_sharedmem == TUN_UNDEF)
|
||||
support_sharedmem = TUN_TRUE;
|
||||
|
||||
if (support_msgqueues == TUN_UNDEF)
|
||||
TUNABLE_BOOL_FETCH ("kern.srv.msgqueues", &support_msgqueues);
|
||||
if (support_msgqueues == TUN_UNDEF)
|
||||
support_msgqueues = TUN_TRUE;
|
||||
|
||||
if (support_semaphores == TUN_UNDEF)
|
||||
TUNABLE_BOOL_FETCH ("kern.srv.semaphores", &support_semaphores);
|
||||
if (support_semaphores == TUN_UNDEF)
|
||||
support_semaphores = TUN_TRUE;
|
||||
|
||||
wincap.init ();
|
||||
if (wincap.has_security () && !setup_privileges ())
|
||||
panic ("Setting process privileges failed.");
|
||||
|
||||
/*XXXXX*/
|
||||
threaded_queue request_queue (request_threads);
|
||||
printf (".");
|
||||
|
||||
transport_layer_base *const transport = create_server_transport ();
|
||||
assert (transport);
|
||||
printf (".");
|
||||
|
||||
process_cache cache (cleanup_threads);
|
||||
printf (".");
|
||||
|
||||
server_submission_loop submission_loop (&request_queue, transport, &cache);
|
||||
printf (".");
|
||||
|
||||
request_queue.add_submission_loop (&submission_loop);
|
||||
printf (".");
|
||||
|
||||
if (transport->listen () == -1)
|
||||
{
|
||||
exit (1);
|
||||
}
|
||||
printf (".");
|
||||
|
||||
ipcinit ();
|
||||
|
||||
cache.start ();
|
||||
printf (".");
|
||||
|
||||
request_queue.start ();
|
||||
printf (".");
|
||||
|
||||
printf ("complete\n");
|
||||
log (LOG_NOTICE, "Initialization complete. Waiting for requests.");
|
||||
|
||||
/* TODO: wait on multiple objects - the thread handle for each
|
||||
* request loop + all the process handles. This should be done by
|
||||
@ -758,16 +764,25 @@ main (const int argc, char *argv[])
|
||||
-- if signal event then retrigger it
|
||||
*/
|
||||
while (!shutdown_server && request_queue.running () && cache.running ())
|
||||
pause ();
|
||||
{
|
||||
pause ();
|
||||
if (ipcunload ())
|
||||
{
|
||||
shutdown_server = false;
|
||||
log (LOG_WARNING, "Shutdown request received but ignored. "
|
||||
"Dependent processes still running.");
|
||||
}
|
||||
}
|
||||
|
||||
printf ("\nShutdown request received - new requests will be denied\n");
|
||||
log (LOG_INFO, "Shutdown request received - new requests will be denied");
|
||||
request_queue.stop ();
|
||||
printf ("All pending requests processed\n");
|
||||
safe_delete (transport);
|
||||
printf ("No longer accepting requests - cygwin will operate in daemonless mode\n");
|
||||
log (LOG_INFO, "All pending requests processed");
|
||||
delete transport;
|
||||
log (LOG_INFO, "No longer accepting requests - cygwin will operate in daemonless mode");
|
||||
cache.stop ();
|
||||
printf ("All outstanding process-cache activities completed\n");
|
||||
printf ("daemon shutdown\n");
|
||||
log (LOG_INFO, "All outstanding process-cache activities completed");
|
||||
log (LOG_NOTICE, "Shutdown finished.");
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* __OUTSIDE_CYGWIN__ */
|
||||
|
Reference in New Issue
Block a user