Example #1
0
int g_mk_temp_dir(const char *app_name)
{
	if (app_name != 0)
	{
		if (app_name[0] != 0)
		{
			if (!g_directory_exist("/tmp/.xrdp"))
			{
				if (!g_create_dir("/tmp/.xrdp"))
				{
					printf("g_mk_temp_dir: g_create_dir failed\n");
					return 1;
				}

				g_chmod_hex("/tmp/.xrdp", 0x1777);
			}

			snprintf(g_temp_base, sizeof(g_temp_base), "/tmp/.xrdp/%s-XXXXXX", app_name);
			snprintf(g_temp_base_org, sizeof(g_temp_base_org), "/tmp/.xrdp/%s-XXXXXX", app_name);

			if (mkdtemp(g_temp_base) == 0)
			{
				printf("g_mk_temp_dir: mkdtemp failed [%s]\n", g_temp_base);
				return 1;
			}
		}
		else
		{
			printf("g_mk_temp_dir: bad app name\n");
			return 1;
		}
	}
	else
	{
		if (g_temp_base_org[0] == 0)
		{
			printf("g_mk_temp_dir: g_temp_base_org not set\n");
			return 1;
		}

		g_strncpy(g_temp_base, g_temp_base_org, 127);

		if (mkdtemp(g_temp_base) == 0)
		{
			printf("g_mk_temp_dir: mkdtemp failed [%s]\n", g_temp_base);
		}
	}

	return 0;
}
Example #2
0
/* will try to create directories up to last / in name
 example /tmp/a/b/c/readme.txt will try to create /tmp/a/b/c
 returns boolean */
int g_create_path(const char *path)
{
	char *pp;
	char *sp;
	char *copypath;
	int status;

	status = 1;
	copypath = g_strdup(path);
	pp = copypath;
	sp = strchr(pp, '/');

	while (sp != 0)
	{
		if (sp != pp)
		{
			*sp = 0;

			if (!g_directory_exist(copypath))
			{
				if (!g_create_dir(copypath))
				{
					status = 0;
					break;
				}
			}

			*sp = '/';
		}

		pp = sp + 1;
		sp = strchr(pp, '/');
	}

	free(copypath);
	return status;
}
Example #3
0
int DEFAULT_CC
main(int argc, char** argv)
{
	int fd;
	int error;
	int daemon = 1;
	int pid;
	char pid_s[8];
	char text[256];

	if(g_is_root() != 0){
		g_printf("Error, xrdp-sesman service must be start with root privilege\n");
		return 0;
	}


	g_snprintf(pid_file, 255, "%s/xrdp-sesman.pid", XRDP_PID_PATH);
	if (1 == argc)
	{
		/* no options on command line. normal startup */
		g_printf("starting sesman...");
		daemon = 1;
	}
	else if ((2 == argc) && ((0 == g_strcasecmp(argv[1], "--nodaemon")) ||
			(0 == g_strcasecmp(argv[1], "-n")) ||
			(0 == g_strcasecmp(argv[1], "-ns"))))
	{
		/* starts sesman not daemonized */
		g_printf("starting sesman in foregroud...");
		daemon = 0;
	}
	else if ((2 == argc) && ((0 == g_strcasecmp(argv[1], "--help")) ||
			(0 == g_strcasecmp(argv[1], "-h"))))
	{
		/* help screen */
		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");
		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");
		g_printf("if no command is specified, sesman is started in background");
		g_exit(0);
	}
	else if ((2 == argc) && ((0 == g_strcasecmp(argv[1], "--kill")) ||
			(0 == g_strcasecmp(argv[1], "-k"))))
	{
		/* killing running sesman */
		/* check if sesman is running */
		if (!g_file_exist(pid_file))
		{
			g_printf("sesman is not running (pid file not found - %s)\n", pid_file);
			g_exit(1);
		}

		fd = g_file_open(pid_file);

		if (-1 == fd)
		{
			g_printf("error opening pid file[%s]: %s\n", pid_file, g_get_strerror());
			return 1;
		}

		error = g_file_read(fd, pid_s, 7);
		if (-1 == error)
		{
			g_printf("error reading pid file: %s\n", g_get_strerror());
			g_file_close(fd);
			g_exit(error);
		}
		g_file_close(fd);
		pid = g_atoi(pid_s);

		error = g_sigterm(pid);
		if (0 != error)
		{
			g_printf("error killing sesman: %s\n", g_get_strerror());
		}
		else
		{
			g_file_delete(pid_file);
		}

		g_exit(error);
	}
	else
	{
		/* there's something strange on the command line */
		g_printf("sesman - xrdp session manager\n\n");
		g_printf("error: invalid command line\n");
		g_printf("usage: sesman [ --nodaemon | --kill | --help ]\n");
		g_exit(1);
	}

	if (g_file_exist(pid_file))
	{
		g_printf("sesman is already running.\n");
		g_printf("if it's not running, try removing ");
		g_printf(pid_file);
		g_printf("\n");
		g_exit(1);
	}

	/* reading config */
	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))
	{
		g_printf("error reading config: %s\nquitting.\n", g_get_strerror());
		g_exit(1);
	}

	/* starting logging subsystem */
	error = log_start(&(g_cfg->log));

	if (error != LOG_STARTUP_OK)
	{
		switch (error)
		{
			case LOG_ERROR_MALLOC:
				g_printf("error on malloc. cannot start logging. quitting.\n");
				break;
			case LOG_ERROR_FILE_OPEN:
				g_printf("error opening log file [%s]. quitting.\n", g_cfg->log.log_file);
				break;
		}
		g_exit(1);
	}

	/* libscp initialization */
	scp_init(&(g_cfg->log));

	if (daemon)
	{
		/* start of daemonizing code */
		if (g_daemonize(pid_file) == 0)
		{
			g_writeln("problem daemonize");
			g_exit(1);
		}
	}

	/* initializing locks */
	lock_init();

	/* signal handling */
	g_pid = g_getpid();
	/* old style signal handling is now managed synchronously by a
	 * separate thread. uncomment this block if you need old style
	 * 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_hang_up(sig_sesman_reload_cfg); /* SIGHUP	*/
	g_signal_user_interrupt(sig_sesman_shutdown); /* SIGINT	*/
	g_signal_kill(sig_sesman_shutdown); /* SIGKILL */
	g_signal_terminate(sig_sesman_shutdown); /* SIGTERM */
//	g_signal_child_stop(sig_sesman_session_end); /* SIGCHLD */
#endif
#if 0
	thread_sighandler_start();
#endif

	/* start program main loop */
	log_message(&(g_cfg->log), LOG_LEVEL_INFO,
			"starting sesman with pid %d", g_pid);

	/* make sure the /tmp/.X11-unix directory exist */
	if (!g_directory_exist("/tmp/.X11-unix"))
	{
		g_create_dir("/tmp/.X11-unix");
		g_chmod_hex("/tmp/.X11-unix", 0x1777);
	}

	if (!g_directory_exist(XRDP_SOCKET_PATH))
	{
		g_create_dir(XRDP_SOCKET_PATH);
		g_chmod_hex(XRDP_SOCKET_PATH, 0x1777);
	}

	g_snprintf(text, 255, "xrdp_sesman_%8.8x_main_term", g_pid);
	g_term_event = g_create_wait_obj(text);
	g_snprintf(text, 255, "xrdp_sesman_%8.8x_main_sync", g_pid);
	g_sync_event = g_create_wait_obj(text);

	scp_init_mutex();
	tc_thread_create(admin_thread, 0);
	tc_thread_create(monit_thread, 0);
	sesman_main_loop();
	scp_remove_mutex();

	g_delete_wait_obj(g_term_event);
	g_delete_wait_obj(g_sync_event);

	if (!daemon)
	{
		log_end(&(g_cfg->log));
	}

	return 0;
}
Example #4
0
int DEFAULT_CC
main(int argc, char** argv)
{
	int fd = 0;
	int error = 0;
	int daemon = 1;
	int pid = 0;
	char pid_s[8] = {0};

	if(g_is_root() != 0)
	{
		g_printf("Error, xrdp-printerd service must be start with root privilege\n");
		return 0;
	}


	g_snprintf(pid_file, 255, "%s/xrdp-printerd.pid", XRDP_PID_PATH);
	if (1 == argc)
	{
		/* no options on command line. normal startup */
		g_printf("starting xrdp-printerd...");
		daemon = 1;
	}
	else if ((2 == argc) && ((0 == g_strcasecmp(argv[1], "--nodaemon")) ||
                           (0 == g_strcasecmp(argv[1], "-n")) ||
                           (0 == g_strcasecmp(argv[1], "-ns"))))
	{
		/* starts xrdp-printerd not daemonized */
		g_printf("starting xrdp-printerd in foregroud...");
		daemon = 0;
	}
	else if ((2 == argc) && ((0 == g_strcasecmp(argv[1], "--help")) ||
                           (0 == g_strcasecmp(argv[1], "-h"))))
	{
		/* help screen */
		g_printf("xrdp-printerd - xrdp printer manager\n\n");
		g_printf("usage: xrdp-printerd [command]\n\n");
		g_printf("command can be one of the following:\n");
		g_printf("-n, -ns, --nodaemon  starts xrdp-printerd in foreground\n");
		g_printf("-k, --kill           kills running xrdp-printerd\n");
		g_printf("-h, --help           shows this help\n");
		g_printf("if no command is specified, xrdp-printerd is started in background");
		g_exit(0);
	}
	else if ((2 == argc) && ((0 == g_strcasecmp(argv[1], "--kill")) || (0 == g_strcasecmp(argv[1], "-k"))))
	{
		/* killing running xrdp-printerd */
		/* check if xrdp-printerd is running */
		if (!g_file_exist(pid_file))
		{
			g_printf("xrdp-printerd is not running (pid file not found - %s)\n", pid_file);
			g_exit(1);
		}

		fd = g_file_open(pid_file);

		if (-1 == fd)
		{
			g_printf("error opening pid file[%s]: %s\n", pid_file, g_get_strerror());
			return 1;
		}

		error = g_file_read(fd, pid_s, 7);
		if (-1 == error)
		{
			g_printf("error reading pid file: %s\n", g_get_strerror());
			g_file_close(fd);
			g_exit(error);
		}
		g_file_close(fd);
		pid = g_atoi(pid_s);

		error = g_sigterm(pid);
		if (0 != error)
		{
			g_printf("error killing xrdp-printerd: %s\n", g_get_strerror());
		}
		else
		{
			g_file_delete(pid_file);
		}

		g_exit(error);
	}
	else
	{
		/* there's something strange on the command line */
		g_printf("xrdp-printerd - xrdp printer manager\n\n");
		g_printf("error: invalid command line\n");
		g_printf("usage: xrdp-printerd [ --nodaemon | --kill | --help ]\n");
		g_exit(1);
	}

	if (g_file_exist(pid_file))
	{
		g_printf("xrdp-printerd is already running.\n");
		g_printf("if it's not running, try removing ");
		g_printf(pid_file);
		g_printf("\n");
		g_exit(1);
	}

	if (printerd_init() != LOG_STARTUP_OK)
	{
		switch (error)
		{
		case LOG_ERROR_MALLOC:
			g_printf("error on malloc. cannot start logging. quitting.\n");
			break;
		case LOG_ERROR_FILE_OPEN:
			g_printf("error opening log file [%s]. quitting.\n", l_config->log_file);
			break;
		}
		g_exit(1);
	}

	if (daemon)
	{
		/* start of daemonizing code */
		if (g_daemonize(pid_file) == 0)
		{
			g_writeln("problem daemonize");
			g_exit(1);
		}
	}

	/* signal handling */
	g_pid = g_getpid();

	g_signal_user_interrupt(sig_printerd_shutdown);		/* SIGINT  */
	g_signal_kill(sig_printerd_shutdown);				/* SIGKILL */
	g_signal_terminate(sig_printerd_shutdown);			/* SIGTERM */
	g_signal_pipe(sig_printerd_pipe);			/* SIGPIPE */

	/* start program main loop */
	log_message(l_config, LOG_LEVEL_INFO,
              "starting xrdp-printerd with pid %d", g_pid);


	if (!g_directory_exist(XRDP_SOCKET_PATH))
	{
		g_create_dir(XRDP_SOCKET_PATH);
		g_chmod_hex(XRDP_SOCKET_PATH, 0x1777);
	}

	printerd_main_loop();


	log_end(l_config);

	return 0;
}