1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
// ----------------------------------------------------------------------------
//
// Copyright (C) 2003-2013 Fons Adriaensen <fons@linuxaudio.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "extproc.h"
Extproc::Extproc (const char *shmname, size_t shmsize) :
_shmem (0)
{
if (shmsize)
{
sprintf (_shmname, "/%s-%d", shmname, getpid ());
_shmem = new Shmem (_shmname, shmsize, true);
}
else *_shmname = 0;
_procid = 0;
_procrc = 0;
}
Extproc::~Extproc (void)
{
wait ();
delete _shmem;
shm_unlink (_shmname);
}
int Extproc::start (const char *prefix, const char *procfile)
{
int p, k = 0;
if (prefix) _args [k++] = (char *) prefix;
_args [k++] = (char *) procfile;
_args [k++] = _shmname;
_args [k] = 0;
_procid = 0;
p = fork ();
if (p == 0)
{
if (execvp (_args [0], _args) < 0)
{
perror ("execvp");
return -1;
}
}
if (p < 0)
{
perror ("fork:");
return -2;
}
_procid = p;
_procrc = 0;
return 0;
}
int Extproc::kill (void)
{
if (_procid && ::kill (_procid, SIGINT))
{
perror ("kill:");
return -1;
}
return 0;
}
int Extproc::wait (void)
{
if (_procid && waitpid (_procid, &_procrc, 0) < 0)
{
perror ("wait:");
return -1;
}
return 0;
}
|