source: roaraudio/roard/lib.c @ 198:168dadfccd08

Last change on this file since 198:168dadfccd08 was 198:168dadfccd08, checked in by phi, 16 years ago

added closefh, lenclose parameters to lib_run_bg to support to close fhs we do not know about

File size: 1.2 KB
Line 
1//lib.c:
2
3#include "roard.h"
4
5int lib_run_bg(char * cmd, int infh, int outfh, int errfh, int * closefh, int lenclose) {
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 midi_free(); // close midi devices
35
36 // close fh's we got ask to close:
37 for (i = 0; i < lenclose; i++)
38  close(closefh[i]);
39
40 // TODO: what aout the output driver?
41
42 // next we need to remap our stdio:
43 // stdio: fh 0..2
44
45 for (i = 0; i < 3; i++) {
46  close(i);
47  dup2(fh[i], i); // todo test if this is ok.
48  close(fh[i]);
49 }
50
51 // OK, now we should have set up all our fh properbly, exec our command:
52
53 execl("/bin/sh", "sh", "-c", cmd, NULL);
54
55 // still alive? BAD!
56 ROAR_ERR("lib_run_bg(*): We are still alive! BAD!");
57 _exit(3);
58 return -1;
59}
60
61//ll
Note: See TracBrowser for help on using the repository browser.