void bear_write_message(int fd, bear_message_t const * e) { write_pid(fd, e->pid); write_pid(fd, e->ppid); write_string(fd, e->fun); write_string(fd, e->cwd); write_string_array(fd, e->cmd); }
int daemon_init(const char *pidfile) { int pid = check_pid(pidfile); if (pid) { fprintf(stderr, "mtask is already running, pid = %d.\n", pid); return 1; } #ifdef __APPLE__ fprintf(stderr, "'daemon' is deprecated: first deprecated in OS X 10.5 , use launchd instead.\n"); #else if (daemon(1,0)) { fprintf(stderr, "Can't daemonize.\n"); return 1; } #endif pid = write_pid(pidfile); if (pid == 0) { return 1; } return 0; }
/* * call this only after you've checked if another instance is running * with check_if_another_instance_running() because the pid file can * contain old pid (if the process has been killed from task manager) * and we don't want to kill another potential process with the same pid. */ void read_pid_and_terminate() { int pid = 0; FILE* get_pid = fopen(PID, "r"); if (get_pid == NULL) die("failed to open the pid file for reading!"); fscanf(get_pid, "%d", &pid); fclose(get_pid); LOG(INFO, "read pid=%d from %s", pid, PID); if (pid == 0) { LOG(INFO, "Process not running, exiting"); return; } char* kill_command = print_to_string("kill %d", pid); FILE* kill = popen(kill_command, "r"); free(kill_command); if (kill == NULL) die("failed to call 'kill PID'"); LOG(INFO, "instance with pid=%d terminated", pid); pclose(kill); turn_light_off(); write_pid(0); }
static void babysit (pid_t grandchild_pid, int parent_pipe) { int sigchld_pipe[2]; /* We don't exec, so we keep parent state, such as the pid that * _dbus_verbose() uses. Reset the pid here. */ _dbus_verbose_reset (); /* I thought SIGCHLD would just wake up the poll, but * that didn't seem to work, so added this pipe. * Probably the pipe is more likely to work on busted * operating systems anyhow. */ if (pipe (sigchld_pipe) < 0) { _dbus_warn ("Not enough file descriptors to create pipe in babysitter process\n"); exit (1); } babysit_sigchld_pipe = sigchld_pipe[WRITE_END]; #ifndef __SYMBIAN32__ _dbus_set_signal_handler (SIGCHLD, babysit_signal_handler); #endif write_pid (parent_pipe, grandchild_pid); check_babysit_events (grandchild_pid, parent_pipe, 0); while (TRUE) { DBusPollFD pfds[2]; pfds[0].fd = parent_pipe; pfds[0].events = _DBUS_POLLIN; pfds[0].revents = 0; pfds[1].fd = sigchld_pipe[READ_END]; pfds[1].events = _DBUS_POLLIN; pfds[1].revents = 0; _dbus_poll (pfds, _DBUS_N_ELEMENTS (pfds), -1); if (pfds[0].revents != 0) { check_babysit_events (grandchild_pid, parent_pipe, pfds[0].revents); } else if (pfds[1].revents & _DBUS_POLLIN) { char b; read (sigchld_pipe[READ_END], &b, 1); /* do waitpid check */ check_babysit_events (grandchild_pid, parent_pipe, 0); } } exit (1); }
void no_daemon_start(char * path, int nbCoreFiles, const char *name, void (*on_start) (void), void (*on_stop) (void), void (*on_hup) (void)) { int pid; DEBUG_FUNCTION; sprintf(pid_file, "%s%s", DAEMON_PID_DIRECTORY, name); // check if running if (read_pid(name, &pid) == 0 && kill(pid, 0) == 0) { fprintf(stderr, "%s already running as pid: %d\n", pid_file, pid); return; } rozofs_signals_declare (path,nbCoreFiles); if (write_pid(name) != 0) { fatal("write_pid failed: %s", strerror(errno)); return; } rozofs_attach_crash_cbk(remove_pid_file); if (on_stop) rozofs_attach_crash_cbk((rozofs_attach_crash_cbk_t)on_stop); if (on_hup) rozofs_attach_hgup_cbk((rozofs_attach_crash_cbk_t)on_hup); on_start(); }
int main(int argc, char* argv[]) { signal(SIGINT, signal_term); signal(SIGTERM, signal_term); int daemon_mode = (argc > 1 && strcmp(argv[1], "-d") == 0) ? 1 : 0; if (daemon_mode) { if (file_exists(file_pid)) { perror("already running"); exit(1); } pid_t pid = fork(); // fork failed if (pid < 0) { return -1; } else if (pid != 0) { // fork successed, parent exit return 0; } write_pid(); setsid(); chdir("/"); umask(0755); close(0); close(1); close(2); } lcd_connect(); lcd_reset(); lcd_clear(); lcd_toggle_backlight(); sleep(1); do { show_hostname(); show_time(); show_uptime(); show_mems(); show_loads(); show_diskspace(DEFAULT_DISKSPACE_DIR); show_ip(DEFAULT_NETWORK_INTERFACE); show_traffic(DEFAULT_NETWORK_INTERFACE); sleep(10); } while (!signal_term_received); lcd_clear(); lcd_reset(); lcd_disconnect(); unlink(file_pid); return 0; }
void suppr(int sig) { write_pid("pid.dat", pid); kill(ordo, SIGUSR2); printf("Envoi à l'ordo de %d\n", pid); kill(pid, SIGKILL); }
/** * 进行服务初始化相关工作 */ void ADService::adservice_init() { memset(modules,0,sizeof(modules)); dosignals(); int pid = write_pid(DEFAULT_DAEMON_FILE); if(pid==0){ DebugMessage("can not write pid file.exit"); exit(0); } }
static void init(const char *pid_file) { if (write_pid(pid_file)) WARNING("can't write PID file to %s: %m", pid_file); if (atexit(cleanup)) WARNING("atexit() failed: %m"); if (register_signal_handler(SIGTERM, sigterm)) WARNING("can't catch SIGTERM: %m"); }
int main() { openlog("kappaio-watch", LOG_CONS|LOG_PID, LOG_USER); syslog(LOG_INFO, "kappaio-watch v1.01"); daemonize(); if (write_pid() < 0) { exit(0); } int keepAliveCountRef = 0; int keepAliveCount; int tolerance = 0; while(1) { if (checkByPIDFile() < 0) { syslog(LOG_INFO, "process is not running"); /* restart process immediately*/ system("/etc/init.d/kappaio restart > /dev/null"); goto SKIP_THE_REST; } keepAliveCount = readKLA(); if (keepAliveCount < 0) { syslog(LOG_INFO, "keep alive counter < 0, restarting..."); /* restart process immediately*/ system("/etc/init.d/kappaio restart > /dev/null"); goto SKIP_THE_REST; } if (keepAliveCount != keepAliveCountRef) { keepAliveCountRef = keepAliveCount; tolerance = 0; } else { if (tolerance > 0) { syslog(LOG_INFO, "tolerance > 0, restarting..."); /* restart process immediately*/ system("/etc/init.d/kappaio restart > /dev/null"); tolerance = 0; } else { tolerance++; } } SKIP_THE_REST: kSleep(10); } }
void ex_program(int sig) { LOG(WARN, "Caught signal: %d! Turning light off...\n", sig); terminated = 1; turn_light_off(); write_pid((pid_t) 0); exit(0); (void) signal(SIGINT, SIG_DFL); // (void) signal(SIGINT, ex_program); }
static void start_chibach_up() { debug( "Starting chibach ... (CHIBACH_HOME = %s)", get_chibach_home() ); maybe_daemonize(); write_pid( get_chibach_pid(), get_chibach_name() ); chibach_started = true; start_messenger(); }
int Application::main(int argc, char **argv){ conf = NULL; welcome(); parse_args(argc, argv); init(); write_pid(); run(); remove_pidfile(); delete conf; return 0; }
static void test_write_pid_fail_if_open_fail() { will_return( mock_open, -1 ); // Test if correctly opened. char path[] = "/home/yasuhito/trema/tmp/chess.pid"; expect_string( mock_open, pathname, path ); expect_value( mock_open, flags, ( O_RDWR | O_CREAT ) ); expect_value( mock_open, mode, 0600 ); expect_string( mock_die, message, "Could not create a PID file: /home/yasuhito/trema/tmp/chess.pid" ); expect_assert_failure( write_pid( "/home/yasuhito/trema/tmp", "chess" ) ); }
int monikor_daemonize(monikor_t *mon) { if (mon->config->daemonize) { if (perform_daemonize(mon)) return 1; } if (drop_privileges(mon)) return 1; if (mon->config->daemonize) { if (write_pid(mon)) { monikor_log(LOG_ERR, "Cannot write pid file: %s\n", strerror(errno)); } } return 0; }
struct switch_arguments * init_parse_args( int argc, char **argv ) { struct switch_arguments *args = xmalloc( sizeof( struct switch_arguments ) ); parse_options( args, argc, argv ); int efd[ 2 ]; uint32_t i; for ( i = 0; i < sizeof( efd ) / sizeof( efd[ 0 ] ); i++ ) { efd[ i ] = create_event_fd(); if ( efd[ i ] == -1 ) { error( "failed to create_event_fd %d", errno ); xfree( args ); return NULL; } } memcpy( args->efd, &efd, sizeof( efd ) ); args->to_protocol_queue = create_message_queue(); assert( args->to_protocol_queue != NULL ); char *switch_log = get_switch_log(); logging_type log_output_type = LOGGING_TYPE_FILE; if ( args->run_as_daemon == false ) { log_output_type |= LOGGING_TYPE_STDOUT; } char name[ PATH_MAX ]; snprintf( name, PATH_MAX, "%s.%#" PRIx64, args->progname, args->datapath_id ); init_log( name, switch_log, log_output_type ); xfree( switch_log ); char *switch_pid_dir = get_switch_pid_dir(); write_pid( switch_pid_dir, name ); char cmd[ PATH_MAX ]; /* * change the pid file permissions to allow deletion since switch is started * as sudo. This would become obsolete if sudo is removed. */ snprintf( cmd, PATH_MAX, "chmod 0666 %s/%s.pid", switch_pid_dir, name ); system( cmd ); xfree( switch_pid_dir ); ignore_sigpipe(); #ifdef NOT_TESTED if ( args->run_as_daemon == true ) { daemonize( get_switch_home() ); } #endif return args; }
void start_wall(){ current_dir = malloc(40); style = malloc(20); read_config(current_dir, style, &time_gap); int pid = fork(); if(!pid){ while(1){ read_images(); sleep(time_gap); } }else{ write_pid(pid); } }
int main( int argc, char *argv[] ) { selected_item = 0; scroll_count = 800; alpha_up = 1; alpha = 0; if ( argc > 1 ) { strcpy( pdm_dir, argv[1] ); if ( pdm_dir[strlen(pdm_dir)-1] != '/' ) strcat( pdm_dir, "/" ); } else { fprintf(stderr, "Usage: %s pdm_exec_dir\n", argv[0] ); exit(0); } write_pid(); printf("reading configuration file: %s\n", pdm_dir_and("data/pdm.conf") ); cfg_read( pdm_dir_and("data/pdm.conf") ); if( general->autoboot_item >= 0 ) { execl( item->exec_path[general->autoboot_item], item->exec_path[general->autoboot_item], NULL); free( item ); free( general ); exit(0); } gui_init(); gui_load(); while( !gui_done ) { gui_draw(); gui_events(); } gui_clean(); free(item); free(general); return 0; }
void initialize (void) { cfgfile = cfgfile ? cfgfile : CFGFILE; logfile = logfile ? logfile : LOGFILE; pidfile = pidfile ? pidfile : PIDFILE; read_conf (cfgfile); if (mode & DAEMON) { /* redirect stdout & stderr to a log file */ redirect_to_log (logfile, STDOUT_FILENO | STDERR_FILENO); write_pid (pidfile); } dns_random_init (seed); }
int main(int argc, char **argv) { struct stat sbuf; char *file, *flags = NULL; int c, err = 0, pid = 0; __pmSetProgname(argv[0]); while ((c = getopt(argc, argv, "f:p:")) != EOF) { switch (c) { case 'f': flags = optarg; break; case 'p': pid = atoi(optarg); break; default: err++; } } if (err || argc != optind + 1) usage(); file = argv[optind]; c = open(file, O_RDWR, 0644); if (c < 0) { fprintf(stderr, "Cannot open %s for writing: %s\n", file, strerror(errno)); exit(1); } fstat(c, &sbuf); addr = __pmMemoryMap(c, sbuf.st_size, 1); close(c); if (flags) write_flags(flags); if (pid) write_pid(pid); __pmMemoryUnmap(addr, sbuf.st_size); exit(0); }
PUBLIC int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "Usage: move [-t] <dbfilename> [<move-source-code-file> ...]\n" "\t-t\tInhibits loading threads which were active when the DB was saved\n"); exit(MOVE_EXIT_ERROR); } signal(SIGINT, siginthandler); /* %%% This can be made to emergency-flush the database to disk, later on %%% */ write_pid(); init_gc(); init_object(); init_prim(); init_vm_global(); init_thread(); checkpoint_filename = "move.checkpoint"; install_primitives(); { int load_threads = 1; if (!strcmp(argv[1], "-t")) { load_threads = 0; argv++; argc--; } import_db(argv[1], load_threads); } bind_primitives_to_symbols(); import_cmdline_files(argc - 2, argv + 2); run_main_loop(); done_gc(); return MOVE_EXIT_OK; }
/* Detach from current terminal, write pidfile, kill parent */ bool detach(void) { setup_signals(); /* First check if we can open a fresh new pidfile */ #ifndef HAVE_MINGW if(!write_pidfile()) return false; /* If we succeeded in doing that, detach */ closelogger(); #endif if(do_detach) { #ifndef HAVE_MINGW if(daemon(0, 0)) { fprintf(stderr, "Couldn't detach from terminal: %s", strerror(errno)); return false; } /* Now UPDATE the pid in the pidfile, because we changed it... */ if(!write_pid(pidfilename)) { fprintf(stderr, "Could not write pid file %s: %s\n", pidfilename, strerror(errno)); return false; } #else if(!statushandle) exit(install_service()); #endif } openlogger(identname, use_logfile?LOGMODE_FILE:(do_detach?LOGMODE_SYSLOG:LOGMODE_STDERR)); logger(LOG_NOTICE, "tincd %s (%s %s) starting, debug level %d", VERSION, __DATE__, __TIME__, debug_level); xalloc_fail_func = memory_full; return true; }
int start(void) { int pid; int ret; // fork a subprocess to execute the job pid = fork(); if (pid < 0) // failed to fork { printf("\x1B[31mFailed to fork subprocess\033[0m\n"); return -2; } else if (pid == 0) // child process { pid = getpid(); // write pid to file to make sure that the router is not allowed to be // executed twice at the same time ret = write_pid(pid, router_id); if (ret) { return ret; } printf("\x1B[32mRouter%d\033[0m starts at pid=\x1B[32m%d\033[0m\n", router_id, pid); // start the router demon ret = router_demon_start(); if (ret) { return ret; } remove_pid(router_id); } else // parent process { } return 0; }
static void test_write_pid_fail_if_lockf_fail() { will_return( mock_open, 1111 ); will_return( mock_lockf, -1 ); // Test if correctly locked. char path[] = "/home/yasuhito/trema/tmp/chess.pid"; expect_string( mock_open, pathname, path ); expect_value( mock_open, flags, ( O_RDWR | O_CREAT ) ); expect_value( mock_open, mode, 0600 ); // Test if correctly locked. expect_value( mock_lockf, fd, 1111 ); expect_value( mock_lockf, cmd, F_TLOCK ); expect_value( mock_lockf, len, 0 ); // lockf_return_value = -1; expect_string( mock_die, message, "Could not acquire a lock on a PID file: /home/yasuhito/trema/tmp/chess.pid" ); expect_assert_failure( write_pid( "/home/yasuhito/trema/tmp", "chess" ) ); }
int daemon_init(const char *pidfile) { // check stored process id is valid or not // if it is valid then indicate the process is already running, just return int pid = check_pid(pidfile); if (pid) { fprintf(stderr, "Skynet is already running, pid = %d.\n", pid); return 1; } #ifdef __APPLE__ fprintf(stderr, "'daemon' is deprecated: first deprecated in OS X 10.5 , use launchd instead.\n"); #else // int daemon(int nochdir, int noclose): // 1. this function is for programs wishing to detach themselves form the controlling terminal // and run in the background as system daemons. // 2. if `nochdir` is 0, it changes the calling process's current working directory // to the root directory (/); otherwise, the current working directory is left unchanged // 3. if `noclose` is 0, it redirects stdin stdout stderr to `/dev/null`, // otherwise, no changes are made to these file descriptors // daemonize current process: daemon or service is a background process that is designed to run auto without ui // and keep current working directory and redirect standand stream to `/dev/null`. if (daemon(1,0)) { fprintf(stderr, "Can't daemonize.\n"); return 1; } #endif // create the pid file if not exist // lock this pid file // get current process id and write to this file // note that the pid file is not closed pid = write_pid(pidfile); if (pid == 0) { return 1; } return 0; }
int main(int argc, char **argv) { int ret; signal(SIGINT, suppr); ordo = read_pid("ordo.dat"); printf("pid ordo = %d\n", ordo); if(argc == 1) { fprintf(stderr, "pas de programme specifié\n" "usage: ./lanceur prog [prog args]\n"); exit(0); } if((pid = fork()) == -1) { perror("erreur fork"); exit(1); } if(pid == 0) { ret = execvp(argv[1], argv+1); if(ret == -1) exit(2); } else { write_pid("pid.dat", pid); printf("Envoi à l'ordo de %d\n", pid); kill(ordo, SIGUSR1); wait(NULL); suppr(0); } return 0; }
/* check for an existing tinc for this net, and write pid to pidfile */ static bool write_pidfile(void) { pid_t pid; pid = check_pid(pidfilename); if(pid) { if(netname) fprintf(stderr, "A tincd is already running for net `%s' with pid %ld.\n", netname, (long)pid); else fprintf(stderr, "A tincd is already running with pid %ld.\n", (long)pid); return false; } /* if it's locked, write-protected, or whatever */ if(!write_pid(pidfilename)) { fprintf(stderr, "Could write pid file %s: %s\n", pidfilename, strerror(errno)); return false; } return true; }
int main(int argc, char *argv[]) { int len, addrlen, sfd; struct sockaddr_in serv_addr; char data[512]; addrlen = sizeof(struct sockaddr_in); parse_cmd(&argc, argv); if(!check_instance()){ DEBUG_MSG("One instance is running!\n"); exit(1); } signal(SIGTERM, handle_int_signal); signal(SIGINT, handle_int_signal); signal(SIGHUP, handle_int_signal); if(!DEBUG_ON) init_daemon(); write_pid(); gbl_fd = sfd = open_udp_server_socket(SERV_PORT, &serv_addr); while (1) { memset(data, '\0', sizeof(data)); len = recvfrom(sfd, data, sizeof(data), 0, (struct sockaddr *) &serv_addr, &addrlen); printf("got %s\n", data); system(data); } close(gbl_fd); return 0; }
static void daemonize (void) { // Process and Session ID pid_t pid, sid; syslog(LOG_INFO,"Going into daemon mode..."); // Fork off process pid = fork(); if (pid < 0) exit(EXIT_FAILURE); else if (pid > 0) exit(EXIT_SUCCESS); // Change umask umask(0); // Create a new SID for the child process sid = setsid(); if (sid < 0) exit(EXIT_FAILURE); // Change current working directory if ((chdir("/")) < 0) exit(EXIT_FAILURE); // Write PID file write_pid(sid); if (!config.quiet) printf("Going into background...\n"); // Close file descriptors close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); }
int main(int argc, char **argv) { int delay; int keepnum; if (argc<3) { fprintf(stderr, "Usage: checkpoint-cleanup <filename-prefix> <time-between-cleans>\n" " <how-many-to-keep>\n"); exit(1); } prefix = argv[1]; /* GLOBAL VARIABLES! */ prefixlen = strlen(prefix); delay = atoi(argv[2]); keepnum = atoi(argv[3]); if (delay < MINDELAY) { fprintf(stderr, "Delay must be >= %d seconds.\n", MINDELAY); exit(1); } if (keepnum < 1) { fprintf(stderr, "Number of files to keep must be 1 or greater.\n"); exit(1); } write_pid(); while (1) { cleanup(keepnum); sleep(delay); } return 0; }