2005-01-30 07:34:19 +03:00
|
|
|
/*
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
xrdp: A Remote Desktop Protocol server.
|
2008-01-30 10:53:57 +03:00
|
|
|
Copyright (C) Jay Sorg 2005-2008
|
2005-01-30 07:34:19 +03:00
|
|
|
*/
|
|
|
|
|
2006-05-26 00:34:32 +04:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @file sesman.c
|
|
|
|
* @brief Main program file
|
|
|
|
* @author Jay Sorg
|
2008-07-30 14:58:30 +04:00
|
|
|
*
|
2006-05-26 00:34:32 +04:00
|
|
|
*/
|
|
|
|
|
2005-09-25 07:30:09 +04:00
|
|
|
#include "sesman.h"
|
2005-11-14 01:06:49 +03:00
|
|
|
|
2005-10-06 23:21:18 +04:00
|
|
|
int g_sck;
|
|
|
|
int g_pid;
|
|
|
|
unsigned char g_fixedkey[8] = { 23, 82, 107, 6, 35, 78, 88, 7 };
|
2008-07-30 14:58:30 +04:00
|
|
|
struct config_sesman* g_cfg; /* config.h */
|
2005-01-30 07:34:19 +03:00
|
|
|
|
2008-12-26 05:20:03 +03:00
|
|
|
extern int g_thread_sck; /* in thread.c */
|
2005-01-30 07:34:19 +03:00
|
|
|
|
2006-05-26 00:34:32 +04:00
|
|
|
/******************************************************************************/
|
2005-11-14 01:06:49 +03:00
|
|
|
/**
|
|
|
|
*
|
2006-05-26 00:34:32 +04:00
|
|
|
* @brief Starts sesman main loop
|
2005-11-14 01:06:49 +03:00
|
|
|
*
|
|
|
|
*/
|
2008-07-30 14:58:30 +04:00
|
|
|
static void DEFAULT_CC
|
2006-08-24 09:58:10 +04:00
|
|
|
sesman_main_loop(void)
|
2005-01-30 07:34:19 +03:00
|
|
|
{
|
|
|
|
int in_sck;
|
2005-02-13 06:52:55 +03:00
|
|
|
int error;
|
2005-01-30 07:34:19 +03:00
|
|
|
|
2005-10-09 23:53:51 +04:00
|
|
|
/*main program loop*/
|
2008-07-30 14:58:30 +04:00
|
|
|
log_message(&(g_cfg->log), LOG_LEVEL_INFO, "listening...");
|
2005-10-09 23:53:51 +04:00
|
|
|
g_sck = g_tcp_socket();
|
|
|
|
g_tcp_set_non_blocking(g_sck);
|
2008-07-30 14:58:30 +04:00
|
|
|
error = scp_tcp_bind(g_sck, g_cfg->listen_address, g_cfg->listen_port);
|
2005-10-09 23:53:51 +04:00
|
|
|
if (error == 0)
|
2005-01-30 07:34:19 +03:00
|
|
|
{
|
2005-10-09 23:53:51 +04:00
|
|
|
error = g_tcp_listen(g_sck);
|
2005-02-13 06:52:55 +03:00
|
|
|
if (error == 0)
|
2005-01-30 07:34:19 +03:00
|
|
|
{
|
2005-10-09 23:53:51 +04:00
|
|
|
in_sck = g_tcp_accept(g_sck);
|
|
|
|
while (in_sck == -1 && g_tcp_last_error_would_block(g_sck))
|
2005-01-30 07:34:19 +03:00
|
|
|
{
|
2005-10-09 23:53:51 +04:00
|
|
|
g_sleep(1000);
|
2005-03-13 04:33:59 +03:00
|
|
|
in_sck = g_tcp_accept(g_sck);
|
2005-10-09 23:53:51 +04:00
|
|
|
}
|
|
|
|
while (in_sck > 0)
|
|
|
|
{
|
2006-05-26 17:10:14 +04:00
|
|
|
/* we've got a connection, so we pass it to scp code */
|
2008-07-30 14:58:30 +04:00
|
|
|
LOG_DBG(&(g_cfg->log), "new connection");
|
2008-12-26 05:20:03 +03:00
|
|
|
g_thread_sck = in_sck;
|
2006-07-14 00:15:21 +04:00
|
|
|
//scp_process_start((void*)in_sck);
|
|
|
|
thread_scp_start(in_sck);
|
2006-05-26 17:10:14 +04:00
|
|
|
|
|
|
|
/* once we've processed the connection, we go back listening */
|
2005-10-09 23:53:51 +04:00
|
|
|
in_sck = g_tcp_accept(g_sck);
|
|
|
|
while (in_sck == -1 && g_tcp_last_error_would_block(g_sck))
|
|
|
|
{
|
|
|
|
g_sleep(1000);
|
2005-03-13 04:33:59 +03:00
|
|
|
in_sck = g_tcp_accept(g_sck);
|
2005-01-30 07:34:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-30 14:58:30 +04:00
|
|
|
log_message(&(g_cfg->log), LOG_LEVEL_ERROR, "listen error");
|
2005-01-30 07:34:19 +03:00
|
|
|
}
|
|
|
|
}
|
2005-10-09 23:53:51 +04:00
|
|
|
else
|
2005-01-30 07:34:19 +03:00
|
|
|
{
|
2008-07-30 14:58:30 +04:00
|
|
|
log_message(&(g_cfg->log), LOG_LEVEL_ERROR, "bind error");
|
2005-01-30 07:34:19 +03:00
|
|
|
}
|
2005-10-09 23:53:51 +04:00
|
|
|
g_tcp_close(g_sck);
|
2005-11-14 01:06:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
int DEFAULT_CC
|
|
|
|
main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int error;
|
2006-04-24 01:54:12 +04:00
|
|
|
int daemon = 1;
|
2005-11-14 01:06:49 +03:00
|
|
|
int pid;
|
|
|
|
char pid_s[8];
|
2006-04-24 01:54:12 +04:00
|
|
|
|
|
|
|
if (1 == argc)
|
2005-11-14 01:06:49 +03:00
|
|
|
{
|
|
|
|
/* no options on command line. normal startup */
|
|
|
|
g_printf("starting sesman...\n");
|
2006-04-24 01:54:12 +04:00
|
|
|
daemon = 1;
|
2005-11-14 01:06:49 +03:00
|
|
|
}
|
2006-04-24 01:54:12 +04:00
|
|
|
else if ((2 == argc) && ((0 == g_strcasecmp(argv[1], "--nodaemon")) ||
|
2008-12-26 05:20:03 +03:00
|
|
|
(0 == g_strcasecmp(argv[1], "-n")) ||
|
|
|
|
(0 == g_strcasecmp(argv[1], "-ns"))))
|
2005-11-14 01:06:49 +03:00
|
|
|
{
|
|
|
|
/* starts sesman not daemonized */
|
|
|
|
g_printf("starting sesman in foregroud...\n");
|
2006-08-24 09:58:10 +04:00
|
|
|
daemon = 0;
|
2005-11-14 01:06:49 +03:00
|
|
|
}
|
2006-04-24 01:54:12 +04:00
|
|
|
else if ((2 == argc) && ((0 == g_strcasecmp(argv[1], "--help")) ||
|
|
|
|
(0 == g_strcasecmp(argv[1], "-h"))))
|
2005-11-14 01:06:49 +03:00
|
|
|
{
|
|
|
|
/* help screen */
|
2005-11-16 15:27:08 +03:00
|
|
|
g_printf("sesman - xrdp session manager\n\n");
|
|
|
|
g_printf("usage: sesman [command]\n\n");
|
|
|
|
g_printf("command can be one of the following:\n");
|
2008-12-26 05:20:03 +03:00
|
|
|
g_printf("-n, -ns, --nodaemon starts sesman in foreground\n");
|
|
|
|
g_printf("-k, --kill kills running sesman\n");
|
|
|
|
g_printf("-h, --help shows this help\n");
|
2005-11-16 15:27:08 +03:00
|
|
|
g_printf("if no command is specified, sesman is started in background");
|
|
|
|
g_exit(0);
|
2005-11-14 01:06:49 +03:00
|
|
|
}
|
2006-04-24 01:54:12 +04:00
|
|
|
else if ((2 == argc) && ((0 == g_strcasecmp(argv[1], "--kill")) ||
|
|
|
|
(0 == g_strcasecmp(argv[1], "-k"))))
|
2005-11-14 01:06:49 +03:00
|
|
|
{
|
|
|
|
/* killing running sesman */
|
2005-11-16 15:27:08 +03:00
|
|
|
/* check if sesman is running */
|
|
|
|
if (!g_file_exist(SESMAN_PID_FILE))
|
|
|
|
{
|
2006-04-24 01:54:12 +04:00
|
|
|
g_printf("sesman is not running (pid file not found - %s)\n",
|
|
|
|
SESMAN_PID_FILE);
|
2005-11-16 15:27:08 +03:00
|
|
|
g_exit(1);
|
|
|
|
}
|
2006-04-24 01:54:12 +04:00
|
|
|
|
2005-11-14 01:06:49 +03:00
|
|
|
fd = g_file_open(SESMAN_PID_FILE);
|
|
|
|
|
|
|
|
if (-1 == fd)
|
|
|
|
{
|
2008-12-26 05:20:03 +03:00
|
|
|
g_printf("error opening pid file[%s]: %s\n", SESMAN_PID_FILE, g_get_strerror());
|
2005-11-14 01:06:49 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2006-04-24 01:54:12 +04:00
|
|
|
|
2005-11-14 01:06:49 +03:00
|
|
|
error = g_file_read(fd, pid_s, 7);
|
2007-03-25 18:57:08 +04:00
|
|
|
if (-1 == error)
|
2006-09-18 08:40:43 +04:00
|
|
|
{
|
|
|
|
g_printf("error reading pid file: %s\n", g_get_strerror());
|
2007-03-25 18:57:08 +04:00
|
|
|
g_file_close(fd);
|
|
|
|
g_exit(error);
|
2006-09-18 08:40:43 +04:00
|
|
|
}
|
|
|
|
g_file_close(fd);
|
2006-04-24 01:54:12 +04:00
|
|
|
pid = g_atoi(pid_s);
|
|
|
|
|
2005-11-14 01:06:49 +03:00
|
|
|
error = g_sigterm(pid);
|
|
|
|
if (0 != error)
|
|
|
|
{
|
2006-04-24 01:54:12 +04:00
|
|
|
g_printf("error killing sesman: %s\n", g_get_strerror());
|
2005-11-14 01:06:49 +03:00
|
|
|
}
|
2005-12-15 23:41:03 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
g_file_delete(SESMAN_PID_FILE);
|
|
|
|
}
|
2005-11-14 01:06:49 +03:00
|
|
|
|
|
|
|
g_exit(error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* there's something strange on the command line */
|
2005-11-16 15:27:08 +03:00
|
|
|
g_printf("sesman - xrdp session manager\n\n");
|
|
|
|
g_printf("error: invalid command line\n");
|
|
|
|
g_printf("usage: sesman [ --nodaemon | --kill | --help ]\n");
|
2005-11-14 01:06:49 +03:00
|
|
|
g_exit(1);
|
|
|
|
}
|
|
|
|
|
2005-11-16 15:27:08 +03:00
|
|
|
if (g_file_exist(SESMAN_PID_FILE))
|
|
|
|
{
|
|
|
|
g_printf("sesman is already running.\n");
|
|
|
|
g_printf("if it's not running, try removing ");
|
|
|
|
g_printf(SESMAN_PID_FILE);
|
|
|
|
g_printf("\n");
|
|
|
|
g_exit(1);
|
|
|
|
}
|
2006-04-24 01:54:12 +04:00
|
|
|
|
2005-11-14 01:06:49 +03:00
|
|
|
/* reading config */
|
2008-07-30 14:58:30 +04:00
|
|
|
g_cfg = g_malloc(sizeof(struct config_sesman), 1);
|
|
|
|
if (0 == g_cfg)
|
|
|
|
{
|
|
|
|
g_printf("error creating config: quitting.\n");
|
|
|
|
g_exit(1);
|
|
|
|
}
|
|
|
|
g_cfg->log.fd = -1; /* don't use logging before reading its config */
|
|
|
|
if (0 != config_read(g_cfg))
|
2005-11-14 01:06:49 +03:00
|
|
|
{
|
2006-04-24 01:54:12 +04:00
|
|
|
g_printf("error reading config: %s\nquitting.\n", g_get_strerror());
|
2005-11-14 01:06:49 +03:00
|
|
|
g_exit(1);
|
|
|
|
}
|
2006-04-24 01:54:12 +04:00
|
|
|
|
2005-11-14 01:06:49 +03:00
|
|
|
/* starting logging subsystem */
|
2008-07-30 14:58:30 +04:00
|
|
|
error = log_start(&(g_cfg->log));
|
2006-04-24 01:54:12 +04:00
|
|
|
|
2005-11-14 01:06:49 +03:00
|
|
|
if (error != LOG_STARTUP_OK)
|
|
|
|
{
|
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case LOG_ERROR_MALLOC:
|
|
|
|
g_printf("error on malloc. cannot start logging. quitting.\n");
|
2006-04-24 01:54:12 +04:00
|
|
|
break;
|
2005-11-14 01:06:49 +03:00
|
|
|
case LOG_ERROR_FILE_OPEN:
|
2008-12-26 05:20:03 +03:00
|
|
|
g_printf("error opening log file [%s]. quitting.\n", g_cfg->log.log_file);
|
2006-04-24 01:54:12 +04:00
|
|
|
break;
|
2005-11-14 01:06:49 +03:00
|
|
|
}
|
|
|
|
g_exit(1);
|
|
|
|
}
|
2006-08-24 09:58:10 +04:00
|
|
|
|
2007-05-07 01:02:25 +04:00
|
|
|
/* libscp initialization */
|
2008-07-30 14:58:30 +04:00
|
|
|
scp_init(&(g_cfg->log));
|
2007-05-07 01:02:25 +04:00
|
|
|
|
2005-11-14 01:06:49 +03:00
|
|
|
if (daemon)
|
|
|
|
{
|
|
|
|
/* start of daemonizing code */
|
|
|
|
g_pid = g_fork();
|
|
|
|
|
2006-08-24 09:58:10 +04:00
|
|
|
if (0 != g_pid)
|
2005-11-14 01:06:49 +03:00
|
|
|
{
|
|
|
|
g_exit(0);
|
|
|
|
}
|
2006-08-24 09:58:10 +04:00
|
|
|
|
2005-11-14 01:06:49 +03:00
|
|
|
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");
|
|
|
|
}
|
2006-07-14 00:15:21 +04:00
|
|
|
|
|
|
|
/* initializing locks */
|
|
|
|
lock_init();
|
2006-08-24 09:58:10 +04:00
|
|
|
|
2005-11-14 01:06:49 +03:00
|
|
|
/* signal handling */
|
|
|
|
g_pid = g_getpid();
|
2006-07-14 00:15:21 +04:00
|
|
|
/* old style signal handling is now managed synchronously by a
|
|
|
|
* separate thread. uncomment this block if you need old style
|
2008-12-28 09:11:14 +03:00
|
|
|
* signal handling and comment out thread_sighandler_start()
|
|
|
|
* going back to old style for the time being
|
|
|
|
* problem with the sigaddset functions in sig.c - jts */
|
|
|
|
#if 1
|
|
|
|
g_signal(1, sig_sesman_reload_cfg); /* SIGHUP */
|
|
|
|
g_signal(2, sig_sesman_shutdown); /* SIGINT */
|
|
|
|
g_signal(9, sig_sesman_shutdown); /* SIGKILL */
|
|
|
|
g_signal(15, sig_sesman_shutdown); /* SIGTERM */
|
|
|
|
g_signal_child_stop(sig_sesman_session_end); /* SIGCHLD */
|
|
|
|
#endif
|
|
|
|
#if 0
|
2006-07-14 00:15:21 +04:00
|
|
|
thread_sighandler_start();
|
2008-12-28 09:11:14 +03:00
|
|
|
#endif
|
2008-12-26 05:20:03 +03:00
|
|
|
if (daemon)
|
2005-11-14 01:06:49 +03:00
|
|
|
{
|
2008-12-26 05:20:03 +03:00
|
|
|
/* writing pid file */
|
|
|
|
fd = g_file_open(SESMAN_PID_FILE);
|
|
|
|
if (-1 == fd)
|
|
|
|
{
|
|
|
|
log_message(&(g_cfg->log), LOG_LEVEL_ERROR,
|
|
|
|
"error opening pid file[%s]: %s",
|
|
|
|
SESMAN_PID_FILE, g_get_strerror());
|
|
|
|
log_end(&(g_cfg->log));
|
|
|
|
g_exit(1);
|
|
|
|
}
|
|
|
|
g_sprintf(pid_s, "%d", g_pid);
|
|
|
|
g_file_write(fd, pid_s, g_strlen(pid_s) + 1);
|
|
|
|
g_file_close(fd);
|
2005-11-14 01:06:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* start program main loop */
|
2008-12-26 05:20:03 +03:00
|
|
|
log_message(&(g_cfg->log), LOG_LEVEL_ALWAYS,
|
|
|
|
"starting sesman with pid %d", g_pid);
|
2005-11-14 01:06:49 +03:00
|
|
|
|
2006-10-27 08:22:29 +04:00
|
|
|
/* make sure the /tmp/.X11-unix directory exist */
|
|
|
|
if (!g_directory_exist("/tmp/.X11-unix"))
|
|
|
|
{
|
|
|
|
g_create_dir("/tmp/.X11-unix");
|
2006-10-28 07:28:59 +04:00
|
|
|
g_chmod_hex("/tmp/.X11-unix", 0x1777);
|
2006-10-27 08:22:29 +04:00
|
|
|
}
|
|
|
|
|
2005-11-14 01:06:49 +03:00
|
|
|
sesman_main_loop();
|
|
|
|
|
2005-12-15 23:41:03 +03:00
|
|
|
if (!daemon)
|
|
|
|
{
|
2008-07-30 14:58:30 +04:00
|
|
|
log_end(&(g_cfg->log));
|
2005-12-15 23:41:03 +03:00
|
|
|
}
|
2005-11-14 01:06:49 +03:00
|
|
|
|
2005-01-30 07:34:19 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2005-11-14 01:06:49 +03:00
|
|
|
|