int main(int argc, char* argv[]) { struct connection conn; iopoll_fd fds[2]; int selfpipe; int i; msg_debug_init(); testmode = getenv("TESTMODE") != 0; if ((shell_argv = malloc((argc + 3) * sizeof *argv)) == 0) die_oom(111); for (i = 1; i < argc; ++i) shell_argv[i-1] = argv[i]; for (; i < argc + 4; ++i) shell_argv[i-1] = 0; shell_argc = argc - 1; if ((path = getenv("PATH")) == 0) die1(111, "No PATH is set"); if ((devnull = open("/dev/null", O_RDWR)) == -1) die1sys(111, "Could not open \"/dev/null\""); if (!nonblock_on(0)) die1sys(111, "Could not set non-blocking status"); if ((selfpipe = selfpipe_init()) == -1) die1sys(111, "Could not create self-pipe"); init_slots(); connection_init(&conn, 0, 0); fds[0].fd = 0; fds[0].events = IOPOLL_READ; fds[1].fd = selfpipe; fds[1].events = IOPOLL_READ; for (;;) { if (iopoll_restart(fds, 2, -1) == -1) die1sys(111, "Poll failed"); if (fds[0].revents) if (connection_read(&conn, handle_packet) <= 0) break; if (fds[1].revents) { read(selfpipe, &i, 1); handle_child(WNOHANG); } } msg1("Waiting for remaining slots to complete"); while (slots_used > 0) handle_child(0); return 0; }
void handle_fork(pid_t pid, _Bool in_background, char *tokens[]) { if (pid == 0) { handle_child(tokens, in_background); } else if (pid < 0) { fprintf(stderr, "Please, make sure to specify a positive integer.\n"); } else { handle_parent(pid, in_background, tokens); } }
int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { if (argc == 1) // parent ACE_ASSERT (handle_parent (argv[0]) == 0); else ACE_ASSERT (handle_child () == 0); ACE_UNUSED_ARG (argv); return 0; }
void begin_session(session_t *sess) { /*开启接收带外数据*/ activate_oobinline(sess->ctrl_fd); /* int sockfds[2]; if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds) < 0) { ERR_EXIT("socketpair"); } */ /*初始化内部进程间通讯通道*/ priv_sock_init(sess); pid_t pid; pid = fork(); if (pid < 0) { ERR_EXIT("fork"); } if (pid == 0) { //ftp服务进程 /* close(sockfds[0]); sess->child_fd = sockfds[1]; */ /*设置子进程环境*/ priv_sock_set_child_context(sess); handle_child(sess); } else { //nobody进程 /* close(sockfds[1]); sess->parent_fd = sockfds[0]; */ /*设置父进程环境*/ priv_sock_set_parent_context(sess); handle_parent(sess); } }
void begin_session(session_t* psess) { priv_sock_init(psess); pid_t pid = fork(); if (pid == -1) ERR_EXIT("fork"); if (pid == 0) {//服务进程 priv_sock_set_child_context(psess); handle_child(psess); } else if(pid > 0) {//nobody进程 priv_sock_set_parent_context(psess); handle_parent(psess); } }
void begin_session(session_t *sess) { activate_oobinline(sess->ctrl_fd); priv_sock_init(sess); pid_t pid; pid = fork(); if(pid < 0) ERR_EXIT("fork"); if(pid == 0) { /* ftp process */ priv_sock_set_child_context(sess); handle_child(sess); }else { /* nobody process */ priv_sock_set_parent_context(sess); handle_parent(sess); } }
void begin_session(session_t *sess) { /* struct passwd *pw = getpwnam("nobody"); if(pw == NULL) return; if(setegid(pw->pw_gid) < 0) ERR_EXIT("setegid"); if(seteuid(pw->pw_uid) < 0) ERR_EXIT("seteuid");*/ int sockfds[2]; if(socketpair(PF_UNIX, SOCK_STREAM, 0, sockfds) < 0) ERR_EXIT("sockpair"); priv_sock_init(sess); pid_t pid; pid = fork(); if(pid < 0) ERR_EXIT("fork"); if (pid == 0) { /* close(sockfds[0]); sess->parent_fd = sockfds[1];*/ priv_sock_set_child_context(sess); handle_child(sess); } else { /* close(sockfds[1]); sess->child_fd = sockfds[0];*/ priv_sock_set_parent_context(sess); handle_parent(sess); } }
void server(int sock) { int pid; int cs; struct sockaddr_in csin; unsigned int cslen; while (1) { cs = accept(sock, (struct sockaddr *)&csin, &cslen); printf("Client connected:\n\tfamily: %i\n\tport: %u\n\taddr: %s\n", csin.sin_family, csin.sin_port, inet_ntoa(csin.sin_addr)); if (pid = fork(), pid == 0) { close(sock); while (chdir(SERVER_DIR) == -1) mkdir(SERVER_DIR, 0777) ? shell_perror("mkdir failed") : 1; server_root_dir(); handle_child(cs, csin.sin_port); } close(cs); } }
int handle_event(struct task *task) { int ret; if (!task) return 0; debug(DEBUG_EVENT, "+++ process pid=%d event: %d", task->pid, task->event.type); assert(task->stopped); if (task->defer_func) { ret = task->defer_func(task, task->defer_data); if (ret == RET_DELETED) return 1; task->defer_func = NULL; task->defer_data = NULL; goto out2; } struct event *event = &task->event; enum event_type type = event->type; switch (type) { case EVENT_NONE: ret = continue_task(task, task->event.e_un.signum); break; case EVENT_SIGNAL: ret = handle_signal(task); break; case EVENT_ABOUT_EXIT: ret = handle_about_exit(task); goto out1; case EVENT_EXIT: ret = handle_exit(task); break; case EVENT_EXIT_SIGNAL: ret = handle_exit_signal(task); break; case EVENT_FORK: case EVENT_VFORK: case EVENT_CLONE: ret = handle_child(task); break; case EVENT_EXEC: ret = handle_exec(task); break; case EVENT_BREAKPOINT: ret = handle_breakpoint(task); goto out2; case EVENT_NEW: ret = handle_new(task); break; default: fprintf(stderr, "fatal error, unknown event %d\n", type); abort(); } if (ret == RET_DELETED) return 1; if (ret != RET_DEFERED) { assert(task->event.type == EVENT_NONE); assert(task->stopped == 0); } out2: assert(task->is_new == 0); out1: return (ret < 0) ? ret : 0; }