int my_systemv (const char *command, char *const argv[]) { my_fork_state_t fork_state; int status = 0; my_system_sigactions_t sigactions; my_system__save_sigaction_handlers (&sigactions); fork_state = my_fork (); switch (fork_state) { case FORK_ERROR: status = -1; break; case FORK_CHILD: { signal (SIGINT, SIG_DFL); signal (SIGQUIT, SIG_DFL); signal (SIGTSTP, SIG_DFL); signal (SIGCHLD, SIG_DFL); execvp (command, argv); my_exit (127); /* Exec error */ } break; default: status = 0; break; } my_system__restore_sigaction_handlers (&sigactions); return status; }
int do_snapshot(int parent_fd) { int fds[2]; struct command_response resp; long child; int r; r = socketpair(AF_UNIX, SOCK_STREAM, 0, fds); if (r < 0) VG_(tool_panic)((Char *)"error creating socket pair"); child = my_fork(); if (child < 0) VG_(tool_panic)((Char *)"forking snapshot"); if (child == 0) { VG_(close)(fds[1]); VG_(close)(parent_fd); return fds[0]; } VG_(close)(fds[0]); resp.res = 0; safeish_write(parent_fd, &resp, sizeof(resp)); send_fd(parent_fd, fds[1]); VG_(close)(fds[1]); return parent_fd; }
int main(int argc, const char *argv[]) { int fd_1, fd_2; int len; int i; struct stat buf; fd_1 = open("file1",O_RDONLY); fd_2 = open("file2",O_RDWR | O_CREAT, 0777); fstat(fd_1, &buf); len = buf.st_size; printf("%d\n",len); len_sub = len/N; len_end = len%N; for (i = 0; i < N; i++) { my_fork(len_sub,fd_1,fd_2,i); } close(fd_1); close(fd_2); return 0; }
int main() { if(my_fork()) { printf("I'm parent: My PID=%d\n",my_getpid()); } else { printf("I'm child: My PID=%d and my parent %d\n", my_getpid(), my_getppid()); } }
void end_pipe(t_param exe, int pipe_pre[2], t_shell *sh) { int pid; pid = my_fork(); if (pid == 0) { my_dup2(pipe_pre[0], 0); my_execve(exe, sh); } free(pipe_pre); }
void my_cmd(t_list *list, char *s, char **env, int i) { char *epur_s; char **tab; epur_s = epur_str(s); tab = my_str_to_wordtab(epur_s, &i); if ((my_builtin(list, s, tab, env)) == 0 && \ check_redir(epur_s, tab, env, i) == 0) my_fork(tab[0], tab, env); if (check_env(tab, env) == 1) env = env_cpy(list); check_recurs(list, s, env, i - 1); my_mini_free(epur_s, tab); }
int *first_pipe(t_param exe, t_shell *sh) { int *pipe_next; int pid; pipe_next = malloc(2 * sizeof(int)); my_pipe(pipe_next); pid = my_fork(); if (pid == 0) { close(pipe_next[0]); my_dup2(pipe_next[1], 1); my_execve(exe, sh); } close(pipe_next[1]); return (pipe_next); }
int *between_pipe(t_param exe, int pipe_pre[2], t_shell *sh) { int *pipe_next; int pid; pipe_next = malloc(2 * sizeof(int)); my_pipe(pipe_next); pid = my_fork(); if (pid == 0) { close(pipe_next[0]); my_dup2(pipe_next[1], 1); my_dup2(pipe_pre[0], 0); my_execve(exe, sh); } close(pipe_next[1]); free(pipe_pre); return (pipe_next); }
int ui_loop(void) { int child; int fds[2]; int r; Char buf[16]; Char *args[7] = { (Char *)"ppres/driver/UI", "+RTS", "-p", "-xc", "-RTS", buf, NULL }; r = socketpair(AF_UNIX, SOCK_STREAM, 0, fds); if (r < 0) VG_(tool_panic)((Char *)"Error creating socket pair"); child = my_fork(); if (child == 0) { struct vki_sigaction_base sa; VG_(close)(fds[1]); /* We're going to be doing a lot more forking, and don't want lots of zombies hanging around. Child crash is detected when a control socket closes unexpectedly. */ VG_(memset)(&sa, 0, sizeof(sa)); sa.ksa_handler = VKI_SIG_IGN; sa.sa_flags = 3; /* SA_NOCLDSTOP|SA_NOCLDWAIT */ my_sigaction(VKI_SIGCHLD, &sa, NULL); return fds[0]; } VG_(close)(fds[0]); VG_(sprintf)(buf, "%d", fds[1]); VG_(execv)(args[0], args); VG_(tool_panic)((Char *)"Cannot exec driver!"); }
int main(int argc, char **argv) { int listenfd = 0; int connfd = 0; pid_t childpid ; socklen_t clilen; struct sockaddr_in cliaddr; struct sockaddr_in servaddr; listenfd = my_socket(AF_INET, SOCK_STREAM, 0); signal(SIGCHLD, tu5_11sig_chld); /// bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); my_bind(listenfd, (SA *)&servaddr, sizeof(servaddr)); my_listen(listenfd, LISTENQ); while(1) { clilen = sizeof(cliaddr); if ( (connfd = my_accept(listenfd, (SA*)&cliaddr, &clilen)) < 0) { } if ((childpid = my_fork()) == 0) { my_close(listenfd); tu5_17str_echo(connfd); exit(0); } my_close(connfd); } return 0; }
pid_t execute_and_distrib(t_shell *sh, t_command *c) { pid_t child; pid_t child2; int pip[2][2]; int pip_sh[2][2]; do_pipes(pip, pip_sh); child = my_fork(); if (!child) { execute_all(sh, c, pip_sh, pip); handle_error_messages(sh); return (quit(sh, sh->last_status)); } else { prepa_exec(sh, c, pip_sh, pip); child2 = distrib_and_send(sh, c, pip[0][0], pip[1][0]); wait_exec(child2, pip); return (child); } }
pid_t receipt_and_execute(t_shell *sh, t_command *c, int receipt_fd) { pid_t child; int pip[2]; pipe(pip); decompose_command(sh, c->line, c); child = my_fork(); if (child) { if (!is_default_in(c->fd_in, c->list_in, c)) dup2(pip[0], 0); do_execute_and_distrib(sh, c, pip); return (child); } else { close(pip[0]); if (!is_default_in(c->fd_in, c->list_in, c)) pipe_in(c->list_in, c, pip[1], receipt_fd); close(pip[1]); return (quit(sh, 0)); } }
pid_t my_fork(void) { pid_t pid = -1; int try; for (try = 0; try < 10; try++) { errno = 0; pid = fork(); if (pid != -1 || errno != EAGAIN) return pid; err("fork: retry: Resource temporarily unavailable\n"); usleep(100 * 1000); } return pid; } void m__system(char **argv, int flags, const char *res_name, pid_t *kid, int *fd, int *ex) { pid_t pid; int status, rv = -1; int timeout = 0; char **cmdline = argv; int pipe_fds[2]; struct sigaction so; struct sigaction sa; if (flags & (RETURN_STDERR_FD | RETURN_STDOUT_FD)) assert(fd); sa.sa_handler = &alarm_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; if (dry_run || verbose) { if (sh_varname && *cmdline) printf("%s=%s\n", sh_varname, res_name ? shell_escape(res_name) : ""); while (*cmdline) { printf("%s ", shell_escape(*cmdline++)); } printf("\n"); if (dry_run) { if (kid) *kid = -1; if (fd) *fd = -1; if (ex) *ex = 0; return; } } /* flush stdout and stderr, so output of drbdadm * and helper binaries is reported in order! */ fflush(stdout); fflush(stderr); if (adjust_with_progress && !(flags & RETURN_STDERR_FD)) flags |= SUPRESS_STDERR; /* create the pipe in any case: * it helps the analyzer and later we have: * '*fd = pipe_fds[0];' */ if (pipe(pipe_fds) < 0) { perror("pipe"); fprintf(stderr, "Error in pipe, giving up.\n"); exit(E_EXEC_ERROR); } pid = my_fork(); if (pid == -1) { fprintf(stderr, "Can not fork\n"); exit(E_EXEC_ERROR); } if (pid == 0) { #ifndef _WIN32 prctl(PR_SET_PDEATHSIG, SIGKILL); #endif /* Child: close reading end. */ close(pipe_fds[0]); if (flags & RETURN_STDOUT_FD) { dup2(pipe_fds[1], STDOUT_FILENO); } if (flags & RETURN_STDERR_FD) { dup2(pipe_fds[1], STDERR_FILENO); } close(pipe_fds[1]); if (flags & SUPRESS_STDERR) { FILE *f = freopen("/dev/null", "w", stderr); if (!f) fprintf(stderr, "freopen(/dev/null) failed\n"); } if (argv[0]) { #ifdef _WIN32 // DW-1203 execvp() run with the full path. char path[256]; char *temp = strdup(argv[0]); char *ptr, *name; /* DW-1425: it's supposed to run any application that belongs to the paths have been set in 'path' env var as long as this is able to parse env vars. since cygwin is NOT, preferentially search full path that is gotton from env var of drbd and drx. */ #define DRBDADM_MAX_ENV_LEN 64 char envs[][DRBDADM_MAX_ENV_LEN] = { "DRBD_PATH", "DRX_PATH", "" }; int i = 0; // remove /usr/bin/ name = ptr = strtok(temp, "/"); while (ptr = strtok(NULL, "/")) { name = ptr; } for (i = 0; i < sizeof(envs) / DRBDADM_MAX_ENV_LEN; i++) { if (i == (sizeof(envs) / DRBDADM_MAX_ENV_LEN) - 1) strcpy(path, name); else sprintf(path, "%s\\%s", getenv(envs[i]), name); if (!execvp(path, argv)) break; } #else execvp(argv[0], argv); #endif } #ifdef _WIN32 fprintf(stderr, "Can not exec %s\n", argv[0]); perror("Failed"); #else fprintf(stderr, "Can not exec\n"); #endif exit(E_EXEC_ERROR); } /* Parent process: close writing end. */ close(pipe_fds[1]); if (flags & SLEEPS_FINITE) { sigaction(SIGALRM, &sa, &so); alarm_raised = 0; switch (flags & SLEEPS_MASK) { case SLEEPS_SHORT: timeout = global_options.cmd_timeout_short; timeout = timeout * 2; // DW-1280 adjust alarm timeout. break; case SLEEPS_LONG: timeout = global_options.cmd_timeout_medium; break; case SLEEPS_VERY_LONG: timeout = global_options.cmd_timeout_long; break; default: fprintf(stderr, "logic bug in %s:%d\n", __FILE__, __LINE__); exit(E_THINKO); } alarm(timeout); } if (kid) *kid = pid; if (flags & (RETURN_STDOUT_FD | RETURN_STDERR_FD) || flags == RETURN_PID) { if (fd) *fd = pipe_fds[0]; return; } while (1) { if (waitpid(pid, &status, 0) == -1) { if (errno != EINTR) break; if (alarm_raised) { alarm(0); sigaction(SIGALRM, &so, NULL); rv = 0x100; break; } else { fprintf(stderr, "logic bug in %s:%d\n", __FILE__, __LINE__); exit(E_EXEC_ERROR); } } else { if (WIFEXITED(status)) { rv = WEXITSTATUS(status); break; } } } /* Do not close earlier, else the child gets EPIPE. */ close(pipe_fds[0]); if (flags & SLEEPS_FINITE) { if (rv >= 10 && !(flags & (DONT_REPORT_FAILED | SUPRESS_STDERR))) { fprintf(stderr, "Command '"); for (cmdline = argv; *cmdline; cmdline++) { fprintf(stderr, "%s", *cmdline); if (cmdline[1]) fputc(' ', stderr); } if (alarm_raised) { fprintf(stderr, "' did not terminate within %u seconds\n", timeout); exit(E_EXEC_ERROR); } else { fprintf(stderr, "' terminated with exit code %d\n", rv); } } } fflush(stdout); fflush(stderr); if (ex) *ex = rv; }
int main(int argc, char *argv[]) { pid_t pid; char *line, *arg_list[255], name[BUFSIZ]; char *tok; int status, i; int bgfg; line = malloc(sizeof(char) * 255); printf("\n\n\t***************************\n"); printf("\t* Welcome to our Shell! *\n"); printf("\t* 'exit' or 'quit' to *\n"); printf("\t* exit! Enjoy! *\n"); printf("\t***************************\n\n"); while (1) { printf("LainShell: "); fflush(stdout); fgets(line, 255, stdin); // If the last character is a newline we set it to a null-terminator if (line[strlen(line) - 1] == '\n') line[strlen(line) - 1] = '\0'; if (strlen(line) == 0) continue; // Check for a pipe, if there is we'll go straight into the exec_pipe // function. For consistency we can't really reuse the below parent / child // code but we can reuse the process_args function. if (strstr(line, "|")) { exec_pipe(line); // Execute the pipe'd command and go to the next iteration of the while // loop. continue; } // Tokenize based on ; tok = strtok(line, ";"); while (tok != NULL) { // Save our position in the line so when we retokenize we're at the next // token. We can't use NULL because strtok in process_args overwrites it. line += strlen(tok) + 1; process_args(tok, arg_list, &bgfg); tok = strtok(line, ";"); // If they typed exit or quit..we exit if (strcmp(arg_list[0], "exit") == 0 || strcmp(arg_list[0], "quit") == 0) exit(0); pid = my_fork(); if (pid == -1) { perror("Fork failed!\n"); exit(1); } else if (pid > 0) { // If this flag was set by process args we run in the background if (bgfg == 1) { waitpid(pid, &status, WNOHANG); } else { // Otherwise the foreground wait(&status); } } else { if (execvp(arg_list[0], arg_list) == -1) { #ifdef MY_DEBUG DEBUG(arg_list[0]); #endif int i = 0; while (arg_list[i] != NULL) printf("%d:\t%s\n", i, arg_list[i++]); perror("Child process could not exec"); exit(22); } } } } return 0; }