source: roaraudio/roard/lib.c @ 3517:1a3218a3fc5b

Last change on this file since 3517:1a3218a3fc5b was 3517:1a3218a3fc5b, checked in by phi, 14 years ago

updated license headers, FSF moved office

File size: 2.5 KB
Line 
1//lib.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "roard.h"
27
28int lib_run_bg(char * cmd, int infh, int outfh, int errfh, int * closefh, int lenclose) {
29#ifdef ROAR_HAVE_FORK
30 pid_t child = fork();
31 int fh[3] = {-1, -1, -1};
32 int i;
33
34 if ( child == -1 ) {
35  ROAR_ERR("lib_run_bg(*): Can not fork: %s", strerror(errno));
36  return -1;
37 }
38
39 if ( child ) {
40  // we are the parent!
41  return child;
42 }
43
44 // we are the child.
45 // first we need to close a lot of open files!
46
47 // before we do this we need to keep backups of our handles:
48 fh[0] = dup(infh);
49 fh[1] = dup(outfh);
50 fh[2] = dup(errfh);
51
52 // TODO: test for errors here.
53
54#ifdef ROAR_SUPPORT_LISTEN
55 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++)
56  if ( g_listen[i].socket != -1 )
57   close(g_listen[i].socket); // listen socket.
58#endif
59
60// this breaks the new driver interface
61// clients_free(); // delete all clients!, this allso delets all streams
62
63#ifndef ROAR_WITHOUT_DCOMP_MIDI
64 midi_free(); // close midi devices
65#endif
66
67 // close fh's we got ask to close:
68#ifdef ROAR_HAVE_IO_POSIX
69 for (i = 0; i < lenclose; i++)
70  close(closefh[i]);
71#else
72 if ( lenclose ) {
73  ROAR_WARN("lib_run_bg(*): lenclose > 0 and no way known to close fds");
74 }
75#endif
76
77 // next we need to remap our stdio:
78 // stdio: fh 0..2
79
80 for (i = 0; i < 3; i++) {
81#ifdef ROAR_HAVE_IO_POSIX
82  close(i);
83#endif
84  dup2(fh[i], i); // todo test if this is ok.
85  close(fh[i]);
86 }
87
88 // OK, now we should have set up all our fh properbly, exec our command:
89
90 execl("/bin/sh", "sh", "-c", cmd, (char*)NULL);
91
92 // still alive? BAD!
93 ROAR_ERR("lib_run_bg(*): We are still alive! BAD!");
94 ROAR_U_EXIT(3);
95 return -1;
96#else
97 return -1;
98#endif
99}
100
101//ll
Note: See TracBrowser for help on using the repository browser.