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."); }
static void exit_program(int val) { char buf[64]; debug(RPT_DEBUG, "%s(val=%d)", __FUNCTION__, val); /* TODO: These things shouldn't be so interdependent. The order * things are shut down in shouldn't matter... */ if (val > 0) { strncpy(buf, "Server shutting down on ", sizeof(buf)); switch(val) { case 1: strcat(buf, "SIGHUP"); break; case 2: strcat(buf, "SIGINT"); break; case 15: strcat(buf, "SIGTERM"); break; default: snprintf(buf, sizeof(buf), "Server shutting down on signal %d", val); break; /* Other values should not be seen, but just in case.. */ } report(RPT_NOTICE, buf); /* report it */ } /* Set emergency reporting and flush all messages if not done already. */ if (report_level == UNSET_INT) report_level = DEFAULT_REPORTLEVEL; if (report_dest == UNSET_INT) report_dest = DEFAULT_REPORTDEST; set_reporting("LCDd", report_level, report_dest); goodbye_screen(); /* display goodbye screen on LCD display */ drivers_unload_all(); /* release driver memory and file descriptors */ /* Shutdown things if server start was complete */ clients_shutdown(); /* shutdown clients (must come first) */ menuscreens_shutdown(); screenlist_shutdown(); /* shutdown screens (must come after client_shutdown) */ input_shutdown(); /* shutdown key input part */ sock_shutdown(); /* shutdown the sockets server */ report(RPT_INFO, "Exiting."); _exit(EXIT_SUCCESS); }
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; }
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; }