Post on 14-Jun-2015
2. Outline
3. Applicability
4. errno
5. Process
6. Process Relationships
7. fork A new process
pid = fork(); if (pid < 0) { TRACE(("Fork failed, errno=%d", errno)); if (jp) freejob(jp); ash_msg_and_raise_error("can't fork"); } if (pid == 0) forkchild(jp, /*n,*/ mode); else forkparent(jp, n, mode, pid);
8. exec Letting the child go
9. Exec Cont'd
10. User/Group Ids
11. Ids and fork/exec
12. setuid API
13. Config
14. Signals
15. exit Process termination
16. atexitstatic void mke2fs_clean_up(void) { if (ENABLE_FEATURE_CLEAN_UP && journal_device)free(journal_device); } int mke2fs_main (int argc, char **argv) { ....if (ENABLE_FEATURE_CLEAN_UP) atexit(mke2fs_clean_up); .... }
int atexit(void (*function)(void)); 17. wait Responsible parenting
18. Wait example sshd pid = fork(); if (pid == -1) { fatal("fork of unprivileged child failed"); } else if (pid != 0) { debug2("Network child is on pid %ld", (long)pid); close(pmonitor->m_recvfd); pmonitor->m_pid = pid; monitor_child_preauth(authctxt, pmonitor); close(pmonitor->m_sendfd); /* Sync memory */ monitor_sync(pmonitor); /* Wait for the child's exit status */ while (waitpid(pid, &status, 0) < 0) if (errno != EINTR) break; return (1); } else { 19. Job Control
20. Interesting Example -busybox/init.c if (pid > 0) { /* Parent - wait till the child is done */ bb_signals(0 + (1 0) session_close_by_pid(pid, status); child_terminated = 0; } sigprocmask(SIG_SETMASK, &oset, NULL); } 27. Versatile Signal Handling Interface
28. Example - inetd memset(&sa, 0, sizeof(sa)); sigaddset(&sa.sa_mask, SIGALRM); sigaddset(&sa.sa_mask, SIGCHLD); sigaddset(&sa.sa_mask, SIGHUP); sa.sa_handler = retry_network_setup; sigaction_set(SIGALRM, &sa); sa.sa_handler = reread_config_file; sigaction_set(SIGHUP, &sa); sa.sa_handler = reap_child; sigaction_set(SIGCHLD, &sa); sa.sa_handler = clean_up_and_exit; sigaction_set(SIGTERM, &sa); sa.sa_handler = clean_up_and_exit; sigaction_set(SIGINT, &sa); sa.sa_handler = SIG_IGN; sigaction(SIGPIPE, &sa, &saved_pipe_handler); static void clean_up_and_exit(int sig UNUSED_PARAM) { servtab_t *sep; /* XXX signal race walking sep list */ for (sep = serv_list; sep; sep = sep->se_next) { if (sep->se_fd == -1) continue; switch (sep->se_family) { case AF_UNIX: unlink(sep->se_service); break; default: /* case AF_INET, AF_INET6 */ #if ENABLE_FEATURE_INETD_RPC if (sep->se_wait == 1 && is_rpc_service(sep)) unregister_rpc(sep);/* XXX signal race */ #endif break; } if (ENABLE_FEATURE_CLEAN_UP) close(sep->se_fd); } remove_pidfile(_PATH_INETDPID); exit(EXIT_SUCCESS); } 29. Some Signal Notes
30. system/popen
31. Scheduling
32. Scheduling (for now)
33. Real-time Scheduling
34. Time 35. Alarms