/*@-superuser@*/ int initializeSystem(int argc, char *argv[], struct InterfaceInfoList * ifs, struct ServerInfoList * servers, struct FdInfoList * fds) { struct ConfigInfo cfg; pid_t pid, pidfile_pid; int pidfile_fd; cfg.conffile_name = CFG_FILENAME; cfg.do_fork = true; parseCommandline(argc, argv, &cfg); /*@-boundswrite@*/ getConfig(cfg.conffile_name, &cfg); initFDs(fds, &cfg); /*@=boundswrite@*/ pidfile_fd = Eopen(cfg.pidfile_name, O_WRONLY|O_CREAT, 0444); openMsgfile(cfg.logfile_name); *ifs = cfg.interfaces; *servers = cfg.servers; Eclose(0); if (cfg.do_fork) pid = fork(); else pid = 0; pidfile_pid = 0; switch (pid) { case 0 : pidfile_pid = initializeDaemon(&cfg); break; case -1 : perror("fork()"); break; default : pidfile_pid = pid; break; } if (pidfile_pid!=0) { writeUInt(pidfile_fd, pidfile_pid); (void)write(pidfile_fd, "\n", 1); } freeLimitList(&cfg.ulimits); /* It is too late to handle an error here. So just ignore it... */ (void)close(pidfile_fd); return pid; }
int main(int argc, char *argv[]) { openlog ("SmartReflectServer", LOG_PID, 0); syslog(LOG_INFO, "Started SmartReflectServer"); prgmName = basename(argv[0]); int c = 0, port = WEBSOCKET_PORT; char *runDir = NULL; unsigned int sleepDivisor = DEFAULT_SLEEP_DIV; while ((c = getopt(argc, argv, "hDp:j:d:s:")) != -1) { switch (c) { case 'h': printHelp(); return EXIT_SUCCESS; case 'D': //calls mainDaemon to daemonize the magic-mirror process mainDaemon(); break; case 'p': port = strtol(optarg, NULL, 10); syslog(LOG_INFO, "Starting magic mirror on port: %d", port); break; case 'd': runDir = optarg; break; case 's': sleepDivisor = strtol(optarg, NULL, 10); default: break; } } char *filepath = NULL; if (optind < argc && optind > 0) { filepath = argv[optind]; } else { SYSLOG(LOG_ERR, "Main: No Plugin folder specified."); return EXIT_FAILURE; } //All code after this is in background! openlog("magic mirror", LOG_PID | LOG_CONS, LOG_USER); int prgmStatus = 0; char *runDirectory = realpath(runDir, NULL); do { unsigned int startTime = time(NULL); //initialize the daemon and all the plugins if (initializeDaemon(runDirectory, filepath, port)) { free(runDirectory); return EXIT_SUCCESS; } MainProgram_BootSeconds = (time(NULL) - startTime) + 1; SYSLOG(LOG_INFO, "Boot Time: %d", MainProgram_BootSeconds); prgmStatus = daemonProcess(sleepDivisor); //clean up... API_ShutdownPlugins(); PluginSocket_Cleanup(); Display_Cleanup(); PluginList_Free(); } while (API_Reboot()); SYSLOG(LOG_INFO, "Main: hard shutdown"); free(runDirectory); closelog(); if (prgmStatus) return EXIT_FAILURE; return EXIT_SUCCESS; }