From a114167f3ee6db22217d70a48267871ee08e6c4b Mon Sep 17 00:00:00 2001 From: Tzafrir Cohen Date: Thu, 13 Aug 2009 15:15:49 +0000 Subject: xpp: Add astribank_is_starting: astribank_is_running is used to tell when we may have an Astribank that is initializing (and may be re-enumerating and thus not listed as a device). It uses a semaphore as we can always write to one and we can't always write to a file. git-svn-id: http://svn.asterisk.org/svn/dahdi/tools/trunk@6987 a0bf4364-ded3-4de4-8d8a-66a801d63aff --- xpp/astribank_is_starting.c | 120 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 xpp/astribank_is_starting.c (limited to 'xpp/astribank_is_starting.c') diff --git a/xpp/astribank_is_starting.c b/xpp/astribank_is_starting.c new file mode 100644 index 0000000..3018904 --- /dev/null +++ b/xpp/astribank_is_starting.c @@ -0,0 +1,120 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +static char *progname; +static const key_t key_astribanks = 0xAB11A0; +static int debug; + +static void usage(void) +{ + fprintf(stderr, "Usage: %s [-d] [-a] [-r]\n", progname); + exit(1); +} + +static int absem_get(int createit) +{ + int flags = (createit) ? IPC_CREAT | 0644 : 0; + int absem; + + if((absem = semget(key_astribanks, 1, flags)) < 0) + absem = -errno; + return absem; +} + +static int absem_touch(void) +{ + int absem; + + if((absem = absem_get(1)) < 0) { + perror(__FUNCTION__); + return absem; + } + if(semctl(absem, 0, SETVAL, 0) < 0) { + perror("SETVAL"); + return -errno; + } + if(debug) + fprintf(stderr, "%s: touched absem\n", progname); + return 0; +} + +static int absem_remove(void) +{ + int absem; + + if((absem = absem_get(0)) < 0) { + if(absem == -ENOENT) { + if(debug) + fprintf(stderr, "%s: absem already removed\n", progname); + return 0; + } + perror(__FUNCTION__); + return absem; + } + if(semctl(absem, 0, IPC_RMID, 0) < 0) { + perror("RMID"); + return -errno; + } + if(debug) + fprintf(stderr, "%s: removed absem\n", progname); + return 0; +} + +static int absem_detected(void) +{ + int absem; + + if((absem = absem_get(0)) < 0) { + if(debug) + fprintf(stderr, "%s: absem does not exist\n", progname); + return absem; + } + if(debug) + fprintf(stderr, "%s: absem exists\n", progname); + return 0; +} + +int main(int argc, char *argv[]) +{ + const char options[] = "darh"; + int val; + + progname = argv[0]; + while (1) { + int c; + + c = getopt (argc, argv, options); + if (c == -1) + break; + + switch (c) { + case 'd': + debug++; + break; + case 'a': + if((val = absem_touch()) < 0) { + fprintf(stderr, "%s: Add failed: %d\n", progname, val); + return 1; + } + return 0; + case 'r': + if((val = absem_remove()) < 0) { + fprintf(stderr, "%s: Remove failed: %d\n", progname, val); + return 1; + } + return 0; + case 'h': + default: + fprintf(stderr, "Unknown option '%c'\n", c); + usage(); + } + } + val = absem_detected(); + return (val == 0) ? 0 : 1; +} -- cgit v1.2.3