Go to the source code of this file.
Enumerations | |
enum | bg_quit_t { BG_QUIT = 1, BG_ABORT } |
Functions | |
void | bg_prepare_split (void) |
void | bg_send_quit (bg_quit_t q) |
void | bg_do_split (void) |
enum bg_quit_t |
void bg_do_split | ( | void | ) |
Definition at line 230 of file bg.c.
References bg_do_detach(), BG_QUIT, bg_send_quit(), fatal, and fork_before_tcl().
Referenced by main().
00231 { 00232 if (fork_before_tcl()) { 00233 /* Tell our parent process to go away now, as we don't need it anymore. */ 00234 bg_send_quit(BG_QUIT); 00235 00236 } else { 00237 /* Split off a new process. */ 00238 int xx = fork(); 00239 00240 if (xx == -1) 00241 fatal("CANNOT FORK PROCESS.", 0); 00242 if (xx != 0) 00243 bg_do_detach(xx); 00244 } 00245 }
void bg_prepare_split | ( | void | ) |
Definition at line 129 of file bg.c.
References bg_do_detach(), BG_PARENT, BG_SPLIT, bg_t::child_pid, bg_comm_t::comm_data, bg_t::comm_recv, bg_t::comm_send, bg_comm_t::comm_type, fatal, fork_before_tcl(), pid_file, bg_t::state, and bg_comm_t::transferpf.
Referenced by main().
00130 { 00131 pid_t p; 00132 bg_comm_t message; 00133 00134 if (!fork_before_tcl()) 00135 return; 00136 00137 /* Create a pipe between parent and split process, fork to create a 00138 * parent and a split process and wait for messages on the pipe. */ 00139 { 00140 int comm_pair[2]; 00141 00142 if (pipe(comm_pair) < 0) 00143 fatal("CANNOT OPEN PIPE.", 0); 00144 00145 bg.comm_recv = comm_pair[0]; 00146 bg.comm_send = comm_pair[1]; 00147 } 00148 00149 p = fork(); 00150 if (p == -1) 00151 fatal("CANNOT FORK PROCESS.", 0); 00152 if (p == 0) { 00153 bg.state = BG_SPLIT; 00154 return; 00155 } else { 00156 bg.child_pid = p; 00157 bg.state = BG_PARENT; 00158 } 00159 00160 while (read(bg.comm_recv, &message, sizeof(message)) > 0) { 00161 switch (message.comm_type) { 00162 case BG_COMM_QUIT: 00163 bg_do_detach(p); 00164 break; 00165 case BG_COMM_ABORT: 00166 exit(1); 00167 break; 00168 case BG_COMM_TRANSFERPF: 00169 /* Now transferring file from split process. 00170 */ 00171 if (message.comm_data.transferpf.len >= 40) 00172 message.comm_data.transferpf.len = 40 - 1; 00173 /* Next message contains data. */ 00174 if (read(bg.comm_recv, pid_file, message.comm_data.transferpf.len) <= 0) 00175 goto error; 00176 pid_file[message.comm_data.transferpf.len] = 0; 00177 break; 00178 } 00179 } 00180 00181 error: 00182 /* We only reach this point in case of an error. 00183 */ 00184 fatal("COMMUNICATION THROUGH PIPE BROKE.", 0); 00185 }
void bg_send_quit | ( | bg_quit_t | q | ) |
Definition at line 209 of file bg.c.
References BG_PARENT, BG_QUIT, bg_send_pidfile(), BG_SPLIT, bg_t::child_pid, bg_t::comm_send, bg_comm_t::comm_type, fatal, fork_before_tcl(), and bg_t::state.
Referenced by bg_do_split(), do_arg(), fatal(), got_bus(), got_segv(), and main().
00210 { 00211 if (!fork_before_tcl()) 00212 return; 00213 00214 if (bg.state == BG_PARENT) { 00215 kill(bg.child_pid, SIGKILL); 00216 } else if (bg.state == BG_SPLIT) { 00217 bg_comm_t message; 00218 00219 if (q == BG_QUIT) { 00220 bg_send_pidfile(); 00221 message.comm_type = BG_COMM_QUIT; 00222 } else 00223 message.comm_type = BG_COMM_ABORT; 00224 /* Send message. */ 00225 if (write(bg.comm_send, &message, sizeof(message)) < 0) 00226 fatal("COMMUNICATION THROUGH PIPE BROKE.", 0); 00227 } 00228 }