int main(int argc, char **argv) { FdEventHandlerPtr listener; int i; int rc; int expire = 0, printConfig = 0; initAtoms(); CONFIG_VARIABLE(daemonise, CONFIG_BOOLEAN, "Run as a daemon"); CONFIG_VARIABLE(pidFile, CONFIG_ATOM, "File with pid of running daemon."); preinitChunks(); preinitLog(); preinitObject(); preinitIo(); preinitDns(); preinitServer(); preinitHttp(); preinitDiskcache(); preinitLocal(); preinitForbidden(); preinitSocks(); preinitOffline(); i = 1; while(i < argc) { if(argv[i][0] != '-') break; if(strcmp(argv[i], "--") == 0) { i++; break; } else if(strcmp(argv[i], "-h") == 0) { usage(argv[0]); exit(0); } else if(strcmp(argv[i], "-v") == 0) { printConfig = 1; i++; } else if(strcmp(argv[i], "-x") == 0) { expire = 1; i++; } else if(strcmp(argv[i], "-c") == 0) { i++; if(i >= argc) { usage(argv[0]); exit(1); } if(configFile) releaseAtom(configFile); configFile = internAtom(argv[i]); i++; } else { usage(argv[0]); exit(1); } } if(configFile) configFile = expandTilde(configFile); if(configFile == NULL) { configFile = expandTilde(internAtom("~/.polipo")); if(configFile) if(access(configFile->string, F_OK) < 0) { releaseAtom(configFile); configFile = NULL; } } if(configFile == NULL) { if(access("/etc/polipo/config", F_OK) >= 0) configFile = internAtom("/etc/polipo/config"); if(configFile && access(configFile->string, F_OK) < 0) { releaseAtom(configFile); configFile = NULL; } } rc = parseConfigFile(configFile); if(rc < 0) exit(1); while(i < argc) { rc = parseConfigLine(argv[i], "command line", 0, 0); if(rc < 0) exit(1); i++; } initChunks(); initLog(); initObject(); if(!expire && !printConfig) initEvents(); initIo(); initDns(); initHttp(); initServer(); initDiskcache(); initForbidden(); initSocks(); initOffline(); if(printConfig) { printConfigVariables(stdout, 0); exit(0); } if(expire) { expireDiskObjects(); exit(0); } if(daemonise) do_daemonise(logFile == NULL || logFile->length == 0); if(pidFile) writePid(pidFile->string); listener = create_listener(proxyAddress->string, proxyPort, httpAccept, NULL); if(!listener) { if(pidFile) unlink(pidFile->string); exit(1); } eventLoop(); if(pidFile) unlink(pidFile->string); return 0; }
int main(int argc, char **argv) { int res; int opt; char *configfile; char *pidfile; configfile = strdup(CONFIGFILE); pidfile = strdup(PIDFILE); while ((opt = getopt(argc, argv, "vhf:p:")) != -1) { switch (opt) { case 'f': free(configfile); configfile = strdup(optarg); break; case 'p': free(pidfile); pidfile = strdup(optarg); break; case 'v': printf("%s: Version %s\n", argv[0], EKEYD_VERSION_S); return 0; case 'h': default: fprintf(stderr, usage, argv[0]); return 1; } } if (optind != argc) { fprintf(stderr, "Unexpected argument\n"); fprintf(stderr, usage, argv[0]); return 1; } if (lstate_init() == false) { return 1; } if (!lstate_runconfig(configfile)) { /* Failed to run the configuration */ return 1; } /* Everything is good, daemonise */ if (lstate_request_daemonise()) do_daemonise(pidfile, false); /* now we are a daemon, start system logging */ openlog("ekeyd", LOG_ODELAY, LOG_DAEMON); syslog(LOG_INFO, "Starting Entropy Key Daemon"); while (true) { res = ekeyfd_poll(-1); if (res == 0) break; /* no more fd open, finish */ if (res < 0) { if ((errno == EINTR) || (errno == EWOULDBLOCK)) continue; /* these errors are ok and the poll is retried */ syslog(LOG_ERR, "Unhandled error in poll %s, exiting", strerror(errno)); break; } if (lua_fd_ready) { lstate_controlbytes(); lua_fd_ready = false; } } lstate_finalise(); estream_close(output_stream); close_nonce(); unlink(pidfile); free(configfile); free(pidfile); syslog(LOG_INFO, "Entropy Key Daemon Stopping"); closelog(); return 0; }