added deamonizing

This commit is contained in:
jsorg71 2005-11-06 00:30:53 +00:00
parent 97b1407d91
commit bd2ace23b8

View File

@ -93,8 +93,8 @@ xrdp_shutdown(int sig)
{
return;
}
g_printf("shutting down\n\r");
g_printf("signal %d threadid %d\n\r", sig, g_get_threadid());
g_printf("shutting down\r\n");
g_printf("signal %d threadid %d\r\n", sig, g_get_threadid());
listen = g_listen;
g_listen = 0;
if (listen != 0)
@ -103,6 +103,8 @@ xrdp_shutdown(int sig)
g_sleep(1000);
xrdp_listen_delete(listen);
}
/* delete the xrdp.pid file */
g_file_delete("./xrdp.pid");
}
/*****************************************************************************/
@ -165,7 +167,7 @@ void DEFAULT_CC
pipe_sig(int sig_num)
{
/* do nothing */
g_printf("got SIGPIPE(%d)\n\r", sig_num);
g_printf("got SIGPIPE(%d)\r\n", sig_num);
}
/*****************************************************************************/
@ -195,6 +197,11 @@ main(int argc, char** argv)
int host_be;
#if defined(_WIN32)
WSADATA w;
#else
int pid;
int fd;
int no_daemon;
char text[32];
#endif
/* check compiled endian with actual edian */
@ -207,18 +214,23 @@ main(int argc, char** argv)
if (host_be)
#endif
{
g_printf("endian wrong, edit arch.h\n\r");
g_printf("endian wrong, edit arch.h\r\n");
return 0;
}
/* check long, int and void* sizes */
if (sizeof(int) != 4)
{
g_printf("unusable int size, must be 4\n\r");
g_printf("unusable int size, must be 4\r\n");
return 0;
}
if (sizeof(long) != sizeof(void*) || (sizeof(long) != 4 && sizeof(long) != 8))
if (sizeof(long) != sizeof(void*))
{
g_printf("unusable long size, must be 4 or 8\n\r");
g_printf("long size must match void* size\r\n");
return 0;
}
if (sizeof(long) != 4 && sizeof(long) != 8)
{
g_printf("unusable long size, must be 4 or 8\r\n");
return 0;
}
#if defined(_WIN32)
@ -226,6 +238,117 @@ main(int argc, char** argv)
InitializeCriticalSection(&g_term_mutex);
InitializeCriticalSection(&g_sync_mutex);
InitializeCriticalSection(&g_sync1_mutex);
#else
no_daemon = 0;
if (argc == 2)
{
if (g_strncasecmp(argv[1], "-kill", 255) == 0 ||
g_strncasecmp(argv[1], "--kill", 255) == 0)
{
g_printf("stopping xrdp\r\n");
/* read the xrdp.pid file */
fd = -1;
if (g_file_exist("./xrdp.pid"))
{
fd = g_file_open("./xrdp.pid");
}
if (fd == -1)
{
g_printf("problem opening to xrdp.pid\r\n");
g_printf("maybe its not running\r\n");
}
else
{
g_memset(text, 0, 32);
g_file_read(fd, text, 31);
pid = g_atoi(text);
g_printf("stopping process id %d\r\n", pid);
if (pid > 0)
{
g_sigterm(pid);
}
g_file_close(fd);
}
g_exit(0);
}
else if (g_strncasecmp(argv[1], "-nodeamon", 255) == 0 ||
g_strncasecmp(argv[1], "--nodeamon", 255) == 0)
{
no_daemon = 1;
}
else if (g_strncasecmp(argv[1], "-help", 255) == 0 ||
g_strncasecmp(argv[1], "--help", 255) == 0 ||
g_strncasecmp(argv[1], "-h", 255) == 0)
{
g_printf("\r\n");
g_printf("xrdp: A Remote Desktop Protocol server.\r\n");
g_printf("Copyright (C) Jay Sorg 2004-2005\r\n");
g_printf("See http://xrdp.sourceforge.net for more information.\r\n");
g_printf("\r\n");
g_printf("Usage: xrdp [options]\r\n");
g_printf(" -h: show help\r\n");
g_printf(" -nodeamon: don't fork into background\r\n");
g_printf(" -kill: shut down xrdp\r\n");
g_printf("\r\n");
g_exit(0);
}
else
{
g_printf("unknown parameter\r\n");
g_exit(0);
}
}
else if (argc > 1)
{
g_printf("unknown parameter\r\n");
g_exit(0);
}
if (g_file_exist("./xrdp.pid"))
{
g_printf("It looks like xrdp is allready running,\r\n");
g_printf("if not delete the xrdp.pid file and try again\r\n");
g_exit(0);
}
if (!no_daemon)
{
/* start of daemonizing code */
pid = g_fork();
if (pid == -1)
{
g_printf("problem forking\r\n");
g_exit(1);
}
if (0 != pid)
{
g_printf("process %d started ok\r\n", pid);
/* exit, this is the main process */
g_exit(0);
}
g_sleep(1000);
g_file_close(0);
g_file_close(1);
g_file_close(2);
g_file_open("/dev/null");
g_file_open("/dev/null");
g_file_open("/dev/null");
/* end of daemonizing code */
}
/* write the pid to file */
pid = g_getpid();
fd = g_file_open("./xrdp.pid");
if (fd == -1)
{
g_printf("trying to write process id to xrdp.pid\r\n");
g_printf("problem opening xrdp.pid\r\n");
g_printf("maybe no rights\r\n");
}
else
{
g_set_file_rights("./xrdp.pid", 1, 1);
g_sprintf(text, "%d", pid);
g_file_write(fd, text, g_strlen(text));
g_file_close(fd);
}
#endif
g_threadid = g_get_threadid();
g_listen = xrdp_listen_create();