static void glue_daemonize_init(const struct options *options) { daemonize_init(config_get_string(CONF_USER, NULL), config_get_string(CONF_GROUP, NULL), config_get_path(CONF_PID_FILE)); if (options->kill) daemonize_kill(); }
static bool glue_daemonize_init(const struct options *options, GError **error_r) { GError *error = NULL; char *pid_file = config_dup_path(CONF_PID_FILE, &error); if (pid_file == NULL && error != NULL) { g_propagate_error(error_r, error); return false; } daemonize_init(config_get_string(CONF_USER, NULL), config_get_string(CONF_GROUP, NULL), pid_file); g_free(pid_file); if (options->kill) daemonize_kill(); return true; }
int main(int ac, char *av[]) { char *dir = "/"; int allflag = 0; int df_allflag = 0; int opt_cnt = 0; int maxservers = 1; /* zero allows inifinte number of threads */ int maxservers_set = 0; int logmaxservers = 0; int pid; int i; char *provider = (char *)NULL; char *df_provider = (char *)NULL; struct protob *protobp0, *protobp; NETSELDECL(proto) = NULL; NETSELDECL(df_proto) = NULL; NETSELPDECL(providerp); char *defval; boolean_t can_do_mlp; uint_t dss_npaths = 0; char **dss_pathnames = NULL; sigset_t sgset; char name[PATH_MAX], value[PATH_MAX]; int ret, bufsz; int pipe_fd = -1; MyName = *av; /* * Initializations that require more privileges than we need to run. */ (void) _create_daemon_lock(NFSD, DAEMON_UID, DAEMON_GID); svcsetprio(); can_do_mlp = priv_ineffect(PRIV_NET_BINDMLP); if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET, DAEMON_UID, DAEMON_GID, PRIV_SYS_NFS, can_do_mlp ? PRIV_NET_BINDMLP : NULL, NULL) == -1) { (void) fprintf(stderr, "%s should be run with" " sufficient privileges\n", av[0]); exit(1); } (void) enable_extended_FILE_stdio(-1, -1); /* * Read in the values from SMF first before we check * command line options so the options override SMF values. */ bufsz = PATH_MAX; ret = nfs_smf_get_prop("max_connections", value, DEFAULT_INSTANCE, SCF_TYPE_INTEGER, NFSD, &bufsz); if (ret == SA_OK) { errno = 0; max_conns_allowed = strtol(value, (char **)NULL, 10); if (errno != 0) max_conns_allowed = -1; } bufsz = PATH_MAX; ret = nfs_smf_get_prop("listen_backlog", value, DEFAULT_INSTANCE, SCF_TYPE_INTEGER, NFSD, &bufsz); if (ret == SA_OK) { errno = 0; listen_backlog = strtol(value, (char **)NULL, 10); if (errno != 0) { listen_backlog = 32; } } bufsz = PATH_MAX; ret = nfs_smf_get_prop("protocol", value, DEFAULT_INSTANCE, SCF_TYPE_ASTRING, NFSD, &bufsz); if ((ret == SA_OK) && strlen(value) > 0) { df_proto = strdup(value); opt_cnt++; if (strncasecmp("ALL", value, 3) == 0) { free(df_proto); df_proto = NULL; df_allflag = 1; } } bufsz = PATH_MAX; ret = nfs_smf_get_prop("device", value, DEFAULT_INSTANCE, SCF_TYPE_ASTRING, NFSD, &bufsz); if ((ret == SA_OK) && strlen(value) > 0) { df_provider = strdup(value); opt_cnt++; } bufsz = PATH_MAX; ret = nfs_smf_get_prop("servers", value, DEFAULT_INSTANCE, SCF_TYPE_INTEGER, NFSD, &bufsz); if (ret == SA_OK) { errno = 0; maxservers = strtol(value, (char **)NULL, 10); if (errno != 0) maxservers = 1; else maxservers_set = 1; } bufsz = 4; ret = nfs_smf_get_prop("server_versmin", value, DEFAULT_INSTANCE, SCF_TYPE_INTEGER, NFSD, &bufsz); if (ret == SA_OK) nfs_server_vers_min = strtol(value, (char **)NULL, 10); bufsz = 4; ret = nfs_smf_get_prop("server_versmax", value, DEFAULT_INSTANCE, SCF_TYPE_INTEGER, NFSD, &bufsz); if (ret == SA_OK) nfs_server_vers_max = strtol(value, (char **)NULL, 10); bufsz = PATH_MAX; ret = nfs_smf_get_prop("server_delegation", value, DEFAULT_INSTANCE, SCF_TYPE_ASTRING, NFSD, &bufsz); if (ret == SA_OK) if (strncasecmp(value, "off", 3) == 0) nfs_server_delegation = FALSE; /* * Conflict options error messages. */ if (opt_cnt > 1) { (void) fprintf(stderr, "\nConflicting options, only one of " "the following options can be specified\n" "in SMF:\n" "\tprotocol=ALL\n" "\tprotocol=protocol\n" "\tdevice=devicename\n\n"); usage(); } opt_cnt = 0; while ((i = getopt(ac, av, "ac:p:s:t:l:")) != EOF) { switch (i) { case 'a': free(df_proto); df_proto = NULL; free(df_provider); df_provider = NULL; allflag = 1; opt_cnt++; break; case 'c': max_conns_allowed = atoi(optarg); break; case 'p': proto = optarg; df_allflag = 0; opt_cnt++; break; /* * DSS: NFSv4 distributed stable storage. * * This is a Contracted Project Private interface, for * the sole use of Sun Cluster HA-NFS. See PSARC/2006/313. */ case 's': if (strlen(optarg) < MAXPATHLEN) { /* first "-s" option encountered? */ if (dss_pathnames == NULL) { /* * Allocate maximum possible space * required given cmdline arg count; * "-s <path>" consumes two args. */ size_t sz = (ac / 2) * sizeof (char *); dss_pathnames = (char **)malloc(sz); if (dss_pathnames == NULL) { (void) fprintf(stderr, "%s: " "dss paths malloc failed\n", av[0]); exit(1); } (void) memset(dss_pathnames, 0, sz); } dss_pathnames[dss_npaths] = optarg; dss_npaths++; } else { (void) fprintf(stderr, "%s: -s pathname too long.\n", av[0]); } break; case 't': provider = optarg; df_allflag = 0; opt_cnt++; break; case 'l': listen_backlog = atoi(optarg); break; case '?': usage(); /* NOTREACHED */ } } allflag = df_allflag; if (proto == NULL) proto = df_proto; if (provider == NULL) provider = df_provider; /* * Conflict options error messages. */ if (opt_cnt > 1) { (void) fprintf(stderr, "\nConflicting options, only one of " "the following options can be specified\n" "on the command line:\n" "\t-a\n" "\t-p protocol\n" "\t-t transport\n\n"); usage(); } if (proto != NULL && strncasecmp(proto, NC_UDP, strlen(NC_UDP)) == 0) { if (nfs_server_vers_max == NFS_V4) { if (nfs_server_vers_min == NFS_V4) { fprintf(stderr, "NFS version 4 is not supported " "with the UDP protocol. Exiting\n"); exit(3); } else { fprintf(stderr, "NFS version 4 is not supported " "with the UDP protocol.\n"); } } } /* * If there is exactly one more argument, it is the number of * servers. */ if (optind == ac - 1) { maxservers = atoi(av[optind]); maxservers_set = 1; } /* * If there are two or more arguments, then this is a usage error. */ else if (optind < ac - 1) usage(); /* * Check the ranges for min/max version specified */ else if ((nfs_server_vers_min > nfs_server_vers_max) || (nfs_server_vers_min < NFS_VERSMIN) || (nfs_server_vers_max > NFS_VERSMAX)) usage(); /* * There are no additional arguments, and we haven't set maxservers * explicitly via the config file, we use a default number of * servers. We will log this. */ else if (maxservers_set == 0) logmaxservers = 1; /* * Basic Sanity checks on options * * max_conns_allowed must be positive, except for the special * value of -1 which is used internally to mean unlimited, -1 isn't * documented but we allow it anyway. * * maxservers must be positive * listen_backlog must be positive or zero */ if (((max_conns_allowed != -1) && (max_conns_allowed <= 0)) || (listen_backlog < 0) || (maxservers <= 0)) { usage(); } /* * Set current dir to server root */ if (chdir(dir) < 0) { (void) fprintf(stderr, "%s: ", MyName); perror(dir); exit(1); } #ifndef DEBUG pipe_fd = daemonize_init(); #endif openlog(MyName, LOG_PID | LOG_NDELAY, LOG_DAEMON); /* * establish our lock on the lock file and write our pid to it. * exit if some other process holds the lock, or if there's any * error in writing/locking the file. */ pid = _enter_daemon_lock(NFSD); switch (pid) { case 0: break; case -1: fprintf(stderr, "error locking for %s: %s\n", NFSD, strerror(errno)); exit(2); default: /* daemon was already running */ exit(0); } /* * If we've been given a list of paths to be used for distributed * stable storage, and provided we're going to run a version * that supports it, setup the DSS paths. */ if (dss_pathnames != NULL && nfs_server_vers_max >= DSS_VERSMIN) { if (dss_init(dss_npaths, dss_pathnames) != 0) { fprintf(stderr, "%s", "dss_init failed. Exiting.\n"); exit(1); } } /* * Block all signals till we spawn other * threads. */ (void) sigfillset(&sgset); (void) thr_sigsetmask(SIG_BLOCK, &sgset, NULL); if (logmaxservers) { fprintf(stderr, "Number of servers not specified. Using default of %d.\n", maxservers); } /* * Make sure to unregister any previous versions in case the * user is reconfiguring the server in interesting ways. */ svc_unreg(NFS_PROGRAM, NFS_VERSION); svc_unreg(NFS_PROGRAM, NFS_V3); svc_unreg(NFS_PROGRAM, NFS_V4); svc_unreg(NFS_ACL_PROGRAM, NFS_ACL_V2); svc_unreg(NFS_ACL_PROGRAM, NFS_ACL_V3); /* * Set up kernel RPC thread pool for the NFS server. */ if (nfssvcpool(maxservers)) { fprintf(stderr, "Can't set up kernel NFS service: %s. " "Exiting.\n", strerror(errno)); exit(1); } /* * Set up blocked thread to do LWP creation on behalf of the kernel. */ if (svcwait(NFS_SVCPOOL_ID)) { fprintf(stderr, "Can't set up NFS pool creator: %s. Exiting.\n", strerror(errno)); exit(1); } /* * RDMA start and stop thread. * Per pool RDMA listener creation and * destructor thread. * * start rdma services and block in the kernel. * (only if proto or provider is not set to TCP or UDP) */ if ((proto == NULL) && (provider == NULL)) { if (svcrdma(NFS_SVCPOOL_ID, nfs_server_vers_min, nfs_server_vers_max, nfs_server_delegation)) { fprintf(stderr, "Can't set up RDMA creator thread : %s\n", strerror(errno)); } } /* * Now open up for signal delivery */ (void) thr_sigsetmask(SIG_UNBLOCK, &sgset, NULL); sigset(SIGTERM, sigflush); sigset(SIGUSR1, quiesce); /* * Build a protocol block list for registration. */ protobp0 = protobp = (struct protob *)malloc(sizeof (struct protob)); protobp->serv = "NFS"; protobp->versmin = nfs_server_vers_min; protobp->versmax = nfs_server_vers_max; protobp->program = NFS_PROGRAM; protobp->next = (struct protob *)malloc(sizeof (struct protob)); protobp = protobp->next; protobp->serv = "NFS_ACL"; /* not used */ protobp->versmin = nfs_server_vers_min; /* XXX - this needs work to get the version just right */ protobp->versmax = (nfs_server_vers_max > NFS_ACL_V3) ? NFS_ACL_V3 : nfs_server_vers_max; protobp->program = NFS_ACL_PROGRAM; protobp->next = (struct protob *)NULL; if (allflag) { if (do_all(protobp0, nfssvc) == -1) { fprintf(stderr, "setnetconfig failed : %s\n", strerror(errno)); exit(1); } } else if (proto) { /* there's more than one match for the same protocol */ struct netconfig *nconf; NCONF_HANDLE *nc; bool_t protoFound = FALSE; if ((nc = setnetconfig()) == (NCONF_HANDLE *) NULL) { fprintf(stderr, "setnetconfig failed : %s\n", strerror(errno)); goto done; } while (nconf = getnetconfig(nc)) { if (strcmp(nconf->nc_proto, proto) == 0) { protoFound = TRUE; do_one(nconf->nc_device, NULL, protobp0, nfssvc); } } (void) endnetconfig(nc); if (protoFound == FALSE) { fprintf(stderr, "couldn't find netconfig entry for protocol %s\n", proto); } } else if (provider) do_one(provider, proto, protobp0, nfssvc); else { for (providerp = defaultproviders; *providerp != NULL; providerp++) { provider = *providerp; do_one(provider, NULL, protobp0, nfssvc); } } done: free(protobp); free(protobp0); if (num_fds == 0) { fprintf(stderr, "Could not start NFS service for any protocol." " Exiting.\n"); exit(1); } end_listen_fds = num_fds; /* * nfsd is up and running as far as we are concerned. */ daemonize_fini(pipe_fd); /* * Get rid of unneeded privileges. */ __fini_daemon_priv(PRIV_PROC_FORK, PRIV_PROC_EXEC, PRIV_PROC_SESSION, PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL); /* * Poll for non-data control events on the transport descriptors. */ poll_for_action(); /* * If we get here, something failed in poll_for_action(). */ return (1); }
int main(int argc, char **argv) { daemonize_close_stdin(); parse_cmdline(argc, argv); if (!file_read_config()) g_error("cannot read configuration file\n"); log_init(file_config.log, file_config.verbose); daemonize_init(file_config.daemon_user, file_config.pidfile); if (!file_config.no_daemon) daemonize_detach(); daemonize_write_pidfile(); daemonize_set_user(); #ifndef NDEBUG if (!file_config.no_daemon) #endif daemonize_close_stdout_stderr(); main_loop = g_main_loop_new(NULL, FALSE); lmc_connect(file_config.host, file_config.port); http_client_init(); as_init(file_config.scrobblers); setup_signals(); timer = g_timer_new(); /* set up timeouts */ save_source_id = g_timeout_add_seconds(file_config.journal_interval, timer_save_journal, NULL); /* run the main loop */ g_main_loop_run(main_loop); /* cleanup */ g_message("shutting down\n"); g_source_remove(save_source_id); g_main_loop_unref(main_loop); g_timer_destroy(timer); as_save_cache(); as_cleanup(); http_client_finish(); lmc_disconnect(); file_cleanup(); log_deinit(); daemonize_finish(); return 0; }
int main(int argc, char* argv[]) { int oc; /*To parse the options. The options from command line will overwrite those in configuration file(because -c <config_file> is the first option) */ // while( ( oc=getopt(argc, argv, "b:dg:hi:l:L:n:op:s:t:")) != -1 ) while( ( oc=getopt(argc, argv, "c:dhl:p:")) != -1 ) { switch (oc) { case 'c': config_file=strdup(optarg); config.file_config = config_file; break; /* case 'b': // blacklist file config.file_blacklist=strdup(optarg); break; */ case 'd': //daemonize config.daemonize=TRUE; break; case 'h': usage(argv[0]); break; /* case 'i': //IP address file used by censors config.file_badip = strdup(optarg); break; */ case 'l': //log file config.file_log = strdup(optarg); break; case 'p': //pid file config.file_pid = strdup(optarg); break; default: usage(argv[0]); break; }// switch(oc) }//end of while (parse the options) if(config.file_config == NULL) { config.file_config = DEFAULT_CONFIG; } if( load_config(config.file_config, &config)<0 ) //The values can be overwrite by the following options { fprintf(stderr, "ERROR: unable to load configuration file: %s.\n", config.file_config); exit(-1); } if(config.daemonize) daemonize_init(); /*set default log level to ERROR(2) */ log_level= (config.log_level == 0? LOG_ERROR: config.log_level); /* if (config.open_resolver == NULL ) config.open_resolver = "8.8.8.8"; if (config.open_resolver_port == 0 ) config.open_resolver_port = 53; if (config.local_resolver == NULL ) config.open_resolver = "127.0.0.1"; if (config.local_resolver_port == 0 ) config.local_resolver_port = 53; */ if( config.file_log == NULL) { fd_log = stderr; } else { fd_log = fopen(config.file_log, "a"); if( fd_log ==NULL) { fprintf(stderr, "ERROR: unable to open log file: %s to write(append).\n", config.file_log); exit(-1); } } if(config.file_pid != NULL) { FILE * pid_fd=fopen(config.file_pid,"w"); // open again if (pid_fd == NULL) { my_log(LOG_ERROR, "E: Unable to open pid_file:%s to write\n", config.file_pid); exit(-1); } fprintf(pid_fd, "%d" , getpid()); fclose(pid_fd); } blacklist = blacklist_load(config.file_blacklist, 1); if( blacklist == NULL) { my_log(LOG_ERROR, "E: unable to load Blacklist %s.\n", config.file_blacklist); exit(-1); } //blacklist_dump(blacklist, config.file_blacklist); //printf("OK\n"); //blacklist_release(blacklist); //exit(0); badip=badip_load(config.file_badip); if(badip== NULL) { my_log(LOG_ERROR, "E: unable to load bad IP address %s.\n", config.file_badip); exit(-1); } if( measure_ttl(OPEN_RESOLVER, config.open_resolver, config.open_resolver_port, "www.mit.edu", &goodTTL[OPEN_RESOLVER]) <0) { my_log(LOG_ERROR, "E: open_resolver test failed: %s\n", config.open_resolver); exit(-1); } if( measure_ttl(LOCAL_RESOLVER, config.local_resolver, config.local_resolver_port,"www.mit.edu", &goodTTL[LOCAL_RESOLVER]) <0) { my_log(LOG_ERROR, "E: local_resolver test failed: %s\n", config.local_resolver); exit(-1); } my_log(LOG_MEASURE, "M: M_TTL: %d %d\n", goodTTL[OPEN_RESOLVER], goodTTL[LOCAL_RESOLVER]); pthread_t tid_measure_handler; int rcode=pthread_create(&tid_measure_handler,NULL, measure_handler, NULL ); if( rcode !=0) { my_log(LOG_ERROR, "E: Cannot create thread for tcp_request_handler:rcode=%d in %s(%d).\n", rcode, __FILE__, __LINE__); exit(-1); } forwarder(config.service_port, config.open_resolver, config.open_resolver_port, config.local_resolver, config.local_resolver_port, 1); //set_resolver_to_me() //create_report_service() pthread_join( tid_measure_handler, NULL) ; return 0; }
int main (int argc, char *argv[]) { int fd = -1; char *log_identity = argv[0]; int log_priority = LOG_INFO; int log_options = LOG_OPT_PRIORITY; #ifndef NDEBUG log_priority = LOG_DEBUG; log_options |= LOG_OPT_TIMESTAMP; #endif /* NDEBUG */ log_open_file (stderr, log_identity, log_priority, log_options); disable_core_dumps (); conf = create_conf (); parse_cmdline (conf, argc, argv); auth_recv_init (conf->auth_server_dir, conf->auth_client_dir, conf->got_force); if (!conf->got_foreground) { fd = daemonize_init (argv[0]); if (conf->got_syslog) { log_open_file (NULL, NULL, 0, 0); log_open_syslog (log_identity, LOG_DAEMON); } else { open_logfile (conf->logfile_name, log_priority, conf->got_force); } } handle_signals (); lookup_ip_addr (conf); write_pidfile (conf->pidfile_name, conf->got_force); if (conf->got_mlockall) { lock_memory (); } crypto_init (); if (random_init (conf->seed_name) < 0) { if (conf->seed_name) { free (conf->seed_name); conf->seed_name = NULL; } } create_subkeys (conf); conf->gids = gids_create (conf->gids_update_secs, conf->got_group_stat); replay_init (); timer_init (); sock_create (conf); if (!conf->got_foreground) { daemonize_fini (fd); } log_msg (LOG_NOTICE, "Starting %s daemon (pid %d)", META_ALIAS, (int) getpid ()); job_accept (conf); sock_destroy (conf); timer_fini (); replay_fini (); gids_destroy (conf->gids); random_fini (conf->seed_name); crypto_fini (); destroy_conf (conf); log_msg (LOG_NOTICE, "Stopping %s daemon (pid %d)", META_ALIAS, (int) getpid ()); exit (EMUNGE_SUCCESS); }