int main(int argc, char **argv) { #if ! defined(_WIN32) __log_error = (void (*)(void *, const char *,...)) log_server; /*set c-icap library log function */ #else __vlog_error = vlog_server; /*set c-icap library log function */ #endif mem_init(); init_internal_lookup_tables(); ci_acl_init(); init_http_auth(); if (init_body_system() != CI_OK) { ci_debug_printf(1, "Can not initialize body system\n"); exit(-1); } ci_txt_template_init(); ci_txt_template_set_dir(DATADIR"templates"); commands_init(); if (!(CI_CONF.MAGIC_DB = ci_magic_db_load(CI_CONF.magics_file))) { ci_debug_printf(1, "Can not load magic file %s!!!\n", CI_CONF.magics_file); } init_conf_tables(); request_stats_init(); init_modules(); init_services(); config(argc, argv); compute_my_hostname(); ci_debug_printf(2, "My hostname is:%s\n", MY_HOSTNAME); if (!log_open()) { ci_debug_printf(1, "Can not init loggers. Exiting.....\n"); exit(-1); } #if ! defined(_WIN32) if (is_icap_running(CI_CONF.PIDFILE)) { ci_debug_printf(1, "c-icap server already running!\n"); exit(-1); } if (DAEMON_MODE) run_as_daemon(); if (!set_running_permissions(CI_CONF.RUN_USER, CI_CONF.RUN_GROUP)) exit(-1); store_pid(CI_CONF.PIDFILE); #endif if (!init_server(CI_CONF.ADDRESS, CI_CONF.PORT, &(CI_CONF.PROTOCOL_FAMILY))) return -1; post_init_modules(); post_init_services(); start_server(); clear_pid(CI_CONF.PIDFILE); return 0; }
/* * Run as background process */ static void fsstatd_daemonize(void) { pid_t pid; /* Fork off the parent process */ pid = fork(); if (pid < 0) { syslog(LOG_ERR, "fork error: %s\n", strerror(errno)); exit(1); } if (pid > 0) exit(0); /* Change the file mode mask */ umask(0); /* Store daemon pid */ store_pid(); /* Catch SIGINT and SIGTERM to clean up pid file on exit */ fsstatd_handle_signals(); /* Create a new SID for the child process */ if (setsid() < 0) { syslog(LOG_ERR, "setsid error: %s\n", strerror(errno)); exit(1); } /* Change the current working directory */ if (chdir("/") < 0) { syslog(LOG_ERR, "chdir error: %s\n", strerror(errno)); exit(1); } /* Close out the standard file descriptors */ close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); }
int main(int argc, char **argv) { struct option long_options[] = { { "gpio", 1, 0, 0 }, { "address", 1, 0, 0 }, { "command", 1, 0, 0 }, { NULL, 0, 0, 0 } }; unsigned char key; unsigned short address = 0; unsigned short code = 0; long int a2i; int gpio = -1, i, c; char command = UNKNOWN; char *progname, *end; if (setuid(0)) { perror("setuid"); return -1; } while (1) { c = getopt_long(argc, argv, "", long_options, &i); if (c == -1) break; switch (c) { case 0: if (strcmp(long_options[i].name, "gpio") == 0) { a2i = strtol(optarg, &end, 10); if (errno == ERANGE && (a2i == LONG_MAX || a2i == LONG_MIN)) { break; } gpio = a2i; } else if (strcmp(long_options[i].name, "address") == 0) { a2i = strtol(optarg, &end, 10); if (errno == ERANGE && (a2i == LONG_MAX || a2i == LONG_MIN)) { break; } address = a2i; } else if (strcmp(long_options[i].name, "command") == 0) { command = get_command_char(optarg); } break; default: usage(argv[0]); } } if (command == UNKNOWN || address == 0 || gpio == -1) { usage(argv[0]); } // store pid and lock it store_pid(); srand(time(NULL)); key = rand() % 255; if (wiringPiSetup() == -1) { fprintf(stderr, "Wiring Pi not installed"); return -1; } openlog("srts", LOG_PID | LOG_CONS, LOG_USER); progname = basename(argv[0]); code = get_next_code(progname, address); syslog(LOG_INFO, "remote: %d, command: %d, code: %d\n", address, command, code); closelog(); piHiPri (99); pinMode(gpio, OUTPUT); srts_transmit(gpio, key, address, command, code, 0); c = 7; if (command == PROG) { c = 20; } for (i = 0; i < c; i++) { srts_transmit(gpio, key, address, command, code, 1); } store_code(progname, address, code); return 0; }
int main(int argc, char **argv) { int listen_port = 0; struct string_list listen_addr = STRING_LIST_INIT_NODUP; int serve_mode = 0, inetd_mode = 0; const char *pid_file = NULL, *user_name = NULL, *group_name = NULL; int detach = 0; struct credentials *cred = NULL; int i; git_setup_gettext(); git_extract_argv0_path(argv[0]); for (i = 1; i < argc; i++) { char *arg = argv[i]; if (!prefixcmp(arg, "--listen=")) { string_list_append(&listen_addr, xstrdup_tolower(arg + 9)); continue; } if (!prefixcmp(arg, "--port=")) { char *end; unsigned long n; n = strtoul(arg+7, &end, 0); if (arg[7] && !*end) { listen_port = n; continue; } } if (!strcmp(arg, "--serve")) { serve_mode = 1; continue; } if (!strcmp(arg, "--inetd")) { inetd_mode = 1; log_syslog = 1; continue; } if (!strcmp(arg, "--verbose")) { verbose = 1; continue; } if (!strcmp(arg, "--syslog")) { log_syslog = 1; continue; } if (!strcmp(arg, "--export-all")) { export_all_trees = 1; continue; } if (!prefixcmp(arg, "--access-hook=")) { access_hook = arg + 14; continue; } if (!prefixcmp(arg, "--timeout=")) { timeout = atoi(arg+10); continue; } if (!prefixcmp(arg, "--init-timeout=")) { init_timeout = atoi(arg+15); continue; } if (!prefixcmp(arg, "--max-connections=")) { max_connections = atoi(arg+18); if (max_connections < 0) max_connections = 0; /* unlimited */ continue; } if (!strcmp(arg, "--strict-paths")) { strict_paths = 1; continue; } if (!prefixcmp(arg, "--base-path=")) { base_path = arg+12; continue; } if (!strcmp(arg, "--base-path-relaxed")) { base_path_relaxed = 1; continue; } if (!prefixcmp(arg, "--interpolated-path=")) { interpolated_path = arg+20; continue; } if (!strcmp(arg, "--reuseaddr")) { reuseaddr = 1; continue; } if (!strcmp(arg, "--user-path")) { user_path = ""; continue; } if (!prefixcmp(arg, "--user-path=")) { user_path = arg + 12; continue; } if (!prefixcmp(arg, "--pid-file=")) { pid_file = arg + 11; continue; } if (!strcmp(arg, "--detach")) { detach = 1; log_syslog = 1; continue; } if (!prefixcmp(arg, "--user="******"--group=")) { group_name = arg + 8; continue; } if (!prefixcmp(arg, "--enable=")) { enable_service(arg + 9, 1); continue; } if (!prefixcmp(arg, "--disable=")) { enable_service(arg + 10, 0); continue; } if (!prefixcmp(arg, "--allow-override=")) { make_service_overridable(arg + 17, 1); continue; } if (!prefixcmp(arg, "--forbid-override=")) { make_service_overridable(arg + 18, 0); continue; } if (!prefixcmp(arg, "--informative-errors")) { informative_errors = 1; continue; } if (!prefixcmp(arg, "--no-informative-errors")) { informative_errors = 0; continue; } if (!strcmp(arg, "--")) { ok_paths = &argv[i+1]; break; } else if (arg[0] != '-') { ok_paths = &argv[i]; break; } usage(daemon_usage); } if (log_syslog) { openlog("git-daemon", LOG_PID, LOG_DAEMON); set_die_routine(daemon_die); } else /* avoid splitting a message in the middle */ setvbuf(stderr, NULL, _IOFBF, 4096); if (inetd_mode && (detach || group_name || user_name)) die("--detach, --user and --group are incompatible with --inetd"); if (inetd_mode && (listen_port || (listen_addr.nr > 0))) die("--listen= and --port= are incompatible with --inetd"); else if (listen_port == 0) listen_port = DEFAULT_GIT_PORT; if (group_name && !user_name) die("--group supplied without --user"); if (user_name) cred = prepare_credentials(user_name, group_name); if (strict_paths && (!ok_paths || !*ok_paths)) die("option --strict-paths requires a whitelist"); if (base_path && !is_directory(base_path)) die("base-path '%s' does not exist or is not a directory", base_path); if (inetd_mode) { if (!freopen("/dev/null", "w", stderr)) die_errno("failed to redirect stderr to /dev/null"); } if (inetd_mode || serve_mode) return execute(); if (detach) daemonize(); else sanitize_stdfds(); if (pid_file) store_pid(pid_file); /* prepare argv for serving-processes */ cld_argv = xmalloc(sizeof (char *) * (argc + 2)); cld_argv[0] = argv[0]; /* git-daemon */ cld_argv[1] = "--serve"; for (i = 1; i < argc; ++i) cld_argv[i+1] = argv[i]; cld_argv[argc+1] = NULL; return serve(&listen_addr, listen_port, cred); }
int main(int argc, char **argv) { int listen_port = 0; struct string_list listen_addr = STRING_LIST_INIT_NODUP; int inetd_mode = 0; const char *pid_file = NULL, *user_name = NULL, *group_name = NULL; int detach = 0; struct passwd *pass = NULL; struct group *group; gid_t gid = 0; int i; git_extract_argv0_path(argv[0]); for (i = 1; i < argc; i++) { char *arg = argv[i]; if (!prefixcmp(arg, "--listen=")) { string_list_append(&listen_addr, xstrdup_tolower(arg + 9)); continue; } if (!prefixcmp(arg, "--port=")) { char *end; unsigned long n; n = strtoul(arg+7, &end, 0); if (arg[7] && !*end) { listen_port = n; continue; } } if (!strcmp(arg, "--inetd")) { inetd_mode = 1; log_syslog = 1; continue; } if (!strcmp(arg, "--verbose")) { verbose = 1; continue; } if (!strcmp(arg, "--syslog")) { log_syslog = 1; continue; } if (!strcmp(arg, "--export-all")) { export_all_trees = 1; continue; } if (!prefixcmp(arg, "--timeout=")) { timeout = atoi(arg+10); continue; } if (!prefixcmp(arg, "--init-timeout=")) { init_timeout = atoi(arg+15); continue; } if (!prefixcmp(arg, "--max-connections=")) { max_connections = atoi(arg+18); if (max_connections < 0) max_connections = 0; /* unlimited */ continue; } if (!strcmp(arg, "--strict-paths")) { strict_paths = 1; continue; } if (!prefixcmp(arg, "--base-path=")) { base_path = arg+12; continue; } if (!strcmp(arg, "--base-path-relaxed")) { base_path_relaxed = 1; continue; } if (!prefixcmp(arg, "--interpolated-path=")) { interpolated_path = arg+20; continue; } if (!strcmp(arg, "--reuseaddr")) { reuseaddr = 1; continue; } if (!strcmp(arg, "--user-path")) { user_path = ""; continue; } if (!prefixcmp(arg, "--user-path=")) { user_path = arg + 12; continue; } if (!prefixcmp(arg, "--pid-file=")) { pid_file = arg + 11; continue; } if (!strcmp(arg, "--detach")) { detach = 1; log_syslog = 1; continue; } if (!prefixcmp(arg, "--user="******"--group=")) { group_name = arg + 8; continue; } if (!prefixcmp(arg, "--enable=")) { enable_service(arg + 9, 1); continue; } if (!prefixcmp(arg, "--disable=")) { enable_service(arg + 10, 0); continue; } if (!prefixcmp(arg, "--allow-override=")) { make_service_overridable(arg + 17, 1); continue; } if (!prefixcmp(arg, "--forbid-override=")) { make_service_overridable(arg + 18, 0); continue; } if (!strcmp(arg, "--")) { ok_paths = &argv[i+1]; break; } else if (arg[0] != '-') { ok_paths = &argv[i]; break; } usage(daemon_usage); } if (log_syslog) { openlog("git-daemon", LOG_PID, LOG_DAEMON); set_die_routine(daemon_die); } else /* avoid splitting a message in the middle */ setvbuf(stderr, NULL, _IOLBF, 0); if (inetd_mode && (group_name || user_name)) die("--user and --group are incompatible with --inetd"); if (inetd_mode && (listen_port || (listen_addr.nr > 0))) die("--listen= and --port= are incompatible with --inetd"); else if (listen_port == 0) listen_port = DEFAULT_GIT_PORT; if (group_name && !user_name) die("--group supplied without --user"); if (user_name) { pass = getpwnam(user_name); if (!pass) die("user not found - %s", user_name); if (!group_name) gid = pass->pw_gid; else { group = getgrnam(group_name); if (!group) die("group not found - %s", group_name); gid = group->gr_gid; } } if (strict_paths && (!ok_paths || !*ok_paths)) die("option --strict-paths requires a whitelist"); if (base_path && !is_directory(base_path)) die("base-path '%s' does not exist or is not a directory", base_path); if (inetd_mode) { struct sockaddr_storage ss; struct sockaddr *peer = (struct sockaddr *)&ss; socklen_t slen = sizeof(ss); if (!freopen("/dev/null", "w", stderr)) die_errno("failed to redirect stderr to /dev/null"); if (getpeername(0, peer, &slen)) peer = NULL; return execute(peer); } if (detach) { daemonize(); loginfo("Ready to rumble"); } else sanitize_stdfds(); if (pid_file) store_pid(pid_file); return serve(&listen_addr, listen_port, pass, gid); }
int main(int argc, char** argv) { struct option long_options[] = { { "gpio", 1, 0, 0 }, { "address", 1, 0, 0 }, { "command", 1, 0, 0 }, { "receiver", 1, 0, 0 }, { "retry", 1, 0, 0 }, { NULL, 0, 0, 0 } }; unsigned int address = 0; unsigned char receiver = 1; long int a2i; int gpio = -1; char command = UNKNOWN; char *end; int retry = 5, i, c; if (setuid(0)) { perror("setuid"); return -1; } while (1) { c = getopt_long(argc, argv, "", long_options, &i); if (c == -1) break; switch (c) { case 0: if (strcmp(long_options[i].name, "gpio") == 0) { a2i = strtol(optarg, &end, 10); if (errno == ERANGE && (a2i == LONG_MAX || a2i == LONG_MIN)) { break; } gpio = a2i; } else if (strcmp(long_options[i].name, "address") == 0) { a2i = strtol(optarg, &end, 10); if (errno == ERANGE && (a2i == LONG_MAX || a2i == LONG_MIN)) { break; } address = a2i; } else if (strcmp(long_options[i].name, "receiver") == 0) { a2i = strtol(optarg, &end, 10); if (errno == ERANGE && (a2i == LONG_MAX || a2i == LONG_MIN)) { break; } receiver = a2i; } else if (strcmp(long_options[i].name, "command") == 0) { command = get_command_char(optarg); } else if (strcmp(long_options[i].name, "command") == 0) { a2i = strtol(optarg, &end, 10); if (errno == ERANGE && (a2i == LONG_MAX || a2i == LONG_MIN)) { break; } retry = a2i; } break; default: usage(argv[0]); } } if (command == UNKNOWN || address == 0 || gpio == -1 || receiver == 0) { usage(argv[0]); } // store pid and lock it store_pid(); if (wiringPiSetup() == -1) { fprintf(stderr, "Wiring Pi not installed"); return -1; } openlog("homeasy", LOG_PID | LOG_CONS, LOG_USER); syslog(LOG_INFO, "remote: %d, receiver, %d, command: %d\n", address, receiver, command); closelog(); pinMode(gpio, OUTPUT); piHiPri(99); for (c = 0; c != retry; c++) { for (i = 0; i < 5; i++) { transmit(gpio, address, receiver, command); } sleep(1); } return 0; }
int main(int argc, char *argv[]) { double interval; int fd, rc; reload_pending = 0; sym_names_count = sizeof(sym_names) / sizeof(struct symbol_names); varinfo_size = VARINFO_SIZE; varinfo = calloc(varinfo_size, 1); if (!varinfo) { cpuplugd_error("Out of memory: varinfo\n"); exit(1); } /* * varinfo must start with '\n' for correct string matching * in get_var_rvalue(). */ varinfo[0] = '\n'; /* Parse the command line options */ parse_options(argc, argv); /* flock() lock file to prevent multiple instances of cpuplugd */ fd = open(LOCKFILE, O_CREAT | O_RDONLY, S_IRUSR); if (fd == -1) { cpuplugd_error("Cannot open lock file %s: %s\n", LOCKFILE, strerror(errno)); exit(1); } rc = flock(fd, LOCK_EX | LOCK_NB); if (rc) { cpuplugd_error("flock() failed on lock file %s: %s\nThis might " "indicate that an instance of this daemon is " "already running.\n", LOCKFILE, strerror(errno)); exit(1); } /* Make sure that the daemon is not started multiple times */ check_if_started_twice(); /* Store daemon pid also in foreground mode */ handle_signals(); handle_sighup(); /* Need 1 history level minimum for internal symbols */ history_max = 1; /* * Parse arguments from the configuration file, also calculate * history_max */ parse_configfile(configfile); if (history_max > MAX_HISTORY) cpuplugd_exit("History depth %i exceeded maximum (%i)\n", history_max, MAX_HISTORY); /* Check the settings in the configuration file */ check_config(); if (!foreground) { rc = daemon(1, 0); if (rc < 0) cpuplugd_exit("Detach from terminal failed: %s\n", strerror(errno)); } /* Store daemon pid */ store_pid(); /* Unlock lock file */ flock(fd, LOCK_UN); close(fd); /* Install signal handler for floating point exceptions */ rc = feenableexcept(FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW | FE_INVALID); act.sa_flags = SA_NODEFER; sigemptyset(&act.sa_mask); act.sa_handler = sigfpe_handler; if (sigaction(SIGFPE, &act, NULL) < 0) cpuplugd_exit("sigaction( SIGFPE, ... ) failed - reason %s\n", strerror(errno)); setup_history(); /* Main loop */ while (1) { if (reload_pending) { // check for daemon reload reload_daemon(); reload_pending = 0; } history_prev = history_current; history_current = (history_current + 1) % (history_max + 1); time_read(×tamps[history_current]); proc_read(meminfo + history_current * meminfo_size, "/proc/meminfo", meminfo_size); proc_read(vmstat + history_current * vmstat_size, "/proc/vmstat", vmstat_size); proc_cpu_read(cpustat + history_current * cpustat_size); interval = timestamps[history_current] - timestamps[history_prev]; cpuplugd_debug("config update interval: %ld seconds\n", cfg.update); cpuplugd_debug("real update interval: %f seconds\n", interval); /* Run code that may signal failure via longjmp. */ if (cpu == 1) { if (setjmp(jmpenv) == 0) eval_cpu_rules(); else cpuplugd_error("Floating point exception, " "skipping cpu rule " "evaluation.\n"); } if (memory == 1) { if (setjmp(jmpenv) == 0) eval_mem_rules(interval); else cpuplugd_error("Floating point exception, " "skipping memory rule " "evaluation.\n"); } sleep(cfg.update); } return 0; }
int main(int argc, char **argv) { int listen_port = 0; char *listen_addr = NULL; int inetd_mode = 0; const char *pid_file = NULL, *user_name = NULL, *group_name = NULL; int detach = 0; struct passwd *pass = NULL; struct group *group; gid_t gid = 0; int i; /* Without this we cannot rely on waitpid() to tell * what happened to our children. */ signal(SIGCHLD, SIG_DFL); for (i = 1; i < argc; i++) { char *arg = argv[i]; if (!prefixcmp(arg, "--listen=")) { char *p = arg + 9; char *ph = listen_addr = xmalloc(strlen(arg + 9) + 1); while (*p) *ph++ = tolower(*p++); *ph = 0; continue; } if (!prefixcmp(arg, "--port=")) { char *end; unsigned long n; n = strtoul(arg+7, &end, 0); if (arg[7] && !*end) { listen_port = n; continue; } } if (!strcmp(arg, "--inetd")) { inetd_mode = 1; log_syslog = 1; continue; } if (!strcmp(arg, "--verbose")) { verbose = 1; continue; } if (!strcmp(arg, "--syslog")) { log_syslog = 1; continue; } if (!strcmp(arg, "--export-all")) { export_all_trees = 1; continue; } if (!prefixcmp(arg, "--timeout=")) { timeout = atoi(arg+10); continue; } if (!prefixcmp(arg, "--init-timeout=")) { init_timeout = atoi(arg+15); continue; } if (!strcmp(arg, "--strict-paths")) { strict_paths = 1; continue; } if (!prefixcmp(arg, "--base-path=")) { base_path = arg+12; continue; } if (!strcmp(arg, "--base-path-relaxed")) { base_path_relaxed = 1; continue; } if (!prefixcmp(arg, "--interpolated-path=")) { interpolated_path = arg+20; continue; } if (!strcmp(arg, "--reuseaddr")) { reuseaddr = 1; continue; } if (!strcmp(arg, "--user-path")) { user_path = ""; continue; } if (!prefixcmp(arg, "--user-path=")) { user_path = arg + 12; continue; } if (!prefixcmp(arg, "--pid-file=")) { pid_file = arg + 11; continue; } if (!strcmp(arg, "--detach")) { detach = 1; log_syslog = 1; continue; } if (!prefixcmp(arg, "--user="******"--group=")) { group_name = arg + 8; continue; } if (!prefixcmp(arg, "--enable=")) { enable_service(arg + 9, 1); continue; } if (!prefixcmp(arg, "--disable=")) { enable_service(arg + 10, 0); continue; } if (!prefixcmp(arg, "--allow-override=")) { make_service_overridable(arg + 17, 1); continue; } if (!prefixcmp(arg, "--forbid-override=")) { make_service_overridable(arg + 18, 0); continue; } if (!strcmp(arg, "--")) { ok_paths = &argv[i+1]; break; } else if (arg[0] != '-') { ok_paths = &argv[i]; break; } usage(daemon_usage); } if (log_syslog) { openlog("git-daemon", 0, LOG_DAEMON); set_die_routine(daemon_die); } if (inetd_mode && (group_name || user_name)) die("--user and --group are incompatible with --inetd"); if (inetd_mode && (listen_port || listen_addr)) die("--listen= and --port= are incompatible with --inetd"); else if (listen_port == 0) listen_port = DEFAULT_GIT_PORT; if (group_name && !user_name) die("--group supplied without --user"); if (user_name) { pass = getpwnam(user_name); if (!pass) die("user not found - %s", user_name); if (!group_name) gid = pass->pw_gid; else { group = getgrnam(group_name); if (!group) die("group not found - %s", group_name); gid = group->gr_gid; } } if (strict_paths && (!ok_paths || !*ok_paths)) die("option --strict-paths requires a whitelist"); if (base_path) { struct stat st; if (stat(base_path, &st) || !S_ISDIR(st.st_mode)) die("base-path '%s' does not exist or " "is not a directory", base_path); } if (inetd_mode) { struct sockaddr_storage ss; struct sockaddr *peer = (struct sockaddr *)&ss; socklen_t slen = sizeof(ss); freopen("/dev/null", "w", stderr); if (getpeername(0, peer, &slen)) peer = NULL; return execute(peer); } if (detach) daemonize(); else sanitize_stdfds(); if (pid_file) store_pid(pid_file); return serve(listen_addr, listen_port, pass, gid); }