int main(int argc, char **argv) { int ret = 0; deamonize(); ret = pidfile_create("/var/run/test.pid", 0); printf("ret = %d\n", ret); printf("press Enter to exit.pid = %d\n", getpid()); while(1) { sleep(10); } return 0; }
void start_client(char *srv, int port) { int socket_d, ret; socklen_t len_inet; struct sigaction sa; struct sockaddr_in in_addr; char buf[bufsize]; ssize_t reads; struct timespec rem; struct timespec req = { .tv_sec = 0, .tv_nsec = 1492000 }; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; sa.sa_handler = chld_reaper; if (sigaction(SIGCHLD, &sa, NULL) == -1) oops("error from sigaction()"); socket_d = socket(AF_INET, SOCK_STREAM, 0); if (socket_d == -1) oops("error from socket()"); len_inet = sizeof(struct sockaddr_in); memset(&in_addr, 0, len_inet); in_addr.sin_family = AF_INET; in_addr.sin_port = htons(port); in_addr.sin_addr.s_addr = inet_addr(srv); if (connect(socket_d, (struct sockaddr *)&in_addr, len_inet) == -1) oops("error from connect()"); switch(fork()) { case -1: oops("error from fork()"); case 0: /* child -- read server's response */ for (;;) { reads = readn(socket_d, buf, bufsize); if (reads <= 0) break; printf("%.*s", (int) reads, buf); } exit(EXIT_SUCCESS); default: /* parent -- write stdin to socket */ for (;;) { req.tv_nsec++; if (req.tv_nsec >= 14920000) req.tv_nsec = 1492000; reads = read(STDIN_FILENO, buf, bufsize); if (reads <= 0) break; /* insert some sleep */ retry: ret = nanosleep(&req, &rem); if (ret) { if (errno == EINTR) { req.tv_sec = rem.tv_sec; req.tv_nsec = rem.tv_nsec; goto retry; } syslog(LOG_ERR, "nanosleep() failed: %s", strerror(errno)); } writen(socket_d, buf, bufsize); } exit(EXIT_SUCCESS); } } int main(int argc, char *argv[]) { int next_opt, mode = 0; char *server = NULL; const char *short_opts = "hsc:p:b:"; const struct option long_opts[] = { {"help", 0, NULL, 'h'}, {"server", 0, NULL, 's'}, {"client", 1, NULL, 'c'}, {"port", 1, NULL, 'p'}, {"bufsz", 1, NULL, 'b'}, {NULL, 0, NULL, 0} } ; int port_num = 9999; prg_name = argv[0]; do { next_opt = getopt_long(argc, argv, short_opts, long_opts, NULL); switch (next_opt) { case 'h': print_usage(stdout, 0); case 's': mode += 1; break; case 'c': mode += 2; server = optarg; break; case 'p': port_num = atoi(optarg); break; case 'b': bufsize = atoi(optarg); break; case '?': print_usage(stderr, 1); case -1: break; default: abort(); } } while (next_opt != -1); if (argc == 1) print_usage(stderr, 2); if (mode == 0 || mode > 2) oops("Ok, I cannot realize what you want now..."); switch (mode) { case 1: /* server mode */ if (deamonize() == -1) oops("failed to become a deamon."); start_server(port_num); break; case 2: /* client mode */ start_client(server, port_num); break; } return 0; }
int main(int argc, char **argv) { /* Run main garbage collector when quiting the daemon */ gc_attach(main_gc); /* Catch all exit signals for gc */ gc_catch(); loglevel = LOG_INFO; log_file_disable(); log_shell_enable(); char *logfile = malloc(strlen(LOG_FILE)+1); strcpy(logfile, LOG_FILE); log_file_set(logfile); sfree((void *)&logfile); prevMessage = malloc(4); memset(prevMessage, '\0', 4); progname = malloc(14); strcpy(progname, "splash-daemon"); struct socket_callback_t socket_callback; struct options_t *options = NULL; char *args = NULL; char buffer[BUFFER_SIZE]; int f; options_add(&options, 'H', "help", no_value, 0, NULL); options_add(&options, 'V', "version", no_value, 0, NULL); options_add(&options, 'D', "nodaemon", no_value, 0, NULL); while (1) { int c; c = options_parse(&options, argc, argv, 1, &args); if (c == -1) break; switch(c) { case 'H': printf("Usage: %s [options]\n", progname); printf("\t -H --help\t\tdisplay usage summary\n"); printf("\t -V --version\t\tdisplay version\n"); printf("\t -S --settings\t\tsettings file\n"); printf("\t -D --nodaemon\t\tdo not daemonize and\n"); printf("\t\t\t\tshow debug information\n"); return (EXIT_SUCCESS); break; case 'V': printf("%s %s\n", progname, "1.0"); return (EXIT_SUCCESS); break; case 'D': nodaemon=1; break; default: printf("Usage: %s [options]\n", progname); return (EXIT_FAILURE); break; } } options_delete(options); pid_file = malloc(sizeof(PID_FILE)+1); strcpy(pid_file, PID_FILE); template_file = malloc(14); strcpy(template_file, "template.json"); if((f = open(pid_file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) != -1) { if(read(f, buffer, BUFFER_SIZE) != -1) { //If the file is empty, create a new process if(!atoi(buffer)) { running = 0; } else { //Check if the process is running kill(atoi(buffer), 0); //If not, create a new process if(errno == ESRCH) { running = 0; } } } } else { logprintf(LOG_ERR, "could not open / create pid_file %s", pid_file); return EXIT_FAILURE; } close(f); if(nodaemon == 1 || running == 1) { log_level_set(LOG_DEBUG); } if(running == 1) { nodaemon=1; logprintf(LOG_NOTICE, "already active (pid %d)", atoi(buffer)); return EXIT_FAILURE; } if(nodaemon == 0) { deamonize(); socket_start(PORT); fb_init(); } else { socket_start(PORT); } if(template_read(template_file) == EXIT_FAILURE) { logprintf(LOG_NOTICE, "failed to read template file %s", template_file); main_gc(); return EXIT_FAILURE; } if(nodaemon == 1) { //template_print(); } //initialise all socket_clients and handshakes to 0 so not checked memset(socket_clients, 0, sizeof(socket_clients)); socket_callback.client_disconnected_callback = NULL; socket_callback.client_connected_callback = NULL; socket_callback.client_data_callback = &socket_parse_data; main_draw(); /* Make sure the server part is non-blocking by creating a new thread */ pthread_create(&pth, NULL, &update_progress, (void *)NULL); socket_wait((void *)&socket_callback); while(main_loop) { sleep(1); } return EXIT_FAILURE; }
int main (int argc, char *argv[]) { int deamon = 1; /* deamon by default */ int xosd = 0; /* do not start as xosd client */ openlog("fsfn",LOG_CONS|LOG_NDELAY|LOG_PID,LOG_DAEMON); /* fill a default */ //strncpy (devinput, "/dev/input/event0", 255); strncpy (devinput, getConfig("DEVICE"), MAX_CFG_LENGTH); /* parse command line */ while (0 == 0) { int option_index = 0; int next_option; static struct option long_options[] = { {"help", 0, NULL, 'h'}, {"nodaemon", 0, NULL, 'n'}, {"device", 1, NULL, 'd'}, #ifdef HAVE_LIBXOSD {"osd", 0, NULL, 'o'}, #endif {NULL, 0, NULL, 0} }; #ifdef HAVE_LIBXOSD next_option = getopt_long (argc, argv, "hnd:o", long_options, &option_index); #else next_option = getopt_long (argc, argv, "hnd:", long_options, &option_index); #endif if (next_option == -1) { break; } switch (next_option) { case 'n': deamon = 0; break; case 'o': // printf("We are osd\n"); xosd = 1; break; case 'd': if (optarg) { // printf("We are changing devinput: %s\n",devinput); strncpy (devinput, optarg, 255); break; } default: // printf("Unknow option: %c\n",next_option); usage (argv[0]); cleanExit(-1); exit (-1); } } deamon&=!xosd; //could not be deamon if osd if (deamon) { deamonize (); } else { if (!xosd) { loop (); } #ifdef HAVE_LIBXOSD else { looposd (); } #endif } cleanExit(EXIT_SUCCESS); return EXIT_SUCCESS; }
int main() { deamonize(); system("ps -axj"); return 0; }