source: roaraudio/roard/lib.c @ 1500:789ffe085574

Last change on this file since 1500:789ffe085574 was 1500:789ffe085574, checked in by phi, 15 years ago

test for ROAR_HAVE_IO_POSIX and ROAR_HAVE_FORK, only close listening socket if we have ROAR_SUPPORT_LISTEN

File size: 2.3 KB
RevLine 
[181]1//lib.c:
2
[668]3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
[181]25#include "roard.h"
26
[198]27int lib_run_bg(char * cmd, int infh, int outfh, int errfh, int * closefh, int lenclose) {
[1500]28#ifdef ROAR_HAVE_FORK
[182]29 pid_t child = fork();
30 int fh[3] = {-1, -1, -1};
31 int i;
32
33 if ( child == -1 ) {
34  ROAR_ERR("lib_run_bg(*): Can not fork: %s", strerror(errno));
35  return -1;
36 }
37
38 if ( child ) {
39  // we are the parent!
40  return child;
41 }
42
43 // we are the child.
44 // first we need to close a lot of open files!
45
46 // before we do this we need to keep backups of our handles:
47 fh[0] = dup(infh);
48 fh[1] = dup(outfh);
49 fh[2] = dup(errfh);
50
51 // TODO: test for errors here.
52
[1500]53#ifdef ROAR_SUPPORT_LISTEN
[182]54 close(g_listen_socket); // listen socket.
[1500]55#endif
[182]56
[1197]57// this breaks the new driver interface
58// clients_free(); // delete all clients!, this allso delets all streams
[182]59
[186]60 midi_free(); // close midi devices
61
[198]62 // close fh's we got ask to close:
[1500]63#ifdef ROAR_HAVE_IO_POSIX
[198]64 for (i = 0; i < lenclose; i++)
65  close(closefh[i]);
[1500]66#else
67 if ( lenclose ) {
68  ROAR_WARN("lib_run_bg(*): lenclose > 0 and no way known to close fds");
69 }
70#endif
[198]71
[182]72 // next we need to remap our stdio:
73 // stdio: fh 0..2
74
75 for (i = 0; i < 3; i++) {
[1500]76#ifdef ROAR_HAVE_IO_POSIX
[182]77  close(i);
[1500]78#endif
[182]79  dup2(fh[i], i); // todo test if this is ok.
[184]80  close(fh[i]);
[182]81 }
82
83 // OK, now we should have set up all our fh properbly, exec our command:
84
85 execl("/bin/sh", "sh", "-c", cmd, NULL);
86
87 // still alive? BAD!
88 ROAR_ERR("lib_run_bg(*): We are still alive! BAD!");
[1500]89 ROAR_U_EXIT(3);
[182]90 return -1;
[1500]91#else
92 return -1;
93#endif
[182]94}
95
[181]96//ll
Note: See TracBrowser for help on using the repository browser.