[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] | 27 | int lib_run_bg(char * cmd, int infh, int outfh, int errfh, int * closefh, int lenclose) { |
---|
[182] | 28 | pid_t child = fork(); |
---|
| 29 | int fh[3] = {-1, -1, -1}; |
---|
| 30 | int i; |
---|
| 31 | |
---|
| 32 | if ( child == -1 ) { |
---|
| 33 | ROAR_ERR("lib_run_bg(*): Can not fork: %s", strerror(errno)); |
---|
| 34 | return -1; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | if ( child ) { |
---|
| 38 | // we are the parent! |
---|
| 39 | return child; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | // we are the child. |
---|
| 43 | // first we need to close a lot of open files! |
---|
| 44 | |
---|
| 45 | // before we do this we need to keep backups of our handles: |
---|
| 46 | fh[0] = dup(infh); |
---|
| 47 | fh[1] = dup(outfh); |
---|
| 48 | fh[2] = dup(errfh); |
---|
| 49 | |
---|
| 50 | // TODO: test for errors here. |
---|
| 51 | |
---|
| 52 | close(g_listen_socket); // listen socket. |
---|
| 53 | |
---|
| 54 | clients_free(); // delete all clients!, this allso delets all streams |
---|
| 55 | |
---|
[186] | 56 | midi_free(); // close midi devices |
---|
| 57 | |
---|
[198] | 58 | // close fh's we got ask to close: |
---|
| 59 | for (i = 0; i < lenclose; i++) |
---|
| 60 | close(closefh[i]); |
---|
| 61 | |
---|
[182] | 62 | // TODO: what aout the output driver? |
---|
| 63 | |
---|
| 64 | // next we need to remap our stdio: |
---|
| 65 | // stdio: fh 0..2 |
---|
| 66 | |
---|
| 67 | for (i = 0; i < 3; i++) { |
---|
| 68 | close(i); |
---|
| 69 | dup2(fh[i], i); // todo test if this is ok. |
---|
[184] | 70 | close(fh[i]); |
---|
[182] | 71 | } |
---|
| 72 | |
---|
| 73 | // OK, now we should have set up all our fh properbly, exec our command: |
---|
| 74 | |
---|
| 75 | execl("/bin/sh", "sh", "-c", cmd, NULL); |
---|
| 76 | |
---|
| 77 | // still alive? BAD! |
---|
| 78 | ROAR_ERR("lib_run_bg(*): We are still alive! BAD!"); |
---|
| 79 | _exit(3); |
---|
| 80 | return -1; |
---|
| 81 | } |
---|
| 82 | |
---|
[181] | 83 | //ll |
---|