[181] | 1 | //lib.c: |
---|
| 2 | |
---|
[668] | 3 | /* |
---|
[5961] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2014 |
---|
[668] | 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[668] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
[181] | 26 | #include "roard.h" |
---|
| 27 | |
---|
[5568] | 28 | int lib_run_bg(const char * cmd, const int infh, const int outfh, const int errfh, int * closefh, const size_t lenclose) { |
---|
[1500] | 29 | #ifdef ROAR_HAVE_FORK |
---|
[3802] | 30 | pid_t child; |
---|
[182] | 31 | int fh[3] = {-1, -1, -1}; |
---|
[5586] | 32 | size_t i; |
---|
[182] | 33 | |
---|
[5651] | 34 | ROAR_WARN("lib_run_bg(cmd='%s', ...): This function should never be called. Contact devels.", cmd); |
---|
[3802] | 35 | |
---|
[5619] | 36 | child = roar_fork(NULL); |
---|
[3802] | 37 | |
---|
[182] | 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. |
---|
[5619] | 49 | |
---|
| 50 | // first stop the watchdog. |
---|
| 51 | roar_watchdog_stop(); |
---|
| 52 | |
---|
| 53 | // then we need to close a lot of open files! |
---|
[182] | 54 | |
---|
| 55 | // before we do this we need to keep backups of our handles: |
---|
| 56 | fh[0] = dup(infh); |
---|
| 57 | fh[1] = dup(outfh); |
---|
| 58 | fh[2] = dup(errfh); |
---|
| 59 | |
---|
| 60 | // TODO: test for errors here. |
---|
| 61 | |
---|
[1500] | 62 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[2530] | 63 | for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) |
---|
[3802] | 64 | if ( g_listen[i].used ) |
---|
| 65 | roar_vio_close(&(g_listen[i].sock)); // listen socket. |
---|
[1500] | 66 | #endif |
---|
[182] | 67 | |
---|
[1197] | 68 | // this breaks the new driver interface |
---|
| 69 | // clients_free(); // delete all clients!, this allso delets all streams |
---|
[182] | 70 | |
---|
[2500] | 71 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
[186] | 72 | midi_free(); // close midi devices |
---|
[2500] | 73 | #endif |
---|
[186] | 74 | |
---|
[198] | 75 | // close fh's we got ask to close: |
---|
[1500] | 76 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[198] | 77 | for (i = 0; i < lenclose; i++) |
---|
| 78 | close(closefh[i]); |
---|
[1500] | 79 | #else |
---|
| 80 | if ( lenclose ) { |
---|
| 81 | ROAR_WARN("lib_run_bg(*): lenclose > 0 and no way known to close fds"); |
---|
| 82 | } |
---|
| 83 | #endif |
---|
[198] | 84 | |
---|
[182] | 85 | // next we need to remap our stdio: |
---|
| 86 | // stdio: fh 0..2 |
---|
| 87 | |
---|
| 88 | for (i = 0; i < 3; i++) { |
---|
[1500] | 89 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[182] | 90 | close(i); |
---|
[1500] | 91 | #endif |
---|
[182] | 92 | dup2(fh[i], i); // todo test if this is ok. |
---|
[184] | 93 | close(fh[i]); |
---|
[182] | 94 | } |
---|
| 95 | |
---|
| 96 | // OK, now we should have set up all our fh properbly, exec our command: |
---|
| 97 | |
---|
[1653] | 98 | execl("/bin/sh", "sh", "-c", cmd, (char*)NULL); |
---|
[182] | 99 | |
---|
| 100 | // still alive? BAD! |
---|
| 101 | ROAR_ERR("lib_run_bg(*): We are still alive! BAD!"); |
---|
[1500] | 102 | ROAR_U_EXIT(3); |
---|
[182] | 103 | return -1; |
---|
[1500] | 104 | #else |
---|
| 105 | return -1; |
---|
| 106 | #endif |
---|
[182] | 107 | } |
---|
| 108 | |
---|
[181] | 109 | //ll |
---|