Exemple #1
0
static void
do_reload(void)
{
	int e = 0;

	drivers_unload_all();		/* Close all drivers */

	config_clear();
	clear_settings();

	/* Reread command line*/
	CHAIN(e, process_command_line(stored_argc, stored_argv));

	/* Reread config file */
	if (strcmp(configfile, UNSET_STR)==0)
		strncpy(configfile, DEFAULT_CONFIGFILE, sizeof(configfile));
	CHAIN(e, process_configfile(configfile));

	/* Set default values */
	CHAIN(e, (set_default_settings(), 0));

	/* Set reporting values */
	CHAIN(e, set_reporting("LCDd", report_level, report_dest));
	CHAIN(e, (report(RPT_INFO, "Set report level to %d, output to %s", report_level,
			((report_dest == RPT_DEST_SYSLOG) ? "syslog" : "stderr")), 0));

	/* And restart the drivers */
	CHAIN(e, init_drivers());
	CHAIN_END(e, "Critical error while reloading, abort.");
}
Exemple #2
0
void check_sig(int signr)
{
    int nr;
    char buffer[MAX_LOGMESSAGE_SIZE];

    switch (signr) {
    case SIGINT:
        LOG(0, "Caught SIGINT - Good bye\n");
        exit(EXIT_SUCCESS);
        break;

    case SIGTERM:
        LOG(0, "Caught Termination Signal - Astalavista... baby\n");
        exit(EXIT_SUCCESS);
        break;

    case SIGHUP:
        LOG(0, "Caught SIGHUP - reloading configuration\n");
        sighup_detected = TRUE;

        process_configfile(config_file);
        if (check_needed_config_options() != 0) {
            LOG(0, "There is an error in the config! Exiting...\n");
            exit(EXIT_FAILURE);
        }
        prepare_vars();
        LOG(0, "Configuration reload succesfull.\n");

        break;

    default:
        snprintf(buffer, sizeof(buffer - 1),
                 "Caught the Signal '%d' but don't care about this.\n",
                 signr);
        LOG(2, buffer);
        break;
    }
}
Exemple #3
0
int
main(int argc, char **argv)
{
	int cfgresult;
	int c;

	/* set locale for cwdate & time formatting in chrono.c */
	setlocale(LC_TIME, "");

	/* get uname information */
	if (uname(&unamebuf) == -1) {
		perror("uname");
		return (EXIT_FAILURE);
	}

	/* setup error handlers */
	signal(SIGINT, exit_program);	/* Ctrl-C */
	signal(SIGTERM, exit_program);	/* "regular" kill */
	signal(SIGHUP, exit_program);	/* kill -HUP */
	signal(SIGPIPE, exit_program);	/* write to closed socket */
	signal(SIGKILL, exit_program);	/* kill -9 [cannot be trapped; but ...] */

	/* No error output from getopt */
	opterr = 0;

	/* get options from command line */
	while ((c = getopt(argc, argv, "s:p:e:c:fhv")) > 0) {
		char *end;

		switch (c) {
			/* c is for config file */
			case 'c':
				configfile = optarg;
				break;
			/* s is for server */
			case 's':
				server = optarg;
				break;
			/* p is for port */
			case 'p':
				port = strtol(optarg, &end, 0);
				if ((*optarg == '\0') || (*end != '\0') ||
				    (port <= 0) || (port >= 0xFFFF)) {
					fprintf(stderr, "Illegal port value %s\n", optarg);
					exit(EXIT_FAILURE);
				}
				break;
			case 'e':
				islow = strtol(optarg, &end, 0);
				if ((*optarg == '\0') || (*end != '\0') || (islow < 0)) {
					fprintf(stderr, "Illegal delay value %s\n", optarg);
					exit(EXIT_FAILURE);
				}
				break;
			case 'f':
				foreground = TRUE;
				break;
			case 'h':
				HelpScreen(EXIT_SUCCESS);
				break;
			case 'v':
				fprintf(stderr, "LCDproc %s\n", version);
				exit(EXIT_SUCCESS);
				break;
			/* otherwise...  Get help! */
			case '?':	/* unknown option or missing argument */
				/* FALLTHROUGH */
			default:
				HelpScreen(EXIT_FAILURE);
				break;
		}
	}

	/* Read config file */
	cfgresult = process_configfile(configfile);
	if (cfgresult < 0) {
		fprintf(stderr, "Error reading config file\n");
		exit(EXIT_FAILURE);
	}

	/* Set default reporting options */
	if (report_dest == UNSET_INT)
		report_dest = DEFAULT_REPORTDEST;
	if (report_level == UNSET_INT)
		report_level = DEFAULT_REPORTLEVEL;

	/* Set reporting settings */
	set_reporting("lcdproc", report_level, report_dest);

	/* parse non-option arguments: modes to add/delete */
	if (argc > max(optind, 1)) {
		int i;

		/*
		 * if no config file was read, ignore hard coded default
		 * modes
		 */
		if (cfgresult == 0)
			clear_modes();

		/* turn additional options on or off (using ! as prefix) */
		for (i = max(optind, 1); i < argc; i++) {
			int state = (*argv[i] == '!') ? 0 : 1;
			char *name = (state) ? argv[i] : argv[i] + 1;
			int shortname = (strlen(name) == 1) ? name[0] : '\0';
			int found = set_mode(shortname, name, state);

			if (!found) {
				fprintf(stderr, "Invalid Screen: %s\n", name);
				return (EXIT_FAILURE);
			}
		}
	}

	if (server == NULL)
		server = DEFAULT_SERVER;

	/* Connect to the server... */
	sock = sock_connect(server, port);
	if (sock < 0) {
		fprintf(stderr, "Error connecting to LCD server %s on port %d.\n"
			"Check to see that the server is running and operating normally.\n",
			server, port);
		return (EXIT_FAILURE);
	}

	sock_send_string(sock, "hello\n");
	usleep(500000);		/* wait for the server to say hi. */

	/* We grab the real values below, from the "connect" line. */
	lcd_wid = 20;
	lcd_hgt = 4;
	lcd_cellwid = 5;
	lcd_cellhgt = 8;

	if (foreground != TRUE) {
		if (daemon(1, 0) != 0) {
			fprintf(stderr, "Error: daemonize failed\n");
			return (EXIT_FAILURE);
		}

		if (pidfile != NULL) {
			FILE *pidf = fopen(pidfile, "w");

			if (pidf) {
				fprintf(pidf, "%d\n", (int)getpid());
				fclose(pidf);
				pidfile_written = TRUE;
			}
			else {
				fprintf(stderr, "Error creating pidfile %s: %s\n",
					pidfile, strerror(errno));
				return (EXIT_FAILURE);
			}
		}
	}

	/* Init the status gatherers... */
	mode_init();

	/* And spew stuff! */
	main_loop();
	exit_program(EXIT_SUCCESS);

	/* NOTREACHED */
	return EXIT_SUCCESS;
}
Exemple #4
0
int
main(int argc, char **argv)
{
	int e = 0;
	pid_t parent_pid = 0;

	stored_argc = argc;
	stored_argv = argv;

	/*
	 * Settings in order of preference:
	 *
	 * 1: Settings specified in command line options...
	 * 2: Settings specified in configuration file...
	 * 3: Default settings
	 *
	 * Because of this, and because one option (-c) specifies where
	 * the configuration file is, things are done in this order:
	 *
	 * 1. Read and set options.
	 * 2. Read configuration file; if option is read in configuration
	 *    file and not already set, then set it.
	 * 3. Having read configuration file, if parameter is not set,
	 *    set it to the default value.
	 *
	 * It is for this reason that the default values are **NOT** set
	 * in the variable declaration...
	 */

	/* Report that server is starting (report will be delayed) */
	report(RPT_NOTICE, "LCDd version %s starting", version);
	report(RPT_INFO, "Built on %s, protocol version %s, API version %s",
		build_date, protocol_version, api_version);

	clear_settings();

	/* Read command line*/
	CHAIN(e, process_command_line(argc, argv));

	/* Read config file
	 * If config file was not given on command line use default */
	if (strcmp(configfile, UNSET_STR) == 0)
		strncpy(configfile, DEFAULT_CONFIGFILE, sizeof(configfile));
	CHAIN(e, process_configfile(configfile));

	/* Set default values*/
	set_default_settings();

	/* Set reporting settings (will also flush delayed reports) */
	set_reporting("LCDd", report_level, report_dest);
	report(RPT_INFO, "Set report level to %d, output to %s", report_level,
			((report_dest == RPT_DEST_SYSLOG) ? "syslog" : "stderr"));
	CHAIN_END(e, "Critical error while processing settings, abort.");

	/* Now, go into daemon mode (if we should)...
	 * We wait for the child to report it is running OK. This mechanism
	 * is used because forking after starting the drivers causes the
	 * child to loose the (LPT) port access. */
	if (!foreground_mode) {
		report(RPT_INFO, "Server forking to background");
		CHAIN(e, parent_pid = daemonize());
	} else {
		output_GPL_notice();
		report(RPT_INFO, "Server running in foreground");
	}
	install_signal_handlers(!foreground_mode);
		/* Only catch SIGHUP if not in foreground mode */

	/* Startup the subparts of the server */
	CHAIN(e, sock_init(bind_addr, bind_port));
	CHAIN(e, screenlist_init());
	CHAIN(e, init_drivers());
	CHAIN(e, clients_init());
	CHAIN(e, input_init());
	CHAIN(e, menuscreens_init());
	CHAIN(e, server_screen_init());
	CHAIN_END(e, "Critical error while initializing, abort.");
	if (!foreground_mode) {
		/* Tell to parent that startup went OK. */
		wave_to_parent(parent_pid);
	}
	drop_privs(user); /* This can't be done before, because sending a
			signal to a process of a different user will fail */

	do_mainloop();
	/* This loop never stops; we'll get out only with a signal...*/

	return 0;
}
Exemple #5
0
int main(int argc, char **argv) {

	int i = 0;
	int filecounter = 0, pthread_ret = 0;
	double load;
	char buffer[MAX_LOGMESSAGE_SIZE];

	FILE *fppid = NULL;

	struct dirent **namelist;

	load = 0.0;

	if (process_arguments(argc, argv) == EXIT_FAILURE)
		exit(EXIT_FAILURE);

	process_configfile(config_file);

	if (loglevel == -1) {
		printf("DEBUG: Config File = %s\n", config_file);
		printf("CONFIG_OPT_LOGTYPE = %s\n", macro_x[CONFIG_OPT_LOGTYPE]);
		printf("CONFIG_OPT_LOGFILE = %s\n", macro_x[CONFIG_OPT_LOGFILE]);
		printf("CONFIG_OPT_LOGFILESIZE = %s\n", macro_x[CONFIG_OPT_LOGFILESIZE]);
		printf("CONFIG_OPT_LOGLEVEL = %s\n", macro_x[CONFIG_OPT_LOGLEVEL]);
		printf("CONFIG_OPT_SCANDIR = %s\n", macro_x[CONFIG_OPT_SCANDIR]);
		printf("CONFIG_OPT_RUNCMD = %s\n", macro_x[CONFIG_OPT_RUNCMD]);
		printf("CONFIG_OPT_RUNCMD_ARG = %s\n", macro_x[CONFIG_OPT_RUNCMD_ARG]);
		printf("CONFIG_OPT_MAXTHREADS = %s\n", macro_x[CONFIG_OPT_MAXTHREADS]);
		printf("CONFIG_OPT_LOAD = %s\n", macro_x[CONFIG_OPT_LOAD]);
		printf("CONFIG_OPT_USER = %s\n", macro_x[CONFIG_OPT_USER]);
		printf("CONFIG_OPT_GROUP = %s\n", macro_x[CONFIG_OPT_GROUP]);
		printf("CONFIG_OPT_PIDFILE = %s\n", macro_x[CONFIG_OPT_PIDFILE]);
		printf("CONFIG_OPT_SLEEPTIME = %s\n", macro_x[CONFIG_OPT_SLEEPTIME]);
		printf("CONFIG_OPT_IDENTMYSELF = %s\n", macro_x[CONFIG_OPT_IDENTMYSELF]);
		printf("---------------------------\n");
		if (check_needed_config_options() != 0) {
			printf("There is an Error! Exiting...\n");
			exit(EXIT_FAILURE);
		}
	}

	if (prepare_vars() != 0)
		exit(EXIT_FAILURE);
	if (loglevel == -1)
		printf("DEBUG: load_threshold is %s - ('%f')\n",
				use_load_threshold ? "enabled" : "disabled", load_threshold);

	pthread_t th[max_threads];
	for (i=0;i<max_threads;i++){
		th[i] = (pthread_t) NULL;
	}
	i = 0;

	/* Nice point for another function to set
	 * the internal vars from macro_x[] */

	/* Start in Daemon Mode or in foreground? */
	if (daemon_mode == TRUE)
		start_daemon("NPCD", LOG_LOCAL0);

	else if (use_syslog)
		openlog("NPCD", LOG_PID | LOG_CONS | LOG_NDELAY, LOG_LOCAL0);

	/* Create PID File or exit on failure */
	if (daemon_mode == TRUE && sighup_detected == FALSE) {
		fppid = fopen(pidfile, "w");

		if (fppid == NULL) {
			printf("Could not open pidfile '%s': %s\n", pidfile,
					strerror(errno));
			exit(EXIT_FAILURE);
		} else {
			fprintf(fppid, "%d", getpid());
			fclose(fppid);
		}
	}

	/* Try to drop the privileges */
	if (drop_privileges(user, group) == EXIT_FAILURE)
		exit(EXIT_FAILURE);

	snprintf(buffer, sizeof(buffer) - 1,
			"%s Daemon (%s) started with PID=%d\n", progname, PACKAGE_VERSION,
			getpid());
	LOG(0, buffer);
	snprintf(buffer, sizeof(buffer) - 1,
			"Please have a look at '%s -V' to get license information\n",
			progname);
	LOG(0, buffer);

	//sigemptyset();
	handle_signal(SIGINT, check_sig);
	handle_signal(SIGHUP, check_sig);
	handle_signal(SIGTERM, check_sig);

	snprintf(buffer, sizeof(buffer) - 1,
			"HINT: load_threshold is %s - ('%f')\n",
			use_load_threshold ? "enabled" : "disabled", load_threshold);
	LOG(0, buffer);

	/* beginn main loop */
	while (1) {

		if (chdir(directory) != 0)
			exit(EXIT_FAILURE);

		/* is_file() filter may cause trouble on some systems
		 * like Solaris or HP-UX that don't have a d-type
		 * member in struct dirent
		 */
		/* #ifdef HAVE_STRUCT_DIRENT_D_TYPE
		 if ( ( filecounter = scandir( directory, &namelist, is_file, alphasort ) ) < 0 ) {
		 #else */
		if ((filecounter = scandir(directory, &namelist, 0, alphasort)) < 0) {
			/* #endif */
			snprintf(buffer, sizeof(buffer) - 1,
					"Error while get file list from spooldir (%s) - %s\n",
					directory, strerror(errno));
			LOG(0, buffer);
			snprintf(buffer, sizeof(buffer) - 1, "Exiting...\n");
			LOG(0, buffer);

			if (daemon_mode != TRUE)
				printf("Error while get file list from spooldir (%s) - %s\n",
						directory, strerror(errno));
			break;
		}

		snprintf(buffer, sizeof(buffer) - 1, "Found %d files in %s\n",
				filecounter, directory);
		LOG(2, buffer);

		for (i = 0, namelist; i < filecounter; i++) {

#ifdef HAVE_GETLOADAVG
			if (use_load_threshold == TRUE) {
				load = getload(1);
				snprintf(buffer, sizeof(buffer) - 1, "DEBUG: load %f/%f\n",
						load, load_threshold);
				LOG(2, buffer);
			}

			if (use_load_threshold && (load > load_threshold)) {

				snprintf(buffer, sizeof(buffer) - 1,
						"WARN: MAX load reached: load %f/%f at i=%d", load,
						load_threshold, i);
				LOG(0, buffer);

				if (i > 0)
					i--;
				sleep(sleeptime);
				continue;
			}
#endif

			snprintf(buffer, sizeof(buffer) - 1,
					"ThreadCounter %d/%d File is %s\n", thread_counter,
					max_threads, namelist[i]->d_name);
			LOG(2, buffer);

			struct stat attribute;

			if (stat(namelist[i]->d_name, &attribute) == -1) {
				LOG(0, "Error while getting file status");
				break;
			}

			if (strstr((namelist[i]->d_name), "-PID-") != NULL) {
				snprintf(
						buffer,
						sizeof(buffer) - 1,
						"File '%s' is an already in process PNP file. Leaving it untouched.\n",
						namelist[i]->d_name);

				LOG(1, buffer);
				continue;
			}

			if (S_ISREG(attribute.st_mode)) {
				snprintf(buffer, sizeof(buffer) - 1, "Regular File: %s\n",
						namelist[i]->d_name);
				LOG(2, buffer);

				/* only start new threads if the max_thread config option is not reached */
				if (thread_counter < max_threads && we_should_stop == FALSE) {

					if ((pthread_ret = pthread_create(&th[thread_counter],
							NULL, processfile, namelist[i]->d_name)) != 0) {
						snprintf(buffer, sizeof(buffer) - 1,
							"Could not create thread... exiting with error '%s'\n", strerror(errno));
						LOG(0, buffer);
						exit(EXIT_FAILURE);
					}

					snprintf(buffer, sizeof(buffer) - 1,
							"A thread was started on thread_counter = %d\n",
							thread_counter);
					LOG(2, buffer);

					thread_counter++;

				}

				else if (we_should_stop == TRUE)
					break;

				else {

					snprintf(
							buffer,
							sizeof(buffer) - 1,
							"WARN: MAX Thread reached: %s comes later with ThreadCounter: %d\n",
							namelist[i]->d_name, thread_counter);
					LOG(2, buffer);

					i--;

					for (thread_counter = thread_counter; thread_counter > 0; thread_counter--) {
						snprintf(buffer, sizeof(buffer) - 1,
								"DEBUG: Will wait for th['%d']\n",
								thread_counter - 1);
						LOG(2, buffer);
						pthread_join(th[thread_counter - 1], NULL);
					}
				}
			}
		}

		if (thread_counter > 0) {
			/* Wait for open threads before working on the next run */
			snprintf(buffer, sizeof(buffer) - 1,
					"Have to wait: Filecounter = %d - thread_counter = %d\n",
					filecounter - 2, thread_counter);
			LOG(2, buffer);

			for (thread_counter = thread_counter; thread_counter > 0; thread_counter--)
				pthread_join(th[thread_counter - 1], NULL);
		}

		if (we_should_stop == TRUE)
			break;

		for (i = 0, namelist; i < filecounter; i++) {
			free(namelist[i]);
		}

		free(namelist);

		snprintf(buffer, sizeof(buffer) - 1,
				"No more files to process... waiting for %d seconds\n",
				sleeptime);
		LOG(1, buffer);

		sleep(sleeptime);

	}

	snprintf(buffer, sizeof(buffer) - 1, "Daemon ended. PID was '%d'\n",
			getpid());
	LOG(0, buffer);

	if (use_syslog)
		closelog();
	return EXIT_SUCCESS;
}
Exemple #6
0
int main(int argc, char *argv[])
{
    SDL_Event event;

    atexit(main_exit);

    process_configfile();
    process_options(argc, argv);

    // use g_nports if specified, otherwise use the number of port arguments.
    // if neither is given, create just one port
    int nportargs = argc - optind;
    g_nports = max(1, g_nports ? : nportargs);

    // now that we know the actual number of ports, repeat the last color/scale/height value
    // as often as necessary
    if (g_colors) {
        g_colors = (Uint32*)realloc(g_colors, g_nports * sizeof(Uint32));
        for (int n = ncolors; n < g_nports; ++n) {
            g_colors[n] = g_colors[ncolors - 1];
        }
    }

    if (g_scales) {
        g_scales = (float*)realloc(g_scales, g_nports * sizeof(float));
        for (int n = nscales; n < g_nports; ++n) {
            g_scales[n] = g_scales[nscales - 1];
        }
    }

    if (g_heights) {
        g_heights = (int*)realloc(g_heights, g_nports * sizeof(int));
        for (int n = nheights; n < g_nports; ++n) {
            g_heights[n] = g_heights[nheights - 1];
        }
        // g_heights overrides g_height
        g_height = 0;
        for (int n = 0; n < g_nports; ++n) {
            g_height += g_heights[n];
        }
        g_total_height = g_height;
    }


    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        fprintf(stderr, "can't init SDL: %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }
    atexit(SDL_Quit);

    audio_init(g_client_name, (const char * const *)&argv[optind]);

    video_init();
    SDL_WM_SetCaption(audio_get_client_name(), NULL);

    waves_init();

    g_run = true;

    while (g_run)
    {
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
                case SDL_VIDEORESIZE:
                    video_resize(event.resize.w, event.resize.h);
                    audio_adjust();
                    waves_adjust();
                    break;
                case SDL_QUIT:
                    g_run = false;
                    break;
            }
        }

        waves_draw();
        video_flip();
    }

    return 0;
}