int main(int argc, char *argv[]) { char *conffile = NULL; int request_kill = 0, show_message = 0; if (!read_parameters(argc, argv, &conffile, &request_kill, &show_message)) { print_usage(argv[0]); return 1; } /* read the config file; conffile = NULL means use the default. */ if (!read_config(conffile)) return 1; /* error reading the config file */ setup_defaults(); if (request_kill) { kill_server(); return 0; } if (show_message) { signal_message(); return 0; } setup_signals(); /* Init files and directories. * Start logging last, so that the log is only created if startup succeeds. */ if (!init_workdir() || !init_database() || !check_database() || !begin_logging()) return 1; if (!settings.nodaemon) { if (daemon(0, 0) == -1) { settings.nodaemon = TRUE; log_msg("could not detach from terminal: %s", strerror(errno)); return 1; } if (!create_pidfile()) return 1; } /* give this process and its children their own process group id for kill() */ setpgid(0, 0); report_startup(); runserver(); /* shutdown */ remove_pidfile(); end_logging(); close_database(); remove_unix_socket(); free_config(); return 0; }
int main(int argc,char** argv) { pcap_t *pt; char dev[10]; char errbuf[PCAP_ERRBUF_SIZE]; if(argc != 2){ printf("./url_recoder eth0\n"); exit(0); } daemon(0,0); create_pidfile(); log_init(); strncpy(dev,argv[1],strlen(dev)); dev[9] = 0; pt = pcap_open_live(dev, SNAPLEN, PROMISC, -1, errbuf); if (pt == NULL){ printf("open dev failed\n"); exit(0); } pcap_loop(pt, -1, callback,NULL); }
/* static */ int daemon::start(const char *cwd, const char *pidfile /* = NULL */, const char *logfile /* = NULL */) { int fdpid = -1; if (pidfile) { stored_pidfile = strdup(pidfile); if (NULL == stored_pidfile) LOGGER_ERROR_STR_EXIT(EXIT_FAILURE, "daemon::start, strdup"); if ((fdpid = create_pidfile(pidfile)) < 0) exit(EXIT_FAILURE); } pid_t pid = fork(); if (pid < 0) { logger::perror("daemon::start, fork"); exit(EXIT_FAILURE); } if (pid > 0) { logger::log("daemon started (pid=%d)", pid); exit(EXIT_SUCCESS); } umask(0); if (0 > setsid()) { logger::perror("setsid"); exit(EXIT_FAILURE); } if (NULL != cwd && 0 > chdir("/")) { logger::perror("chdir"); exit(EXIT_FAILURE); } reset_fds(); init_logfile(logfile); pid = getpid(); if (fdpid >= 0) { dprintf(fdpid, "%d", pid); ::close(fdpid); } logger::log("child process started (pid=%d)", pid); return 0; }
int pidfile(const char *path) { if (path == NULL || strchr(path, '/') == NULL) { char *default_path; if ((default_path = generate_varrun_path(path)) == NULL) return -1; if (create_pidfile(default_path) == -1) { free(default_path); return -1; } free(default_path); return 0; } else return create_pidfile(path); }
/* Create the PidFile if one is configured */ static void _init_pidfile(void) { if (slurmdbd_conf->pid_file == NULL) { error("No PidFile configured"); return; } /* Don't close the fd returned here since we need to keep the fd open to maintain the write lock. */ create_pidfile(slurmdbd_conf->pid_file, slurmdbd_conf->slurm_user_id); }
/* * detach - detach us from the controlling terminal. */ void detach(void) { if (detached) return; if (daemon(0, 0) < 0) { perror("Couldn't detach from controlling terminal"); die(1); } detached = 1; pid = getpid(); /* update pid file if it has been written already */ if (pidfilename[0]) create_pidfile(); }
int main(int argc,char** argv) { struct sockaddr_nl addr; struct nlmsghdr *nlh; char buffer[4096]; int sock,len; daemon(0,0); create_pidfile(); log_init(); if((sock = socket(AF_NETLINK,SOCK_RAW,NETLINK_ROUTE)) == -1){ printf("open NETLINK_ROUTE socket failed\n"); goto exit; } memset(&addr,0,sizeof(addr)); addr.nl_family = AF_NETLINK; addr.nl_groups = RTMGRP_LINK |RTMGRP_IPV4_IFADDR; if(bind(sock,(struct sockaddr*)&addr,sizeof(addr)) == -1){ printf("bind failed\n"); goto exit; } while((len = recv(sock,buffer,4096,0)) > 0){ nlh = (struct nlmsghdr*)buffer; while((NLMSG_OK(nlh,len)) && (nlh->nlmsg_type != NLMSG_DONE)){ if(nlh->nlmsg_type == RTM_NEWLINK){ parseBinaryNetLinkMessage(nlh); } nlh = NLMSG_NEXT(nlh,len); } } close(sock); exit: exit(0); }
int main(int argc, char** argv) { ini_sContext* cp; pwr_tStatus sts; set_valid_time(); cp = createContext(argc, argv); ver_WriteVersionInfo("ProviewR Runtime Environment"); if (cp->flags.b.restart) { sts = interactive(argc, argv, cp); } else if (cp->flags.b.stop) { sts = stop(cp); } else { // Now lets daemonize if asked to if (cp->flags.b.daemonize) { daemonize(); } sts = start(cp); // Now lets create the pid file before starting our endless event loop if (cp->flags.b.daemonize) { create_pidfile(); } sts = events(cp); errh_LogInfo(&cp->log, "Ich sterbe!!"); } exit(sts); }
int main(int argc, char **argv) { const char *ptr; int server_mode = -1; int i; int rc = 0; FILE *xmlf; #if !defined(HPUX) setlinebuf(stdout); #endif ptr = strrchr(argv[0], '/'); if (ptr == NULL) { strncpy(prog_name, argv[0], PROG_NAME_LEN); } else { strncpy(prog_name, ptr + 1, PROG_NAME_LEN); } pagesize = getpagesize(); printf("pagesize = %d\n", pagesize); cache_line_size = OPT_CACHE_LINE_EST; #ifdef TBB_KINFO int kinfo_fd = 0; #endif /* TBB_KINFO */ #ifdef TBB_KINFO if (options.kinfo) { kinfo_fd = pre_kinfo_init(); } #endif /* TBB_KINFO */ init_print_time(&tnow, &tprev); /* must happen before processing arguments */ options_set(); /* Process command line arguments */ process_args(argc, argv); /* Check for conflicts in the specified cmd line options */ check_options(); printf("INIT START\n"); num_idle = maxconns; printf("main: num_idle = %d\n", num_idle); printf("main: maxconns = %d\n", maxconns); printf("main: max_fds = %d\n", max_fds); #ifdef DEBUG_ON printf("Debugging is on debugging messages mask = 0x%x\n", MSG_MASK); #else printf("Compiled without debugging\n"); #endif /* DEBUG_ON */ if (options.use_socket_aio) { // for now leave some room for a few open files and // a few listening sockets (unfortunately we need to call this before // we know how many listening sockets there will be // max_sock_sd = maxconns + 20; // For now we just pick a value that _SHOULD_ avoid // collisions with future tests. // Note that if there were sd/fd collisions things // _should_ likely work just fine but this may // make finding problems easier - since there is // potential to control which range of values the // underlying aio layer is returning. #ifdef HAVE_AIO_LAYER /* These are just estimates !!! */ int aio_lowest_sock_sd = OPT_AIO_LOWEST_SD; int aio_highest_sock_sd = (maxconns + aio_lowest_sock_sd - 1) + OPT_AIO_MAX_LISTENERS; max_sock_sd = aio_highest_sock_sd; if (max_sock_sd > max_fds) { printf("main: max_sock_sd = %d > max_fds = %d\n", max_sock_sd, max_fds); exit(1); } printf("main: aio_lowest_sock_sd = %d\n", aio_lowest_sock_sd); printf("main: aio_highest_sock_sd = %d\n", aio_highest_sock_sd); PRINT_TIME(NOFD, &tnow, &tprev, "main: calling aio_sock_init with " "max_sock_sd = %d", max_sock_sd); rc = aio_sock_init(aio_lowest_sock_sd, aio_highest_sock_sd); if (rc < 0) { printf("main: aio_sock_init failed rc = %d\n", rc); exit(1); } #endif /* HAVE_AIO_LAYER */ } else { max_sock_sd = max_fds; } printf("main: max_sock_sd = %d\n", max_sock_sd); printf("main: FD_SETSIZE = %d\n", FD_SETSIZE); printf("main: sizeof(fd_set) = %d\n", (int) sizeof(fd_set)); #ifdef TBB_KINFO if (options.kinfo) { kinfo_fd = pre_kinfo_init(); } #endif /* TBB_KINFO */ rc = info_init(); info_listener_init(); /* must happen after processing arguments */ init_app_queues(); create_pidfile(); options_valid(); options_print(); xmlf = fopen("options.xml", "w"); if (!xmlf) { printf("main: failed to create options.xml\n"); exit(1); } options_print_xml(xmlf); fclose(xmlf); #ifndef AIO_WORKAROUND_BUGS print_extra_info(); #endif /* AIO_WORKAROUND_BUGS */ #ifdef HAVE_NETSTAT netstat_init(); #endif /* HAVE_NETSTAT */ sock_special_init(); /* initialize the event dispatch mechanism */ if (options.use_poll) { PRINT_TIME(NOFD, &tnow, &tprev, "userver: calling poll_loop_init"); poll_loop_init(); #ifdef HAVE_EPOLL } else if (options.use_epoll || options.use_epoll2) { PRINT_TIME(NOFD, &tnow, &tprev, "userver: calling epoll_loop_init"); epoll_loop_init(); #endif /* HAVE_EPOLL */ #ifdef HAVE_AIO_LAYER } else if (options.use_socket_aio) { PRINT_TIME(NOFD, &tnow, &tprev, "userver: calling aio_loop_init"); aio_loop_init(); #endif /* HAVE_AIO_LAYER */ } else if (!options.send_loop) { assert(options.send_loop == 0); PRINT_TIME(NOFD, &tnow, &tprev, "userver: calling select_loop_init"); select_loop_init(); } else { #ifdef SEND assert(options.send_loop == 1); PRINT_TIME(NOFD, &tnow, &tprev, "userver: calling send_loop_init"); send_loop_init(); #endif } switch (options.get_connections) { case OPT_CONN_WITH_SELECT_POLL_EPOLL: server_mode = LISTENER_NOT_ASYNC_INIT; break; #ifdef SEND case OPT_CONN_WITH_SEND_SELECT: server_mode = LISTENER_NOT_ASYNC_INIT | LISTENER_SEND_INIT; break; #endif /* SEND */ case OPT_CONN_WITH_SIGIO: server_mode = LISTENER_DO_ASYNC_INIT; break; case OPT_CONN_WITH_SEND_EVTS: server_mode = LISTENER_SEND_INIT; break; case OPT_CONN_WITH_AIO_ACCEPT: server_mode = LISTENER_AIO_INIT; break; default: printf("%s: options.get_connections = %d not handled\n", argv[0], options.get_connections); exit(1); break; } /* switch */ printf("Calling server_init with mode = 0x%x = %d\n", server_mode, server_mode); server_init(server_mode); for (i = sock_listener_min; i <= sock_listener_max; i++) { if (sock_is_listener(i)) { printf("listen_sd = %d\n", i); if (i > max_sd) { max_sd = i; } } } switch (options.process_sds_order) { case OPT_PROCESS_SDS_LRU: lru_copy_init(); break; case OPT_PROCESS_SDS_LIFO: q_init(max_fds + 1); if (options.get_connections == OPT_CONN_WITH_SELECT_POLL_EPOLL) { for (i = sock_listener_min; i <= sock_listener_max; i++) { if (sock_is_listener(i)) { q_add_to_front(i); } } } break; case OPT_PROCESS_SDS_FIFO: q_init(max_fds + 1); if (options.get_connections == OPT_CONN_WITH_SELECT_POLL_EPOLL) { for (i = sock_listener_min; i <= sock_listener_max; i++) { if (sock_is_listener(i)) { q_add_to_rear(i); } } } break; default: /* do nothing */ break; } if (options.caching_on) { initCache(options.cache_table_size, options.cache_max_bytes, options.cache_max_file_size, options.cache_max_load_factor, options.cache_lock_pages); #ifdef CACHE_MAPPED_NEW if (options.cache_warm) { cache_warm(options.cache_warm_file, (options.doc_root[0] != '\0') ? options.doc_root : NULL, options.cache_table_print); } #endif } rusage_init(); fork_servers(numprocs); return 0; }
/* the main program... */ int main(int argc, char *argv[]) { int i; sigset_t signalmask, oldmask; #ifdef HAVE_PTHREAD_TIMEDJOIN_NP struct timespec ts; #endif /* HAVE_PTHREAD_TIMEDJOIN_NP */ /* close all file descriptors (except stdin/out/err) */ i = sysconf(_SC_OPEN_MAX) - 1; /* if the system does not have OPEN_MAX just close the first 32 and hope we closed enough */ if (i < 0) i = 32; for (; i > 3; i--) close(i); /* parse the command line */ parse_cmdline(argc, argv); /* clean the environment */ #ifdef HAVE_CLEARENV if (clearenv() || putenv("HOME=/") || putenv("TMPDIR=/tmp") || putenv("LDAPNOINIT=1")) { log_log(LOG_ERR, "clearing environment failed"); exit(EXIT_FAILURE); } #else /* not HAVE_CLEARENV */ /* this is a bit ugly */ environ = sane_environment; #endif /* not HAVE_CLEARENV */ /* disable the nss_ldap module for this process */ disable_nss_ldap(); /* set LDAP log level */ if (myldap_set_debuglevel(nslcd_debugging) != LDAP_SUCCESS) exit(EXIT_FAILURE); /* read configuration file */ cfg_init(NSLCD_CONF_PATH); /* set default mode for pidfile and socket */ (void)umask((mode_t)0022); /* see if someone already locked the pidfile if --check option was given exit TRUE if daemon runs (pidfile locked), FALSE otherwise */ if (nslcd_checkonly) { if (is_locked(NSLCD_PIDFILE)) { log_log(LOG_DEBUG, "pidfile (%s) is locked", NSLCD_PIDFILE); exit(EXIT_SUCCESS); } else { log_log(LOG_DEBUG, "pidfile (%s) is not locked", NSLCD_PIDFILE); exit(EXIT_FAILURE); } } /* normal check for pidfile locked */ if (is_locked(NSLCD_PIDFILE)) { log_log(LOG_ERR, "daemon may already be active, cannot acquire lock (%s): %s", NSLCD_PIDFILE, strerror(errno)); exit(EXIT_FAILURE); } /* daemonize */ if ((!nslcd_debugging) && (!nslcd_nofork) && (daemon(0, 0) < 0)) { log_log(LOG_ERR, "unable to daemonize: %s", strerror(errno)); exit(EXIT_FAILURE); } /* intilialize logging */ if (!nslcd_debugging) log_startlogging(); log_log(LOG_INFO, "version %s starting", VERSION); /* start subprocess to do invalidating if reconnect_invalidate is set */ for (i = 0; i < LM_NONE; i++) if (nslcd_cfg->reconnect_invalidate[i]) break; if (i < LM_NONE) invalidator_start(); /* write pidfile */ create_pidfile(NSLCD_PIDFILE); /* install handler to close stuff off on exit and log notice */ if (atexit(exithandler)) { log_log(LOG_ERR, "atexit() failed: %s", strerror(errno)); exit(EXIT_FAILURE); } /* create socket */ nslcd_serversocket = create_socket(NSLCD_SOCKET); if ((nslcd_cfg->gid != NOGID) && (nslcd_cfg->uidname != NULL)) { #ifdef HAVE_INITGROUPS /* load supplementary groups */ if (initgroups(nslcd_cfg->uidname, nslcd_cfg->gid) < 0) log_log(LOG_WARNING, "cannot initgroups(\"%s\",%d) (ignored): %s", nslcd_cfg->uidname, (int)nslcd_cfg->gid, strerror(errno)); else log_log(LOG_DEBUG, "initgroups(\"%s\",%d) done", nslcd_cfg->uidname, (int)nslcd_cfg->gid); #else /* not HAVE_INITGROUPS */ #ifdef HAVE_SETGROUPS /* just drop all supplemental groups */ if (setgroups(0, NULL) < 0) log_log(LOG_WARNING, "cannot setgroups(0,NULL) (ignored): %s", strerror(errno)); else log_log(LOG_DEBUG, "setgroups(0,NULL) done"); #else /* not HAVE_SETGROUPS */ log_log(LOG_DEBUG, "neither initgroups() or setgroups() available"); #endif /* not HAVE_SETGROUPS */ #endif /* not HAVE_INITGROUPS */ } /* change to nslcd gid */ if (nslcd_cfg->gid != NOGID) { if (setgid(nslcd_cfg->gid) != 0) { log_log(LOG_ERR, "cannot setgid(%d): %s", (int)nslcd_cfg->gid, strerror(errno)); exit(EXIT_FAILURE); } log_log(LOG_DEBUG, "setgid(%d) done", (int)nslcd_cfg->gid); } /* change to nslcd uid */ if (nslcd_cfg->uid != NOUID) { if (setuid(nslcd_cfg->uid) != 0) { log_log(LOG_ERR, "cannot setuid(%d): %s", (int)nslcd_cfg->uid, strerror(errno)); exit(EXIT_FAILURE); } log_log(LOG_DEBUG, "setuid(%d) done", (int)nslcd_cfg->uid); } /* block all these signals so our worker threads won't handle them */ sigemptyset(&signalmask); sigaddset(&signalmask, SIGHUP); sigaddset(&signalmask, SIGINT); sigaddset(&signalmask, SIGQUIT); sigaddset(&signalmask, SIGABRT); sigaddset(&signalmask, SIGPIPE); sigaddset(&signalmask, SIGTERM); sigaddset(&signalmask, SIGUSR1); sigaddset(&signalmask, SIGUSR2); pthread_sigmask(SIG_BLOCK, &signalmask, &oldmask); /* start worker threads */ log_log(LOG_INFO, "accepting connections"); nslcd_threads = (pthread_t *)malloc(nslcd_cfg->threads * sizeof(pthread_t)); if (nslcd_threads == NULL) { log_log(LOG_CRIT, "main(): malloc() failed to allocate memory"); exit(EXIT_FAILURE); } for (i = 0; i < nslcd_cfg->threads; i++) { if (pthread_create(&nslcd_threads[i], NULL, worker, NULL)) { log_log(LOG_ERR, "unable to start worker thread %d: %s", i, strerror(errno)); exit(EXIT_FAILURE); } } pthread_sigmask(SIG_SETMASK, &oldmask, NULL); /* install signalhandlers for some signals */ install_sighandler(SIGHUP, sig_handler); install_sighandler(SIGINT, sig_handler); install_sighandler(SIGQUIT, sig_handler); install_sighandler(SIGABRT, sig_handler); install_sighandler(SIGPIPE, SIG_IGN); install_sighandler(SIGTERM, sig_handler); install_sighandler(SIGUSR1, sig_handler); install_sighandler(SIGUSR2, SIG_IGN); /* wait until we received a signal */ while ((nslcd_receivedsignal == 0) || (nslcd_receivedsignal == SIGUSR1)) { sleep(INT_MAX); /* sleep as long as we can or until we receive a signal */ if (nslcd_receivedsignal == SIGUSR1) { log_log(LOG_INFO, "caught signal %s (%d), refresh retries", signame(nslcd_receivedsignal), nslcd_receivedsignal); myldap_immediate_reconnect(); nslcd_receivedsignal = 0; } } /* print something about received signal */ log_log(LOG_INFO, "caught signal %s (%d), shutting down", signame(nslcd_receivedsignal), nslcd_receivedsignal); /* cancel all running threads */ for (i = 0; i < nslcd_cfg->threads; i++) if (pthread_cancel(nslcd_threads[i])) log_log(LOG_WARNING, "failed to stop thread %d (ignored): %s", i, strerror(errno)); /* close server socket to trigger failures in threads waiting on accept() */ close(nslcd_serversocket); nslcd_serversocket = -1; /* if we can, wait a few seconds for the threads to finish */ #ifdef HAVE_PTHREAD_TIMEDJOIN_NP ts.tv_sec = time(NULL) + 3; ts.tv_nsec = 0; #endif /* HAVE_PTHREAD_TIMEDJOIN_NP */ for (i = 0; i < nslcd_cfg->threads; i++) { #ifdef HAVE_PTHREAD_TIMEDJOIN_NP pthread_timedjoin_np(nslcd_threads[i], NULL, &ts); #endif /* HAVE_PTHREAD_TIMEDJOIN_NP */ if (pthread_kill(nslcd_threads[i], 0) == 0) log_log(LOG_ERR, "thread %d is still running, shutting down anyway", i); } /* we're done */ return EXIT_FAILURE; }
/*---------------------------------------------------------------------------*/ int main(int argc, char *const argv[]) { struct xio_session *session; char url[256]; int i; struct sigaction sa; int c; char *addr = NULL; char *port = NULL; char *trans = NULL; struct xio_session_params params; struct xio_connection_params cparams; while (1) { c = getopt_long(argc, argv, "a:p:r:hdnV", longopts, NULL); if (c == -1) break; switch (c) { case 'a': addr = optarg; break; case 'p': port = optarg; break; case 'r': trans = optarg; break; case 'h': usage(argv[0], 0); case 'd': debug_flag++; nofork_flag++; break; case 'n': nofork_flag++; break; case 'V': printf("%s\n", PACKAGE_STRING); exit(0); break; default: usage(argv[0], 1); break; } } memset(&sa, 0, sizeof(sa)); sa.sa_handler = signal_handler; sigaction(SIGINT, &sa, NULL); sigaction(SIGTERM, &sa, NULL); sigaction(SIGQUIT, &sa, NULL); sigaction(SIGHUP, &sa, NULL); sa.sa_handler = SIG_IGN; sa.sa_flags = SA_RESTART; sigaction(SIGPIPE, &sa, NULL); if (!nofork_flag && daemon(0, 0)) { logerr("daemon() failed"); exit(1); } if (!debug_flag) { openlog("xioclntd", LOG_PID, LOG_DAEMON); use_syslog = 1; } /* Create the process PID file */ if (create_pidfile(pid_file) != 0) exit(EXIT_FAILURE); memset(&session_data, 0, sizeof(session_data)); memset(¶ms, 0, sizeof(params)); memset(&cparams, 0, sizeof(cparams)); /* initialize library */ xio_init(); /* create "hello world" message */ for (i = 0; i < QUEUE_DEPTH; i++) { memset(&session_data.req[i], 0, sizeof(session_data.req[i])); /* header */ session_data.req[i].out.header.iov_base = strdup("hello world header request"); session_data.req[i].out.header.iov_len = strlen((const char *) session_data.req[i].out.header.iov_base) + 1; /* iovec[0]*/ session_data.req[i].out.sgl_type = XIO_SGL_TYPE_IOV; session_data.req[i].out.data_iov.max_nents = XIO_IOVLEN; session_data.req[i].out.data_iov.sglist[0].iov_base = strdup("hello world iovec request"); session_data.req[i].out.data_iov.sglist[0].iov_len = strlen((const char *) session_data.req[i].out.data_iov.sglist[0].iov_base) + 1; session_data.req[i].out.data_iov.nents = 1; } /* create thread context for the client */ session_data.ctx = xio_context_create(NULL, 0, -1); /* create url to connect to */ if (trans) sprintf(url, "%s://%s:%s", trans, addr, port); else sprintf(url, "rdma://%s:%s", addr, port); params.type = XIO_SESSION_CLIENT; params.ses_ops = &ses_ops; params.user_context = &session_data; params.uri = url; reconnect: session = xio_session_create(¶ms); cparams.session = session; cparams.ctx = session_data.ctx; cparams.conn_user_context = &session_data; /* connect the session */ session_data.conn = xio_connect(&cparams); /* event dispatcher is now running */ xio_context_run_loop(session_data.ctx, XIO_INFINITE); if (reconnect_flag || reload_flag) { session_data.cnt = 0; if (reconnect_flag) sleep(1); reload_flag = 0; reconnect_flag = 0; goto reconnect; } /* normal exit phase */ logit(LOG_INFO, "exit signaled\n"); /* free the message */ for (i = 0; i < QUEUE_DEPTH; i++) { free(session_data.req[i].out.header.iov_base); free(session_data.req[i].out.data_iov.sglist[0].iov_base); } /* free the context */ xio_context_destroy(session_data.ctx); xio_shutdown(); remove_pidfile(); if (use_syslog) closelog(); return 0; }
int aim_main(int argc, char *argv[]) { set_crash_handler(crash_handler); AIM_LOG_STRUCT_REGISTER(); loci_logger = ofagent_loci_logger; core_cfg.stats_check_ms = 900; parse_options(argc, argv); /* Setup logging from command line options */ if (loglevel >= LOGLEVEL_DEFAULT) { aim_log_fid_set_all(AIM_LOG_FLAG_MSG, 1); aim_log_fid_set_all(AIM_LOG_FLAG_FATAL, 1); aim_log_fid_set_all(AIM_LOG_FLAG_ERROR, 1); aim_log_fid_set_all(AIM_LOG_FLAG_WARN, 1); } if (loglevel >= LOGLEVEL_VERBOSE) { aim_log_fid_set_all(AIM_LOG_FLAG_VERBOSE, 1); } if (loglevel >= LOGLEVEL_TRACE) { aim_log_fid_set_all(AIM_LOG_FLAG_TRACE, 1); } if (use_syslog) { aim_log_pvs_set_all(aim_pvs_syslog_open("ofagent", LOG_NDELAY, LOG_DAEMON)); } create_pidfile(); AIM_LOG_MSG("Starting ofagent %s (%s %s) pid %d", ofagent_version, ofagent_build_id, ofagent_build_os, getpid()); /* Increase maximum number of file descriptors */ struct rlimit rlim = { .rlim_cur = SOCKETMANAGER_CONFIG_MAX_SOCKETS, .rlim_max = SOCKETMANAGER_CONFIG_MAX_SOCKETS }; if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) { AIM_LOG_WARN("Failed to increase RLIMIT_NOFILE"); } /* Initialize all modules */ if (ind_soc_init(&soc_cfg) < 0) { AIM_LOG_FATAL("Failed to initialize Indigo socket manager"); return 1; } if (ind_cxn_init(&cxn_cfg) < 0) { AIM_LOG_FATAL("Failed to initialize Indigo connection manager"); return 1; } if (ind_core_init(&core_cfg) < 0) { AIM_LOG_FATAL("Failed to initialize Indigo core module"); return 1; } if (bcm_driver_init() < 0) { AIM_LOG_FATAL("Failed to initialize BCM driver"); return 1; } if (pipeline == NULL) { if (openflow_version == NULL || !strcmp(openflow_version, "1.0")) { pipeline = "standard-1.0"; } else if (!strcmp(openflow_version, "1.3")) { pipeline = "standard-1.3"; } else { AIM_DIE("unexpected OpenFlow version"); } } AIM_LOG_VERBOSE("Initializing forwarding pipeline '%s'", pipeline); indigo_error_t rv = pipeline_set(pipeline); if (rv < 0) { AIM_LOG_FATAL("Failed to set pipeline: %s", indigo_strerror(rv)); return 1; } if (config_filename) { ind_cfg_filename_set(config_filename); if (ind_cfg_load() < 0) { AIM_LOG_FATAL("Failed to load configuration file"); return 1; } } if (dpid) { indigo_core_dpid_set(dpid); } /* Enable all modules */ if (ind_soc_enable_set(1) < 0) { AIM_LOG_FATAL("Failed to enable Indigo socket manager"); return 1; } if (ind_cxn_enable_set(1) < 0) { AIM_LOG_FATAL("Failed to enable Indigo connection manager"); return 1; } if (ind_core_enable_set(1) < 0) { AIM_LOG_FATAL("Failed to enable Indigo core module"); return 1; } if (bcm_driver_enable_set(1) < 0) { AIM_LOG_FATAL("Failed to enable BCM driver"); return 1; } /* Add controller from command line */ { biglist_t *element; char *str; BIGLIST_FOREACH_DATA(element, controllers, char *, str) { AIM_LOG_VERBOSE("Adding controller %s", str); indigo_cxn_protocol_params_t proto; if (parse_controller(str, &proto, OF_TCP_PORT) < 0) { AIM_LOG_FATAL("Failed to parse controller string '%s'", str); return 1; } indigo_cxn_config_params_t config = { .version = OF_VERSION_1_0, .cxn_priority = 0, .local = 0, .listen = 0, .periodic_echo_ms = 2000, .reset_echo_count = 3, }; indigo_controller_id_t id; if (indigo_controller_add(&proto, &config, &id) < 0) { AIM_LOG_FATAL("Failed to add controller %s", str); return 1; } } } ind_core_mfr_desc_set(mfr_desc); snprintf(sw_desc, sizeof(sw_desc), "ofagent %s %s %s", ofagent_version, ofagent_build_id, ofagent_build_os); ind_core_sw_desc_set(sw_desc); // TODO //read_hardware_version(hw_desc); ind_core_hw_desc_set(hw_desc); char hostname[256]; char domainname[256]; if (gethostname(hostname, sizeof(hostname))) { sprintf(hostname, "(unknown)"); } if (getdomainname(domainname, sizeof(domainname))) { sprintf(domainname, "(unknown)"); } snprintf(dp_desc, sizeof(dp_desc), "%s.%s pid %d", hostname, domainname, getpid()); ind_core_dp_desc_set(dp_desc); AIM_LOG_INFO("Datapath description: %s", dp_desc); ind_core_serial_num_set(serial_num); /* The SIGHUP handler triggers sighup_callback to run in the main loop. */ if ((sighup_eventfd = eventfd(0, 0)) < 0) { AIM_LOG_FATAL("Failed to allocate eventfd"); abort(); } signal(SIGHUP, sighup); if (ind_soc_socket_register(sighup_eventfd, sighup_callback, NULL) < 0) { abort(); } /* The SIGTERM handler triggers sigterm_callback to run in the main loop. */ if ((sigterm_eventfd = eventfd(0, 0)) < 0) { AIM_LOG_FATAL("Failed to allocate eventfd"); abort(); } signal(SIGTERM, sigterm); if (ind_soc_socket_register(sigterm_eventfd, sigterm_callback, NULL) < 0) { abort(); } /* TODO Start handling upcalls */ //ind_ovs_enable(); //packet_trace_init(datapath_name); ind_soc_select_and_run(-1); AIM_LOG_MSG("Stopping ofagent %s", ofagent_version); ind_core_finish(); bcm_driver_finish(); ind_cxn_finish(); ind_soc_finish(); return 0; }
int main(int argc, char *argv[]) { int i, n, fdflags; struct sigaction sa; FILE *iffile; char *p; struct passwd *pw; struct timeval timo; sigset_t mask; struct protent *protp; struct stat statbuf; int connect_attempts = 0; char numbuf[16]; phase = PHASE_INITIALIZE; p = ttyname(0); if (p) strcpy(devnam, p); strcpy(default_devnam, devnam); script_env = NULL; /* Initialize syslog facilities */ #ifdef ULTRIX openlog("pppd", LOG_PID); #else openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP); setlogmask(LOG_UPTO(LOG_INFO)); #endif if (gethostname(hostname, MAXNAMELEN) < 0 ) { option_error("Couldn't get hostname: %m"); die(1); } hostname[MAXNAMELEN-1] = 0; uid = getuid(); privileged = uid == 0; sprintf(numbuf, "%d", uid); script_setenv("UID", numbuf); /* * Initialize to the standard option set, then parse, in order, * the system options file, the user's options file, * the tty's options file, and the command line arguments. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) (*protp->init)(0); progname = *argv; if (!options_from_file(_PATH_SYSOPTIONS, !privileged, 0, 1) || !options_from_user()) exit(1); scan_args(argc-1, argv+1); /* look for tty name on command line */ if (!options_for_tty() || !parse_args(argc-1, argv+1)) exit(1); /* * Check that we are running as root. */ if (geteuid() != 0) { option_error("must be root to run %s, since it is not setuid-root", argv[0]); die(1); } if (!ppp_available()) { option_error(no_ppp_msg); exit(1); } /* * Check that the options given are valid and consistent. */ sys_check_options(); auth_check_options(); for (i = 0; (protp = protocols[i]) != NULL; ++i) if (protp->check_options != NULL) (*protp->check_options)(); if (demand && connector == 0) { option_error("connect script required for demand-dialling\n"); exit(1); } script_setenv("DEVICE", devnam); sprintf(numbuf, "%d", baud_rate); script_setenv("SPEED", numbuf); /* * If the user has specified the default device name explicitly, * pretend they hadn't. */ if (!default_device && strcmp(devnam, default_devnam) == 0) default_device = 1; if (default_device) nodetach = 1; /* * Initialize system-dependent stuff and magic number package. */ sys_init(); magic_init(); if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); /* * Detach ourselves from the terminal, if required, * and identify who is running us. */ if (nodetach == 0) detach(); pid = getpid(); p = getlogin(); stime = time(NULL); if (p == NULL) { pw = getpwuid(uid); if (pw != NULL && pw->pw_name != NULL) p = pw->pw_name; else p = "(unknown)"; } syslog(LOG_NOTICE, "pppd %s.%d%s started by %s, uid %d", VERSION, PATCHLEVEL, IMPLEMENTATION, p, uid); /* * Compute mask of all interesting signals and install signal handlers * for each. Only one signal handler may be active at a time. Therefore, * all other signals should be masked when any handler is executing. */ sigemptyset(&mask); sigaddset(&mask, SIGHUP); sigaddset(&mask, SIGINT); sigaddset(&mask, SIGTERM); sigaddset(&mask, SIGCHLD); #define SIGNAL(s, handler) { \ sa.sa_handler = handler; \ if (sigaction(s, &sa, NULL) < 0) { \ syslog(LOG_ERR, "Couldn't establish signal handler (%d): %m", s); \ die(1); \ } \ } sa.sa_mask = mask; sa.sa_flags = 0; SIGNAL(SIGHUP, hup); /* Hangup */ SIGNAL(SIGINT, term); /* Interrupt */ SIGNAL(SIGTERM, term); /* Terminate */ SIGNAL(SIGCHLD, chld); SIGNAL(SIGUSR1, toggle_debug); /* Toggle debug flag */ SIGNAL(SIGUSR2, open_ccp); /* Reopen CCP */ /* * Install a handler for other signals which would otherwise * cause pppd to exit without cleaning up. */ SIGNAL(SIGABRT, bad_signal); SIGNAL(SIGALRM, bad_signal); SIGNAL(SIGFPE, bad_signal); SIGNAL(SIGILL, bad_signal); SIGNAL(SIGPIPE, bad_signal); SIGNAL(SIGQUIT, bad_signal); SIGNAL(SIGSEGV, bad_signal); #ifdef SIGBUS SIGNAL(SIGBUS, bad_signal); #endif #ifdef SIGEMT SIGNAL(SIGEMT, bad_signal); #endif #ifdef SIGPOLL SIGNAL(SIGPOLL, bad_signal); #endif #ifdef SIGPROF SIGNAL(SIGPROF, bad_signal); #endif #ifdef SIGSYS SIGNAL(SIGSYS, bad_signal); #endif #ifdef SIGTRAP SIGNAL(SIGTRAP, bad_signal); #endif #ifdef SIGVTALRM SIGNAL(SIGVTALRM, bad_signal); #endif #ifdef SIGXCPU SIGNAL(SIGXCPU, bad_signal); #endif #ifdef SIGXFSZ SIGNAL(SIGXFSZ, bad_signal); #endif /* * Apparently we can get a SIGPIPE when we call syslog, if * syslogd has died and been restarted. Ignoring it seems * be sufficient. */ signal(SIGPIPE, SIG_IGN); /* * If we're doing dial-on-demand, set up the interface now. */ if (demand) { /* * Open the loopback channel and set it up to be the ppp interface. */ open_ppp_loopback(); syslog(LOG_INFO, "Using interface ppp%d", ifunit); sprintf(ifname, "ppp%d", ifunit); script_setenv("IFNAME", ifname); create_pidfile(); /* write pid to file */ /* * Configure the interface and mark it up, etc. */ demand_conf(); } for (;;) { need_holdoff = 1; if (demand) { /* * Don't do anything until we see some activity. */ phase = PHASE_DORMANT; kill_link = 0; demand_unblock(); for (;;) { wait_loop_output(timeleft(&timo)); calltimeout(); if (kill_link) { if (!persist) die(0); kill_link = 0; } if (get_loop_output()) break; reap_kids(); } /* * Now we want to bring up the link. */ demand_block(); syslog(LOG_INFO, "Starting link"); } /* * Lock the device if we've been asked to. */ if (lockflag && !default_device) { if (lock(devnam) < 0) goto fail; locked = 1; } /* * Open the serial device and set it up to be the ppp interface. * First we open it in non-blocking mode so we can set the * various termios flags appropriately. If we aren't dialling * out and we want to use the modem lines, we reopen it later * in order to wait for the carrier detect signal from the modem. */ while ((ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0)) < 0) { if (errno != EINTR) syslog(LOG_ERR, "Failed to open %s: %m", devnam); if (!persist || errno != EINTR) goto fail; } if ((fdflags = fcntl(ttyfd, F_GETFL)) == -1 || fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) syslog(LOG_WARNING, "Couldn't reset non-blocking mode on device: %m"); hungup = 0; kill_link = 0; /* * Do the equivalent of `mesg n' to stop broadcast messages. */ if (fstat(ttyfd, &statbuf) < 0 || fchmod(ttyfd, statbuf.st_mode & ~(S_IWGRP | S_IWOTH)) < 0) { syslog(LOG_WARNING, "Couldn't restrict write permissions to %s: %m", devnam); } else tty_mode = statbuf.st_mode; /* run connection script */ if (connector && connector[0]) { MAINDEBUG((LOG_INFO, "Connecting with <%s>", connector)); /* * Set line speed, flow control, etc. * On most systems we set CLOCAL for now so that we can talk * to the modem before carrier comes up. But this has the * side effect that we might miss it if CD drops before we * get to clear CLOCAL below. On systems where we can talk * successfully to the modem with CLOCAL clear and CD down, * we can clear CLOCAL at this point. */ set_up_tty(ttyfd, 1); /* drop dtr to hang up in case modem is off hook */ if (!default_device && modem) { setdtr(ttyfd, FALSE); sleep(1); setdtr(ttyfd, TRUE); } if (device_script(connector, ttyfd, ttyfd) < 0) { syslog(LOG_ERR, "Connect script failed"); setdtr(ttyfd, FALSE); connect_attempts++; goto fail; } syslog(LOG_INFO, "Serial connection established."); sleep(1); /* give it time to set up its terminal */ } connect_attempts = 0; /* we made it through ok */ /* set line speed, flow control, etc.; clear CLOCAL if modem option */ set_up_tty(ttyfd, 0); /* reopen tty if necessary to wait for carrier */ if (connector == NULL && modem) { while ((i = open(devnam, O_RDWR)) < 0) { if (errno != EINTR) syslog(LOG_ERR, "Failed to reopen %s: %m", devnam); if (!persist || errno != EINTR || hungup || kill_link) goto fail; } close(i); } /* run welcome script, if any */ if (welcomer && welcomer[0]) { if (device_script(welcomer, ttyfd, ttyfd) < 0) syslog(LOG_WARNING, "Welcome script failed"); } /* set up the serial device as a ppp interface */ establish_ppp(ttyfd); if (!demand) { syslog(LOG_INFO, "Using interface ppp%d", ifunit); sprintf(ifname, "ppp%d", ifunit); create_pidfile(); /* write pid to file */ /* write interface unit number to file */ for (n = strlen(devnam); n > 0 ; n--) if (devnam[n] == '/') { n++; break; } sprintf(iffilename, "%s%s.if", _PATH_VARRUN, &devnam[n]); if ((iffile = fopen(iffilename, "w")) != NULL) { fprintf(iffile, "ppp%d\n", ifunit); fclose(iffile); } else { syslog(LOG_ERR, "Failed to create if file %s: %m", iffilename); iffilename[0] = 0; } script_setenv("IFNAME", ifname); } /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ syslog(LOG_NOTICE, "Connect: %s <--> %s", ifname, devnam); stime = time(NULL); lcp_lowerup(0); lcp_open(0); /* Start protocol */ for (phase = PHASE_ESTABLISH; phase != PHASE_DEAD; ) { wait_input(timeleft(&timo)); calltimeout(); get_input(); if (kill_link) { lcp_close(0, "User request"); kill_link = 0; } if (open_ccp_flag) { if (phase == PHASE_NETWORK) { ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */ (*ccp_protent.open)(0); } open_ccp_flag = 0; } reap_kids(); /* Don't leave dead kids lying around */ } /* * If we may want to bring the link up again, transfer * the ppp unit back to the loopback. Set the * real serial device back to its normal mode of operation. */ clean_check(); if (demand) restore_loop(); disestablish_ppp(ttyfd); /* * Run disconnector script, if requested. * XXX we may not be able to do this if the line has hung up! */ if (disconnector && !hungup) { set_up_tty(ttyfd, 1); if (device_script(disconnector, ttyfd, ttyfd) < 0) { syslog(LOG_WARNING, "disconnect script failed"); } else { syslog(LOG_INFO, "Serial link disconnected."); } } fail: if (ttyfd >= 0) close_tty(); if (locked) { unlock(); locked = 0; } if (!demand) { if (pidfilename[0] != 0 && unlink(pidfilename) < 0 && errno != ENOENT) syslog(LOG_WARNING, "unable to delete pid file: %m"); pidfilename[0] = 0; if (iffile) if (unlink(iffilename) < 0 && errno != ENOENT) syslog(LOG_WARNING, "unable to delete if file: %m"); iffilename[0] = 0; } /* limit to retries? */ if (max_con_attempts) if (connect_attempts >= max_con_attempts) break; if (!persist) die(1); if (demand) demand_discard(); if (holdoff > 0 && need_holdoff) { phase = PHASE_HOLDOFF; TIMEOUT(holdoff_end, NULL, holdoff); do { wait_time(timeleft(&timo)); calltimeout(); if (kill_link) { if (!persist) die(0); kill_link = 0; phase = PHASE_DORMANT; /* allow signal to end holdoff */ } reap_kids(); } while (phase == PHASE_HOLDOFF); } } die(0); return 0; }
int main(int argc, char** argv) { /* I18n */ setlocale(LC_ALL, ""); #if ENABLE_NLS bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); #endif abrt_init(argv); int parent_pid = getpid(); const char *program_usage_string = _( "& [options]" ); enum { OPT_v = 1 << 0, OPT_d = 1 << 1, OPT_s = 1 << 2, // TODO: get rid of -t NUM, it is no longer useful since dbus is moved to a separate tool OPT_t = 1 << 3, OPT_p = 1 << 4, }; /* Keep enum above and order of options below in sync! */ struct options program_options[] = { OPT__VERBOSE(&g_verbose), OPT_BOOL( 'd', NULL, NULL , _("Do not daemonize")), OPT_BOOL( 's', NULL, NULL , _("Log to syslog even with -d")), OPT_INTEGER('t', NULL, &s_timeout, _("Exit after NUM seconds of inactivity")), OPT_BOOL( 'p', NULL, NULL , _("Add program names to log")), OPT_END() }; unsigned opts = parse_opts(argc, argv, program_options, program_usage_string); export_abrt_envvars(opts & OPT_p); #if 0 /* We no longer use dbus */ /* When dbus daemon starts us, it doesn't set PATH * (I saw it set only DBUS_STARTER_ADDRESS and DBUS_STARTER_BUS_TYPE). * In this case, set something sane: */ const char *env_path = getenv("PATH"); if (!env_path || !env_path[0]) putenv((char*)"PATH=/usr/sbin:/usr/bin:/sbin:/bin"); #endif unsetenv("ABRT_SYSLOG"); msg_prefix = g_progname; /* for log_warning(), error_msg() and such */ if (getuid() != 0) error_msg_and_die("Must be run as root"); if (opts & OPT_s) start_logging(); xpipe(s_signal_pipe); close_on_exec_on(s_signal_pipe[0]); close_on_exec_on(s_signal_pipe[1]); ndelay_on(s_signal_pipe[0]); /* I/O should not block - */ ndelay_on(s_signal_pipe[1]); /* especially writes! they happen in signal handler! */ signal(SIGTERM, handle_signal); signal(SIGINT, handle_signal); signal(SIGCHLD, handle_signal); GIOChannel* channel_signal = NULL; guint channel_id_signal_event = 0; bool pidfile_created = false; struct abrt_inotify_watch *aiw = NULL; int ret = 1; /* Initialization */ log_notice("Loading settings"); if (load_abrt_conf() != 0) goto init_error; /* Moved before daemonization because parent waits for signal from daemon * only for short period and time consumed by * mark_unprocessed_dump_dirs_not_reportable() is slightly unpredictable. */ sanitize_dump_dir_rights(); mark_unprocessed_dump_dirs_not_reportable(g_settings_dump_location); /* Daemonize unless -d */ if (!(opts & OPT_d)) { /* forking to background */ fflush(NULL); /* paranoia */ pid_t pid = fork(); if (pid < 0) { perror_msg_and_die("fork"); } if (pid > 0) { /* Parent */ /* Wait for child to notify us via SIGTERM that it feels ok */ int i = 20; /* 2 sec */ while (s_sig_caught == 0 && --i) { usleep(100 * 1000); } if (s_sig_caught == SIGTERM) { exit(0); } if (s_sig_caught) { error_msg_and_die("Failed to start: got sig %d", s_sig_caught); } error_msg_and_die("Failed to start: timeout waiting for child"); } /* Child (daemon) continues */ if (setsid() < 0) perror_msg_and_die("setsid"); if (g_verbose == 0 && logmode != LOGMODE_JOURNAL) start_logging(); } log_notice("Creating glib main loop"); s_main_loop = g_main_loop_new(NULL, FALSE); /* Watching 'g_settings_dump_location' for delete self * because hooks expects that the dump location exists if abrtd is running */ aiw = abrt_inotify_watch_init(g_settings_dump_location, IN_DUMP_LOCATION_FLAGS, handle_inotify_cb, /*user data*/NULL); /* Add an event source which waits for INT/TERM signal */ log_notice("Adding signal pipe watch to glib main loop"); channel_signal = abrt_gio_channel_unix_new(s_signal_pipe[0]); channel_id_signal_event = add_watch_or_die(channel_signal, G_IO_IN | G_IO_PRI | G_IO_HUP, handle_signal_cb); guint name_id = 0; /* Mark the territory */ log_notice("Creating pid file"); if (create_pidfile() != 0) goto init_error; pidfile_created = true; /* Open socket to receive new problem data (from python etc). */ dumpsocket_init(); /* Inform parent that we initialized ok */ if (!(opts & OPT_d)) { log_notice("Signalling parent"); kill(parent_pid, SIGTERM); if (logmode != LOGMODE_JOURNAL) start_logging(); } /* Only now we want signal pipe to work */ s_signal_pipe_write = s_signal_pipe[1]; /* Own a name on D-Bus */ name_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, ABRTD_DBUS_NAME, G_BUS_NAME_OWNER_FLAGS_NONE, on_bus_acquired, on_name_acquired, on_name_lost, NULL, NULL); start_idle_timeout(); /* Enter the event loop */ log_debug("Init complete, entering main loop"); g_main_loop_run(s_main_loop); ret = 0; /* Jump to exit */ goto cleanup; init_error: /* Initialization error */ error_msg("Error while initializing daemon"); /* Inform parent that initialization failed */ if (!(opts & OPT_d)) kill(parent_pid, SIGINT); cleanup: if (name_id > 0) g_bus_unown_name (name_id); /* Error or INT/TERM. Clean up, in reverse order. * Take care to not undo things we did not do. */ dumpsocket_shutdown(); if (pidfile_created) unlink(VAR_RUN_PIDFILE); if (channel_id_signal_event > 0) g_source_remove(channel_id_signal_event); if (channel_signal) g_io_channel_unref(channel_signal); abrt_inotify_watch_destroy(aiw); if (s_main_loop) g_main_loop_unref(s_main_loop); free_abrt_conf_data(); if (s_sig_caught && s_sig_caught != SIGCHLD) { /* We use TERM to stop abrtd, so not printing out error message. */ if (s_sig_caught != SIGTERM) { error_msg("Got signal %d, exiting", s_sig_caught); signal(s_sig_caught, SIG_DFL); raise(s_sig_caught); } } /* Exiting */ log_notice("Exiting"); return ret; }
/* * Entry point. Process command line options, start up pcap and enter capture loop. */ int main(int argc, char *argv[]) { pthread_t packetth; options_t *options; options = parse_options(argc, argv); if (options->verbose) set_loglevel(LOG_INFO); if (options->adjunct) create_pidfile(); /* * In adjunct mode, it's important that the attached program gets * notification of images in a timely manner. Make stdout line-buffered * for this reason. */ if (options->adjunct) setvbuf(stdout, NULL, _IOLBF, 0); /* * If a directory name has not been specified, then we need to create one. * Otherwise, check that it's a directory into which we may write files. */ if (options->tmpdir) { check_dir_is_rw(options->tmpdir); set_tmpdir(options->tmpdir, TMPDIR_USER_OWNED, options->max_tmpfiles, options->adjunct); } else { /* need to make a temporary directory. */ set_tmpdir(make_tmpdir(), TMPDIR_APP_OWNED, options->max_tmpfiles, options->adjunct); } setup_signals(); /* Start up the audio player, if required. */ if (!options->adjunct && (options->extract_type & m_audio)) do_mpeg_player(); #ifndef NO_DISPLAY_WINDOW /* Possibly fork to start the display child process */ if (!options->adjunct && (options->extract_type & m_image)) do_image_display(options->savedimgpfx, options->beep); else log_msg(LOG_INFO, "operating in adjunct mode"); #endif /* !NO_DISPLAY_WINDOW */ init_mediadrv(options->extract_type, !options->adjunct); /* Start up pcap. */ if (options->dumpfile) packetcapture_open_offline(options->dumpfile); else packetcapture_open_live(options->interface, options->filterexpr, options->promisc); connection_alloc_slots(); /* * Actually start the capture stuff up. Unfortunately, on many platforms, * libpcap doesn't have read timeouts, so we start the thing up in a * separate thread. Yay! */ pthread_create(&packetth, NULL, capture_thread, NULL); while (!foad) sleep(1); if (options->verbose) print_exit_reason(); pthread_cancel(packetth); /* make sure thread quits even if it's stuck in pcap_dispatch */ pthread_join(packetth, NULL); /* Clean up. */ /* pcap_freecode(pc, &filter);*/ /* not on some systems... */ packetcapture_close(); /* Easier for memory-leak debugging if we deallocate all this here.... */ connection_free_slots(); clean_tmpdir(); if (options->adjunct) close_pidfile(); return 0; }
int main (int argc, char *argv[]) { int i, pidfd; int blocked_signals[] = {SIGPIPE, 0}; int cc; char *oom_value; uint32_t slurmd_uid = 0; uint32_t curr_uid = 0; char time_stamp[256]; log_options_t lopts = LOG_OPTS_INITIALIZER; /* NOTE: logfile is NULL at this point */ log_init(argv[0], lopts, LOG_DAEMON, NULL); /* * Make sure we have no extra open files which * would be propagated to spawned tasks. */ cc = sysconf(_SC_OPEN_MAX); for (i = 3; i < cc; i++) close(i); /* * Drop supplementary groups. */ if (geteuid() == 0) { if (setgroups(0, NULL) != 0) { fatal("Failed to drop supplementary groups, " "setgroups: %m"); } } else { debug("Not running as root. Can't drop supplementary groups"); } /* * Create and set default values for the slurmd global * config variable "conf" */ conf = xmalloc(sizeof(slurmd_conf_t)); _init_conf(); conf->argv = &argv; conf->argc = &argc; if (_slurmd_init() < 0) { error( "slurmd initialization failed" ); fflush( NULL ); exit(1); } slurmd_uid = slurm_get_slurmd_user_id(); curr_uid = getuid(); if (curr_uid != slurmd_uid) { struct passwd *pw = NULL; char *slurmd_user = NULL; char *curr_user = NULL; /* since when you do a getpwuid you get a pointer to a * structure you have to do a xstrdup on the first * call or your information will just get over * written. This is a memory leak, but a fatal is * called right after so it isn't that big of a deal. */ if ((pw=getpwuid(slurmd_uid))) slurmd_user = xstrdup(pw->pw_name); if ((pw=getpwuid(curr_uid))) curr_user = pw->pw_name; fatal("You are running slurmd as something " "other than user %s(%d). If you want to " "run as this user add SlurmdUser=%s " "to the slurm.conf file.", slurmd_user, slurmd_uid, curr_user); } init_setproctitle(argc, argv); xsignal(SIGTERM, &_term_handler); xsignal(SIGINT, &_term_handler); xsignal(SIGHUP, &_hup_handler ); xsignal_block(blocked_signals); debug3("slurmd initialization successful"); /* * Become a daemon if desired. * Do not chdir("/") or close all fd's */ if (conf->daemonize) { if (daemon(1,1) == -1) error("Couldn't daemonize slurmd: %m"); } test_core_limit(); info("slurmd version %s started", SLURM_VERSION_STRING); debug3("finished daemonize"); if ((oom_value = getenv("SLURMD_OOM_ADJ"))) { i = atoi(oom_value); debug("Setting slurmd oom_adj to %d", i); set_oom_adj(i); } _kill_old_slurmd(); if (conf->mlock_pages) { /* * Call mlockall() if available to ensure slurmd * doesn't get swapped out */ #ifdef _POSIX_MEMLOCK if (mlockall (MCL_FUTURE | MCL_CURRENT) < 0) error ("failed to mlock() slurmd pages: %m"); #else error ("mlockall() system call does not appear to be available"); #endif /* _POSIX_MEMLOCK */ } /* * Restore any saved revoked credential information */ if (!conf->cleanstart && (_restore_cred_state(conf->vctx) < 0)) return SLURM_FAILURE; if (jobacct_gather_init() != SLURM_SUCCESS) fatal("Unable to initialize jobacct_gather"); if (job_container_init() < 0) fatal("Unable to initialize job_container plugin."); if (container_g_restore(conf->spooldir, !conf->cleanstart)) error("Unable to restore job_container state."); if (switch_g_node_init() < 0) fatal("Unable to initialize interconnect."); if (conf->cleanstart && switch_g_clear_node_state()) fatal("Unable to clear interconnect state."); switch_g_slurmd_init(); _create_msg_socket(); conf->pid = getpid(); /* This has to happen after daemon(), which closes all fd's, so we keep the write lock of the pidfile. */ pidfd = create_pidfile(conf->pidfile, 0); rfc2822_timestamp(time_stamp, sizeof(time_stamp)); info("%s started on %s", slurm_prog_name, time_stamp); _install_fork_handlers(); list_install_fork_handlers(); slurm_conf_install_fork_handlers(); /* * Initialize any plugins */ if (slurmd_plugstack_init()) fatal("failed to initialize slurmd_plugstack"); _spawn_registration_engine(); _msg_engine(); /* * Close fd here, otherwise we'll deadlock since create_pidfile() * flocks the pidfile. */ if (pidfd >= 0) /* valid pidfd, non-error */ (void) close(pidfd); /* Ignore errors */ if (unlink(conf->pidfile) < 0) error("Unable to remove pidfile `%s': %m", conf->pidfile); _wait_for_all_threads(120); _slurmd_fini(); _destroy_conf(); slurm_crypto_fini(); /* must be after _destroy_conf() */ info("Slurmd shutdown completing"); log_fini(); return 0; }
static void server_loop(const char *socket_path, const char *pidfile_path, const struct uuidd_cxt_t *uuidd_cxt) { struct sockaddr_un from_addr; socklen_t fromlen; int32_t reply_len = 0; uuid_t uu; char reply_buf[1024], *cp; char op, str[UUID_STR_LEN]; int i, ns, len, num; int s = 0; int fd_pidfile = -1; int ret; #ifdef USE_SOCKET_ACTIVATION if (!uuidd_cxt->no_sock) /* no_sock implies no_fork and no_pid */ #endif { signal(SIGALRM, terminate_intr); alarm(30); if (pidfile_path) fd_pidfile = create_pidfile(pidfile_path, uuidd_cxt->quiet); ret = call_daemon(socket_path, UUIDD_OP_GETPID, reply_buf, sizeof(reply_buf), 0, NULL); if (ret > 0) { if (!uuidd_cxt->quiet) warnx(_("uuidd daemon is already running at pid %s"), reply_buf); exit(EXIT_FAILURE); } alarm(0); s = create_socket(socket_path, (!uuidd_cxt->debug || !uuidd_cxt->no_fork), uuidd_cxt->quiet); if (listen(s, SOMAXCONN) < 0) { if (!uuidd_cxt->quiet) warn(_("couldn't listen on unix socket %s"), socket_path); exit(EXIT_FAILURE); } if (!uuidd_cxt->debug && !uuidd_cxt->no_fork) create_daemon(); if (pidfile_path) { sprintf(reply_buf, "%8d\n", getpid()); ignore_result( ftruncate(fd_pidfile, 0) ); write_all(fd_pidfile, reply_buf, strlen(reply_buf)); if (fd_pidfile > 1) close(fd_pidfile); /* Unlock the pid file */ } } signal(SIGHUP, terminate_intr); signal(SIGINT, terminate_intr); signal(SIGTERM, terminate_intr); signal(SIGALRM, terminate_intr); signal(SIGPIPE, SIG_IGN); #ifdef USE_SOCKET_ACTIVATION if (uuidd_cxt->no_sock) { if (sd_listen_fds(0) != 1) errx(EXIT_FAILURE, _("no or too many file descriptors received")); s = SD_LISTEN_FDS_START + 0; } #endif while (1) { fromlen = sizeof(from_addr); if (uuidd_cxt->timeout > 0) alarm(uuidd_cxt->timeout); ns = accept(s, (struct sockaddr *) &from_addr, &fromlen); alarm(0); if (ns < 0) { if ((errno == EAGAIN) || (errno == EINTR)) continue; else err(EXIT_FAILURE, "accept"); } len = read(ns, &op, 1); if (len != 1) { if (len < 0) warn(_("read failed")); else warnx(_("error reading from client, len = %d"), len); goto shutdown_socket; } if ((op == UUIDD_OP_BULK_TIME_UUID) || (op == UUIDD_OP_BULK_RANDOM_UUID)) { if (read_all(ns, (char *) &num, sizeof(num)) != 4) goto shutdown_socket; if (uuidd_cxt->debug) fprintf(stderr, _("operation %d, incoming num = %d\n"), op, num); } else if (uuidd_cxt->debug) fprintf(stderr, _("operation %d\n"), op); switch (op) { case UUIDD_OP_GETPID: sprintf(reply_buf, "%d", getpid()); reply_len = strlen(reply_buf) + 1; break; case UUIDD_OP_GET_MAXOP: sprintf(reply_buf, "%d", UUIDD_MAX_OP); reply_len = strlen(reply_buf) + 1; break; case UUIDD_OP_TIME_UUID: num = 1; __uuid_generate_time(uu, &num); if (uuidd_cxt->debug) { uuid_unparse(uu, str); fprintf(stderr, _("Generated time UUID: %s\n"), str); } memcpy(reply_buf, uu, sizeof(uu)); reply_len = sizeof(uu); break; case UUIDD_OP_RANDOM_UUID: num = 1; __uuid_generate_random(uu, &num); if (uuidd_cxt->debug) { uuid_unparse(uu, str); fprintf(stderr, _("Generated random UUID: %s\n"), str); } memcpy(reply_buf, uu, sizeof(uu)); reply_len = sizeof(uu); break; case UUIDD_OP_BULK_TIME_UUID: __uuid_generate_time(uu, &num); if (uuidd_cxt->debug) { uuid_unparse(uu, str); fprintf(stderr, P_("Generated time UUID %s " "and %d following\n", "Generated time UUID %s " "and %d following\n", num - 1), str, num - 1); } memcpy(reply_buf, uu, sizeof(uu)); reply_len = sizeof(uu); memcpy(reply_buf + reply_len, &num, sizeof(num)); reply_len += sizeof(num); break; case UUIDD_OP_BULK_RANDOM_UUID: if (num < 0) num = 1; if (num > 1000) num = 1000; if (num * UUID_LEN > (int) (sizeof(reply_buf) - sizeof(num))) num = (sizeof(reply_buf) - sizeof(num)) / UUID_LEN; __uuid_generate_random((unsigned char *) reply_buf + sizeof(num), &num); if (uuidd_cxt->debug) { fprintf(stderr, P_("Generated %d UUID:\n", "Generated %d UUIDs:\n", num), num); for (i = 0, cp = reply_buf + sizeof(num); i < num; i++, cp += UUID_LEN) { uuid_unparse((unsigned char *)cp, str); fprintf(stderr, "\t%s\n", str); } } reply_len = (num * UUID_LEN) + sizeof(num); memcpy(reply_buf, &num, sizeof(num)); break; default: if (uuidd_cxt->debug) fprintf(stderr, _("Invalid operation %d\n"), op); goto shutdown_socket; } write_all(ns, (char *) &reply_len, sizeof(reply_len)); write_all(ns, reply_buf, reply_len); shutdown_socket: close(ns); } }
void bozo_daemon_init(bozohttpd_t *httpd) { struct addrinfo h, *r, *r0; const char *portnum; int e, i, on = 1; if (!httpd->background) return; portnum = (httpd->bindport) ? httpd->bindport : "http"; memset(&h, 0, sizeof(h)); h.ai_family = PF_UNSPEC; h.ai_socktype = SOCK_STREAM; h.ai_flags = AI_PASSIVE; e = getaddrinfo(httpd->bindaddress, portnum, &h, &r0); if (e) bozo_err(httpd, 1, "getaddrinfo([%s]:%s): %s", httpd->bindaddress ? httpd->bindaddress : "*", portnum, gai_strerror(e)); for (r = r0; r != NULL; r = r->ai_next) httpd->nsock++; httpd->sock = bozomalloc(httpd, httpd->nsock * sizeof(*httpd->sock)); httpd->fds = bozomalloc(httpd, httpd->nsock * sizeof(*httpd->fds)); for (i = 0, r = r0; r != NULL; r = r->ai_next) { httpd->sock[i] = socket(r->ai_family, SOCK_STREAM, 0); if (httpd->sock[i] == -1) continue; if (setsockopt(httpd->sock[i], SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) bozo_warn(httpd, "setsockopt SO_REUSEADDR: %s", strerror(errno)); if (bind(httpd->sock[i], r->ai_addr, r->ai_addrlen) == -1) continue; if (listen(httpd->sock[i], SOMAXCONN) == -1) continue; httpd->fds[i].events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND | POLLERR; httpd->fds[i].fd = httpd->sock[i]; i++; } if (i == 0) bozo_err(httpd, 1, "could not find any addresses to bind"); httpd->nsock = i; freeaddrinfo(r0); if (httpd->foreground == 0) daemon(1, 0); create_pidfile(httpd); bozo_warn(httpd, "started in daemon mode as `%s' port `%s' root `%s'", httpd->virthostname, portnum, httpd->slashdir); signal(SIGHUP, controlled_exit); signal(SIGINT, controlled_exit); signal(SIGTERM, controlled_exit); signal(SIGCHLD, sigchild); }
static void setup(void) { char *fname = NULL; uint64_t intvl; if (atexit(teardown) != 0) { log_stderr("cannot register teardown procedure with atexit()"); exit(EX_OSERR); /* only failure comes from NOMEM */ } /* Setup logging first */ log_setup(&stats.log); if (debug_setup(&setting.debug) != CC_OK) { log_stderr("debug log setup failed"); exit(EX_CONFIG); } /* setup top-level application options */ if (option_bool(&setting.ds.daemonize)) { daemonize(); } fname = option_str(&setting.ds.pid_filename); if (fname != NULL) { /* to get the correct pid, call create_pidfile after daemonize */ create_pidfile(fname); } /* setup library modules */ buf_setup(&setting.buf, &stats.buf); dbuf_setup(&setting.dbuf, &stats.dbuf); event_setup(&stats.event); sockio_setup(&setting.sockio, &stats.sockio); tcp_setup(&setting.tcp, &stats.tcp); timing_wheel_setup(&stats.timing_wheel); /* setup pelikan modules */ time_setup(&setting.time); procinfo_setup(&stats.procinfo); request_setup(&setting.request, &stats.request); response_setup(&setting.response, &stats.response); parse_setup(&stats.parse_req, NULL); compose_setup(NULL, &stats.compose_rsp); slab_setup(&setting.slab, &stats.slab); process_setup(&setting.process, &stats.process); admin_process_setup(); core_admin_setup(&setting.admin); core_server_setup(&setting.server, &stats.server); core_worker_setup(&setting.worker, &stats.worker); /* adding recurring events to maintenance/admin thread */ intvl = option_uint(&setting.ds.dlog_intvl); if (core_admin_register(intvl, debug_log_flush, NULL) == NULL) { log_stderr("Could not register timed event to flush debug log"); goto error; } return; error: if (fname != NULL) { remove_pidfile(fname); } /* since we registered teardown with atexit, it'll be called upon exit */ exit(EX_CONFIG); }
int main (int argc, char **argv) { int c, background=0, encap=0, sndbuf=8192; struct atm_qos reqqos; int itfnum; lastsock=-1; lastitf=0; /* st qos to 0 */ memset(&reqqos, 0, sizeof(reqqos)); openlog (LOG_NAME,LOG_OPTION,LOG_FACILITY); if (argc>1) while ((c = getopt(argc, argv,"q:a:bc:e:s:?h")) !=EOF) switch (c) { case 'q': printf ("optarg : %s",optarg); if (text2qos(optarg,&reqqos,0)) fprintf(stderr,"QOS parameter invalid\n"); break; case 'a': assign_vcc(optarg, encap, sndbuf, reqqos); break; case 'b': background=1; break; case 'c': create_br(optarg); itfnum = atoi(optarg); break; case 'e': encap=(atoi(optarg)); if(encap<0){ syslog (LOG_ERR, "invalid encapsulation: %s:\n",optarg); encap=0; } break; case 's': sndbuf=(atoi(optarg)); if(sndbuf<0){ syslog(LOG_ERR, "Invalid sndbuf: %s, using size of 8192 instead\n",optarg); sndbuf=8192; } break; case '?': case 'h': default: usage(argv[0]); } else usage(argv[0]); if (argc != optind) usage(argv[0]); if(lastsock>=0) close(lastsock); if (background) { pid_t pid; pid=fork(); if (pid < 0) { fprintf(stderr,"Error detaching\n"); exit(2); } else if (pid) exit(0); // This is the parent // Become a process group and session group leader if (setsid()<0) { fprintf (stderr,"Could not set process group\n"); exit(2); } // Fork again to let process group leader exit pid = fork(); if (pid < 0) { fprintf(stderr,"Error detaching during second fork\n"); exit(2); } else if (pid) exit(0); // This is the parent // Now we're ready for buisness chdir("/"); // Don't keep directories in use close(0); close(1); close(2); // Close stdin, -out and -error /* Note that this implementation does not keep an open stdout/err. If we need them they can be opened now */ } create_pidfile(itfnum); syslog (LOG_INFO, "RFC 1483/2684 bridge daemon started\n"); atexit (exitFunc); while (1) sleep(30); /* to keep the sockets... */ return 0; }
/* * Entry routine/main loop. */ int main (int argc, char **argv) { extern char *optarg; int pid; int arg; int port = 0, out_port = 0; struct rlimit rlim; int pipefds[2] = { -1, -1}; char status; /* Default: daemon mode, no other options */ run_mode = 0; /* Set the basename */ if ((name_p = strrchr(argv[0],'/')) != NULL) { name_p ++; } else { name_p = argv[0]; } /* Get the version */ if ((version_p = strrchr(VERSION,' ')) != NULL) { version_p++; } else { version_p = VERSION; } /* Set hostname */ MY_NAME = NULL; /* Process command line switches */ while ((arg = getopt_long(argc, argv, "h?vVFNH:dn:p:o:P:L", longopts, NULL)) != EOF) { switch (arg) { case 'V': /* Version */ case 'v': printf("%s version %s\n",name_p,version_p); exit(0); case 'F': /* Foreground/nodaemon mode */ run_mode |= MODE_NODAEMON; break; case 'N': run_mode |= MODE_NOTIFY_ONLY; break; case 'L': /* Listen only */ run_mode |= MODE_NO_NOTIFY; break; case 'd': /* No daemon only - log to stderr */ run_mode |= MODE_LOG_STDERR; break; case 'o': out_port = atoi(optarg); if (out_port < 1 || out_port > 65535) { fprintf(stderr, "%s: bad port number: %s\n", argv[0], optarg); usage(); exit(1); } break; case 'p': port = atoi(optarg); if (port < 1 || port > 65535) { fprintf(stderr, "%s: bad port number: %s\n", argv[0], optarg); usage(); exit(1); } break; case 'n': /* Specify local hostname */ run_mode |= STATIC_HOSTNAME; MY_NAME = xstrdup(optarg); break; case 'P': if ((DIR_BASE = xstrdup(optarg)) == NULL) { fprintf(stderr, "%s: xstrdup(%s) failed!\n", argv[0], optarg); exit(1); } SM_DIR = xmalloc(strlen(DIR_BASE) + 1 + sizeof("sm")); SM_BAK_DIR = xmalloc(strlen(DIR_BASE) + 1 + sizeof("sm.bak")); SM_STAT_PATH = xmalloc(strlen(DIR_BASE) + 1 + sizeof("state")); if ((SM_DIR == NULL) || (SM_BAK_DIR == NULL) || (SM_STAT_PATH == NULL)) { fprintf(stderr, "%s: xmalloc() failed!\n", argv[0]); exit(1); } if (DIR_BASE[strlen(DIR_BASE)-1] == '/') { sprintf(SM_DIR, "%ssm", DIR_BASE ); sprintf(SM_BAK_DIR, "%ssm.bak", DIR_BASE ); sprintf(SM_STAT_PATH, "%sstate", DIR_BASE ); } else { sprintf(SM_DIR, "%s/sm", DIR_BASE ); sprintf(SM_BAK_DIR, "%s/sm.bak", DIR_BASE ); sprintf(SM_STAT_PATH, "%s/state", DIR_BASE ); } break; case 'H': /* PRC: specify the ha-callout program */ if ((ha_callout_prog = xstrdup(optarg)) == NULL) { fprintf(stderr, "%s: xstrdup(%s) failed!\n", argv[0], optarg); exit(1); } break; case '?': /* heeeeeelllllllpppp? heh */ case 'h': usage(); exit (0); default: /* oh dear ... heh */ usage(); exit(-1); } } if (port == out_port && port != 0) { fprintf(stderr, "Listening and outgoing ports cannot be the same!\n"); exit(-1); } if (run_mode & MODE_NOTIFY_ONLY) { fprintf(stderr, "%s: -N deprecated, consider using /usr/sbin/sm-notify directly\n", name_p); run_sm_notify(out_port); } if (!(run_mode & MODE_NODAEMON)) { run_mode &= ~MODE_LOG_STDERR; /* Never log to console in daemon mode. */ } if (getrlimit (RLIMIT_NOFILE, &rlim) != 0) fprintf(stderr, "%s: getrlimit (RLIMIT_NOFILE) failed: %s\n", argv [0], strerror(errno)); else { /* glibc sunrpc code dies if getdtablesize > FD_SETSIZE */ if (rlim.rlim_cur > FD_SETSIZE) { rlim.rlim_cur = FD_SETSIZE; if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) { fprintf(stderr, "%s: setrlimit (RLIMIT_NOFILE) failed: %s\n", argv [0], strerror(errno)); } } } #ifdef SIMULATIONS if (argc > 1) /* LH - I _really_ need to update simulator... */ simulator (--argc, ++argv); /* simulator() does exit() */ #endif if (!(run_mode & MODE_NODAEMON)) { int tempfd; if (pipe(pipefds)<0) { perror("statd: unable to create pipe"); exit(1); } if ((pid = fork ()) < 0) { perror ("statd: Could not fork"); exit (1); } else if (pid != 0) { /* Parent. * Wait for status from child. */ close(pipefds[1]); if (read(pipefds[0], &status, 1) != 1) exit(1); exit (0); } /* Child. */ close(pipefds[0]); setsid (); if (chdir (DIR_BASE) == -1) { perror("statd: Could not chdir"); exit(1); } while (pipefds[1] <= 2) { pipefds[1] = dup(pipefds[1]); if (pipefds[1]<0) { perror("statd: dup"); exit(1); } } tempfd = open("/dev/null", O_RDWR); dup2(tempfd, 0); dup2(tempfd, 1); dup2(tempfd, 2); dup2(pipefds[1], 3); pipefds[1] = 3; closeall(4); } /* Child. */ log_init (/*name_p,version_p*/); log_modes(); signal (SIGHUP, killer); signal (SIGINT, killer); signal (SIGTERM, killer); /* PRC: trap SIGUSR1 to re-read notify list from disk */ signal(SIGUSR1, sigusr); /* WARNING: the following works on Linux and SysV, but not BSD! */ signal(SIGCHLD, SIG_IGN); /* * Ignore SIGPIPE to avoid statd dying when peers close their * TCP connection while we're trying to reply to them. */ signal(SIGPIPE, SIG_IGN); create_pidfile(); atexit(truncate_pidfile); if (! (run_mode & MODE_NO_NOTIFY)) switch (pid = fork()) { case 0: run_sm_notify(out_port); break; case -1: break; default: waitpid(pid, NULL, 0); } /* Make sure we have a privilege port for calling into the kernel */ if (statd_get_socket() < 0) exit(1); /* If sm-notify didn't take all the state files, load * state information into our notify-list so we can * pass on any SM_NOTIFY that arrives */ load_state(); load_state_number(); pmap_unset (SM_PROG, SM_VERS); /* this registers both UDP and TCP services */ rpc_init("statd", SM_PROG, SM_VERS, sm_prog_1, port); /* If we got this far, we have successfully started, so notify parent */ if (pipefds[1] > 0) { status = 0; write(pipefds[1], &status, 1); close(pipefds[1]); pipefds[1] = -1; } drop_privs(); for (;;) { /* * Handle incoming requests: SM_NOTIFY socket requests, as * well as callbacks from lockd. */ my_svc_run(); /* I rolled my own, Olaf made it better... */ /* Only get here when simulating a crash so we should probably * start sm-notify running again. As we have already dropped * privileges, this might not work, but I don't think * responding to SM_SIMU_CRASH is an important use cases to * get perfect. */ if (! (run_mode & MODE_NO_NOTIFY)) switch (pid = fork()) { case 0: run_sm_notify(out_port); break; case -1: break; default: waitpid(pid, NULL, 0); } } return 0; }
/*---------------------------------------------------------------------------*/ int main(int argc, char *const argv[]) { struct xio_server *server; /* server portal */ char url[256]; int i; struct sigaction sa; int c; char *addr = NULL; char *port = NULL; char *trans = NULL; while (1) { c = getopt_long(argc, argv, "a:p:r:hdnV", longopts, NULL); if (c == -1) break; switch (c) { case 'a': addr = optarg; break; case 'p': port = optarg; break; case 'r': trans = optarg; break; case 'h': usage(argv[0], 0); case 'd': debug_flag++; nofork_flag++; break; case 'n': nofork_flag++; break; case 'V': printf("%s\n", PACKAGE_STRING); exit(0); break; default: usage(argv[0], 1); break; } } memset(&sa, 0, sizeof(sa)); sa.sa_handler = signal_handler; sigaction(SIGINT, &sa, NULL); sigaction(SIGTERM, &sa, NULL); sigaction(SIGQUIT, &sa, NULL); sigaction(SIGHUP, &sa, NULL); sa.sa_handler = SIG_IGN; sa.sa_flags = SA_RESTART; sigaction(SIGPIPE, &sa, NULL); if (!nofork_flag && daemon(0, 0)) { logerr("daemon() failed"); exit(1); } if (!debug_flag) { openlog("xiosrvd", LOG_PID, LOG_DAEMON); use_syslog = 1; } /* Create the process PID file */ if (create_pidfile(pid_file) != 0) exit(EXIT_FAILURE); /* initialize library */ xio_init(); /* create "hello world" message */ memset(&server_data, 0, sizeof(server_data)); for (i = 0; i < QUEUE_DEPTH; i++) { server_data.rsp[i].out.header.iov_base = strdup("hello world header response"); server_data.rsp[i].out.header.iov_len = strlen((const char *) server_data.rsp[i].out.header.iov_base) + 1; } /* create thread context for the client */ server_data.ctx = xio_context_create(NULL, 0, -1); /* create url to connect to */ if (trans) sprintf(url, "%s://%s:%s", trans, addr, port); else sprintf(url, "rdma://%s:%s", addr, port); reload: /* bind a listener server to a portal/url */ server = xio_bind(server_data.ctx, &server_ops, url, NULL, 0, &server_data); if (server) { logit(LOG_INFO, "listen to %s", url); xio_context_run_loop(server_data.ctx, XIO_INFINITE); /* free the server */ xio_unbind(server); if (reload_flag) { reload_flag = 0; goto reload; } /* normal exit phase */ logit(LOG_INFO, "exit signaled"); } /* free the message */ for (i = 0; i < QUEUE_DEPTH; i++) free(server_data.rsp[i].out.header.iov_base); /* free the context */ xio_context_destroy(server_data.ctx); xio_shutdown(); remove_pidfile(); if (use_syslog) closelog(); return 0; }
int main(int argc, char **argv) { int event_fd; int sock_fd = -1; /* init to avoid a compiler warning */ /* learn who we really are */ progname = (const char *)strrchr(argv[0], '/'); progname = progname ? (progname + 1) : argv[0]; /* handle the commandline */ handle_cmdline(&argc, &argv); /* close any extra file descriptors */ close_fds(); /* actually open the event file */ event_fd = open(eventfile, O_RDONLY); if (event_fd < 0) { fprintf(stderr, "%s: can't open %s: %s\n", progname, eventfile, strerror(errno)); exit(EXIT_FAILURE); } fcntl(event_fd, F_SETFD, FD_CLOEXEC); /* * if there is data, and the kernel is NOT broken, this eats 1 byte. We * can't have that. This is ifdef'ed out on the assumption that old kernels * are out of popular use, by now. */ #ifdef TEST_FOR_BAD_KERNELS /* * Older kernels did not support read() properly or poll() at all * Check that the kernel supports the proper semantics, or die. * * Good kernels will respect O_NONBLOCK and return -1. Bad kernels * will ignore O_NONBLOCK and return 0. Really bad kernels will block * and overflow the buffer. Can't deal with the really bad ones. */ { int fl; char buf; fl = fcntl(event_fd, F_GETFL); fcntl(event_fd, F_SETFL, fl | O_NONBLOCK); if (read(event_fd, &buf, 1) == 0) { fprintf(stderr, "%s: this kernel does not support proper " "event file handling.\n" "Please get the patch from " "http://acpid.sourceforge.net\n", progname); exit(EXIT_FAILURE); } fcntl(event_fd, F_SETFL, fl); } #endif /* open our socket */ if (!nosocket) { sock_fd = ud_create_socket(socketfile); if (sock_fd < 0) { fprintf(stderr, "%s: can't open socket %s: %s\n", progname, socketfile, strerror(errno)); exit(EXIT_FAILURE); } fcntl(sock_fd, F_SETFD, FD_CLOEXEC); chmod(socketfile, socketmode); if (socketgroup) { struct group *gr; struct stat buf; gr = getgrnam(socketgroup); if (!gr) { fprintf(stderr, "%s: group %s does not exist\n", progname, socketgroup); exit(EXIT_FAILURE); } if (stat(socketfile, &buf) < 0) { fprintf(stderr, "%s: can't stat %s\n", progname, socketfile); exit(EXIT_FAILURE); } if (chown(socketfile, buf.st_uid, gr->gr_gid) < 0) { fprintf(stderr, "%s: chown(): %s\n", progname, strerror(errno)); exit(EXIT_FAILURE); } } } /* if we're running in foreground, we don't daemonize */ if (!foreground) { if (daemonize() < 0) exit(EXIT_FAILURE); } /* open the log */ if (open_log() < 0) { exit(EXIT_FAILURE); } acpid_log(LOG_INFO, "starting up\n"); /* trap key signals */ signal(SIGHUP, reload_conf); signal(SIGINT, clean_exit); signal(SIGQUIT, clean_exit); signal(SIGTERM, clean_exit); signal(SIGPIPE, SIG_IGN); /* read in our configuration */ if (acpid_read_conf(confdir)) { exit(EXIT_FAILURE); } /* create our pidfile */ if (create_pidfile() < 0) { exit(EXIT_FAILURE); } /* main loop */ acpid_log(LOG_INFO, "waiting for events: event logging is %s\n", logevents ? "on" : "off"); while (1) { struct pollfd ar[2]; int r; int fds = 0; /* poll for the socket and the event file */ ar[0].fd = event_fd; ar[0].events = POLLIN; fds++; if (!nosocket) { ar[1].fd = sock_fd; ar[1].events = POLLIN; fds++; } r = poll(ar, fds, -1); if (r < 0 && errno == EINTR) { continue; } else if (r < 0) { acpid_log(LOG_ERR, "poll(): %s\n", strerror(errno)); continue; } /* house keeping */ acpid_close_dead_clients(); /* was it an event? */ if (ar[0].revents) { char *event; struct stat trash; int fexists; /* check for existence of a lockfile */ fexists = (stat(lockfile, &trash) == 0); /* this shouldn't happen */ if (!ar[0].revents & POLLIN) { acpid_log(LOG_DEBUG, "odd, poll set flags 0x%x\n", ar[0].revents); continue; } /* read an event */ event = read_line(event_fd); /* if we're locked, don't process the event */ if (fexists) { if (logevents) { acpid_log(LOG_INFO, "lockfile present, not processing " "event \"%s\"\n", event); } continue; } /* handle the event */ if (event) { if (logevents) { acpid_log(LOG_INFO, "received event \"%s\"\n", event); } acpid_handle_event(event); if (logevents) { acpid_log(LOG_INFO, "completed event \"%s\"\n", event); } } else if (errno == EPIPE) { acpid_log(LOG_WARNING, "events file connection closed\n"); break; } else { static int nerrs; if (++nerrs >= ACPID_MAX_ERRS) { acpid_log(LOG_ERR, "too many errors reading " "events file - aborting\n"); break; } } } /* was it a new connection? */ if (!nosocket && ar[1].revents) { int cli_fd; struct ucred creds; char buf[32]; static int accept_errors; /* this shouldn't happen */ if (!ar[1].revents & POLLIN) { acpid_log(LOG_DEBUG, "odd, poll set flags 0x%x\n", ar[1].revents); continue; } /* accept and add to our lists */ cli_fd = ud_accept(sock_fd, &creds); if (cli_fd < 0) { acpid_log(LOG_ERR, "can't accept client: %s\n", strerror(errno)); accept_errors++; if (accept_errors >= 5) { acpid_log(LOG_ERR, "giving up\n"); clean_exit_with_status(EXIT_FAILURE); } continue; } accept_errors = 0; if (creds.uid != 0 && non_root_clients >= clientmax) { close(cli_fd); acpid_log(LOG_ERR, "too many non-root clients\n"); continue; } if (creds.uid != 0) { non_root_clients++; } fcntl(cli_fd, F_SETFD, FD_CLOEXEC); snprintf(buf, sizeof(buf)-1, "%d[%d:%d]", creds.pid, creds.uid, creds.gid); acpid_add_client(cli_fd, buf); } } clean_exit_with_status(EXIT_SUCCESS); return 0; }