source: roaraudio/roard/lib.c @ 5568:a7bdf096a4d6

Last change on this file since 5568:a7bdf096a4d6 was 5568:a7bdf096a4d6, checked in by phi, 12 years ago

added use of const, use size_t not int

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