int main(int argc, char const *argv[]) { if(argc!=2){ printf("USAGE: %s FILE\n", argv[0]); return EXIT_FAILURE; } FILE* f = fopen(argv[1], "r"); if(!f){ perror("Failed to open file: "); return EXIT_FAILURE; } atexit(clean_matrix); yyin = f; yyparse(); fclose(f); yylex_destroy(); build_starts(); link_matrix(); struct MATRIX *matrix = get_matrix(); int result = forkx(matrix); free(matrix); if(!result){ wait_for_children(); } return EXIT_SUCCESS; }
int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { // Register a signal handler. ACE_Sig_Action sa ((ACE_SignalHandler) handler, SIGINT); ACE_UNUSED_ARG (sa); Options::instance ()->parse_args (argc, argv); #if !defined (ACE_WIN32) if (Options::instance ()->child ()) { ACE_DEBUG ((LM_INFO, "(%P|%t) about to recurse with iteration count = %d, debug = %d\n", Options::instance ()->iteration_count ())); // We've been forked... malloc_recurse (Options::instance ()->iteration_count ()); Malloc::instance ()->remove (); } else #endif /* ACE_WIN32 */ { for (size_t i = 0; i < Options::instance ()->spawn_count (); i++) spawn (); wait_for_children (); Malloc::instance ()->remove (); } return 0; }
void broadcast_signal(int sig, bool wait_for_exit) { sigset_t mask, oldmask; Set *pids = NULL; if (wait_for_exit) pids = set_new(trivial_hash_func, trivial_compare_func); assert_se(sigemptyset(&mask) == 0); assert_se(sigaddset(&mask, SIGCHLD) == 0); assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0); if (kill(-1, SIGSTOP) < 0 && errno != ESRCH) log_warning("kill(-1, SIGSTOP) failed: %m"); killall(sig, pids); if (kill(-1, SIGCONT) < 0 && errno != ESRCH) log_warning("kill(-1, SIGCONT) failed: %m"); if (wait_for_exit) wait_for_children(pids, &mask); assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) == 0); set_free(pids); }
static void ultimate_send_signal(int sign) { sigset_t mask, oldmask; int r; assert_se(sigemptyset(&mask) == 0); assert_se(sigaddset(&mask, SIGCHLD) == 0); assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0); if (kill(-1, SIGSTOP) < 0 && errno != ESRCH) log_warning("kill(-1, SIGSTOP) failed: %m"); r = kill(-1, sign); if (r < 0 && errno != ESRCH) log_warning("kill(-1, %s) failed: %m", signal_to_string(sign)); if (kill(-1, SIGCONT) < 0 && errno != ESRCH) log_warning("kill(-1, SIGCONT) failed: %m"); if (r < 0) goto finish; wait_for_children(0, &mask); finish: sigprocmask(SIG_SETMASK, &oldmask, NULL); }
/* ------------------------------------------------------------------ * Opens and iterates through a given directory, operating on allowed * files found within it. . and .. are skipped, as are files beginning * with . unless it's been enabled in the flags. All other entries, * both files and directories, are thrown to process_entry to determine * whether they're a file/directory/link, and to read/explore them if * appropriate. If threads are being used, they wait for their children * threads to terminate after they themselves are done with their work. * * flags: The usual I/O option flags. * list_head: A list of already visited directories. Always will have * the working directory low was called from as the tail, * and will always have the current directory being explored * as the head (so the src = list_head->next->path assignment * is always safe). * fullpath: The realpath of the current directory being explored. * dir_depth: Passed to process_entry. * ------------------------------------------------------------------ */ int explore_dir(params *P, visited_dir *list_head, char *nextpath, int dir_depth) { DIR *d; struct dirent *entry; struct dirent **done; int len; if ( (d = opendir(nextpath)) == 0) { return print_file_error(P, errno, nextpath); } if (((P->max_dir_depth) - dir_depth) > read_stat(P, t_ddepth)) update_stat(P, t_ddepth, ((P->max_dir_depth) - dir_depth)); len = offsetof(struct dirent, d_name) + pathconf(nextpath, _PC_NAME_MAX) + 1; if ((entry = malloc(len)) == NULL || (done = malloc(sizeof(struct dirent*))) == NULL) { fprintf(stderr, "Malloc failed in explore_dir.\n"); return -1; } done = &entry; while ( (readdir_r(d, entry, done)) == 0 && (*done != NULL)) { /* don't process '.' or '..' in a directory! */ if ( (strcmp(entry->d_name, THIS_DIR) == 0) || (strcmp(entry->d_name, PARENT_DIR) == 0)) continue; /* do all files but ones beginning with a dot, * unless we've enabled it! */ if (entry->d_name[0] == '.') { if (enabled(P, READ_DOT_FILES)) { process_entry(P, list_head, entry->d_name, nextpath, dir_depth, 0); } else inc_stat(P, t_dotfiles, 1); } else process_entry(P, list_head, entry->d_name, nextpath, dir_depth, 0); } closedir(d); wait_for_children(list_head); free(nextpath); return 0; }
int main(int argc, char ** argv) { setpgid(0, 0); sigset_t set, oldset; sigemptyset(&set); sigaddset(&set, SIGTERM); sigprocmask(SIG_BLOCK, &set, &oldset); if (sigsetjmp(env, 1) == 1) { kill(0, SIGTERM); exit(0); } sigprocmask(SIG_SETMASK, &oldset, NULL); struct sigaction act; memset(&act, 0, sizeof act); act.sa_handler = &handle_term; sigaction(SIGTERM, &act, NULL); fcntl(3, F_SETFD, fcntl(3, F_GETFD) | FD_CLOEXEC); while (1) { wait_for_children(); int conn = accept(3, NULL, NULL); if (conn == -1) { if (errno == EINTR) continue; err(1, "accepting socket connection"); } errno = 0; while (dup2(conn, STDIN_FILENO) == -1 && errno == EINTR); if (errno && errno != EINTR) err(2, "duping socket to stdin"); while (dup2(conn, STDOUT_FILENO) == -1 && errno == EINTR); if (errno && errno != EINTR) err(2, "duping socket to stout"); switch (vfork()) { case -1: err(3, "forking"); case 0: execl(argv[2], argv[1], NULL); _exit(212); } if (close(conn) == -1) perror("closing socket connection"); } }
static void send_signal(int sign) { sigset_t mask, oldmask; int n_processes; assert_se(sigemptyset(&mask) == 0); assert_se(sigaddset(&mask, SIGCHLD) == 0); assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0); if (kill(-1, SIGSTOP) < 0 && errno != ESRCH) log_warning("kill(-1, SIGSTOP) failed: %m"); n_processes = killall(sign); if (kill(-1, SIGCONT) < 0 && errno != ESRCH) log_warning("kill(-1, SIGCONT) failed: %m"); if (n_processes <= 0) goto finish; wait_for_children(n_processes, &mask); finish: sigprocmask(SIG_SETMASK, &oldmask, NULL); }
void broadcast_signal(int sig, bool wait_for_exit, bool send_sighup) { sigset_t mask, oldmask; _cleanup_set_free_ Set *pids = NULL; if (wait_for_exit) pids = set_new(NULL); assert_se(sigemptyset(&mask) == 0); assert_se(sigaddset(&mask, SIGCHLD) == 0); assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0); if (kill(-1, SIGSTOP) < 0 && errno != ESRCH) log_warning_errno(errno, "kill(-1, SIGSTOP) failed: %m"); killall(sig, pids, send_sighup); if (kill(-1, SIGCONT) < 0 && errno != ESRCH) log_warning_errno(errno, "kill(-1, SIGCONT) failed: %m"); if (wait_for_exit) wait_for_children(pids, &mask); assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) == 0); }
int main(int argc, char **argv) { int ch, debug = 0, error, iscsi_fd, maxproc = 30, retval, saved_errno, timeout = 60; bool dont_daemonize = false; struct pidfh *pidfh; pid_t pid, otherpid; const char *pidfile_path = DEFAULT_PIDFILE; struct iscsi_daemon_request request; while ((ch = getopt(argc, argv, "P:dl:m:t:")) != -1) { switch (ch) { case 'P': pidfile_path = optarg; break; case 'd': dont_daemonize = true; debug++; break; case 'l': debug = atoi(optarg); break; case 'm': maxproc = atoi(optarg); break; case 't': timeout = atoi(optarg); break; case '?': default: usage(); } } argc -= optind; if (argc != 0) usage(); log_init(debug); pidfh = pidfile_open(pidfile_path, 0600, &otherpid); if (pidfh == NULL) { if (errno == EEXIST) log_errx(1, "daemon already running, pid: %jd.", (intmax_t)otherpid); log_err(1, "cannot open or create pidfile \"%s\"", pidfile_path); } iscsi_fd = open(ISCSI_PATH, O_RDWR); if (iscsi_fd < 0 && errno == ENOENT) { saved_errno = errno; retval = kldload("iscsi"); if (retval != -1) iscsi_fd = open(ISCSI_PATH, O_RDWR); else errno = saved_errno; } if (iscsi_fd < 0) log_err(1, "failed to open %s", ISCSI_PATH); if (dont_daemonize == false) { if (daemon(0, 0) == -1) { log_warn("cannot daemonize"); pidfile_remove(pidfh); exit(1); } } pidfile_write(pidfh); register_sigchld(); for (;;) { log_debugx("waiting for request from the kernel"); memset(&request, 0, sizeof(request)); error = ioctl(iscsi_fd, ISCSIDWAIT, &request); if (error != 0) { if (errno == EINTR) { nchildren -= wait_for_children(false); assert(nchildren >= 0); continue; } log_err(1, "ISCSIDWAIT"); } if (dont_daemonize) { log_debugx("not forking due to -d flag; " "will exit after servicing a single request"); } else { nchildren -= wait_for_children(false); assert(nchildren >= 0); while (maxproc > 0 && nchildren >= maxproc) { log_debugx("maxproc limit of %d child processes hit; " "waiting for child process to exit", maxproc); nchildren -= wait_for_children(true); assert(nchildren >= 0); } log_debugx("incoming connection; forking child process #%d", nchildren); nchildren++; pid = fork(); if (pid < 0) log_err(1, "fork"); if (pid > 0) continue; } pidfile_close(pidfh); handle_request(iscsi_fd, &request, timeout); } return (0); }
int main (int argc, char *argv[]) { GtkWidget *button; GtkWidget *hbox; GtkAccelGroup *accel_group; GtkItemFactory *item_factory; gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); gtk_window_set_title (GTK_WINDOW (window), "Socket Test"); gtk_container_set_border_width (GTK_CONTAINER (window), 0); vbox = gtk_vbox_new (FALSE, 0); gtk_container_add (GTK_CONTAINER (window), vbox); accel_group = gtk_accel_group_new (); gtk_window_add_accel_group (GTK_WINDOW (window), accel_group); item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "<main>", accel_group); gtk_item_factory_create_items (item_factory, G_N_ELEMENTS (menu_items), menu_items, NULL); gtk_box_pack_start (GTK_BOX (vbox), gtk_item_factory_get_widget (item_factory, "<main>"), FALSE, FALSE, 0); button = gtk_button_new_with_label ("Add Active Qt Child"); gtk_box_pack_start (GTK_BOX(vbox), button, FALSE, FALSE, 0); g_signal_connect (button, "clicked", G_CALLBACK (add_active_qt_child), NULL); button = gtk_button_new_with_label ("Add Passive Qt Child"); gtk_box_pack_start (GTK_BOX(vbox), button, FALSE, FALSE, 0); g_signal_connect (button, "clicked", GTK_SIGNAL_FUNC (add_passive_qt_child), NULL); button = gtk_button_new_with_label ("Add Active GTK+ Child"); gtk_box_pack_start (GTK_BOX(vbox), button, FALSE, FALSE, 0); g_signal_connect (button, "clicked", G_CALLBACK (add_active_gtk_child), NULL); button = gtk_button_new_with_label ("Add Passive GTK+ Child"); gtk_box_pack_start (GTK_BOX(vbox), button, FALSE, FALSE, 0); g_signal_connect (button, "clicked", G_CALLBACK (add_passive_gtk_child), NULL); button = gtk_button_new_with_label ("Remove Last Child"); gtk_box_pack_start (GTK_BOX(vbox), button, FALSE, FALSE, 0); g_signal_connect (button, "clicked", G_CALLBACK (remove_child), NULL); hbox = gtk_hbox_new (FALSE, 0); gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0); gtk_widget_show_all (window); gtk_main (); wait_for_children (); return 0; }