source: roaraudio/roard/lib.c @ 184:101d2249eb15

Last change on this file since 184:101d2249eb15 was 184:101d2249eb15, checked in by phi, 16 years ago

close our stdio backup FHs

File size: 1.1 KB
Line 
1//lib.c:
2
3#include "roard.h"
4
5int lib_run_bg(char * cmd, int infh, int outfh, int errfh) {
6 pid_t child = fork();
7 int fh[3] = {-1, -1, -1};
8 int i;
9
10 if ( child == -1 ) {
11  ROAR_ERR("lib_run_bg(*): Can not fork: %s", strerror(errno));
12  return -1;
13 }
14
15 if ( child ) {
16  // we are the parent!
17  return child;
18 }
19
20 // we are the child.
21 // first we need to close a lot of open files!
22
23 // before we do this we need to keep backups of our handles:
24 fh[0] = dup(infh);
25 fh[1] = dup(outfh);
26 fh[2] = dup(errfh);
27
28 // TODO: test for errors here.
29
30 close(g_listen_socket); // listen socket.
31
32 clients_free(); // delete all clients!, this allso delets all streams
33
34 // TODO: what aout the output driver?
35
36 // next we need to remap our stdio:
37 // stdio: fh 0..2
38
39 for (i = 0; i < 3; i++) {
40  close(i);
41  dup2(fh[i], i); // todo test if this is ok.
42  close(fh[i]);
43 }
44
45 // OK, now we should have set up all our fh properbly, exec our command:
46
47 execl("/bin/sh", "sh", "-c", cmd, NULL);
48
49 // still alive? BAD!
50 ROAR_ERR("lib_run_bg(*): We are still alive! BAD!");
51 _exit(3);
52 return -1;
53}
54
55//ll
Note: See TracBrowser for help on using the repository browser.