summaryrefslogtreecommitdiff
path: root/xpp/astribank_is_starting.c
diff options
context:
space:
mode:
authorTzafrir Cohen <tzafrir.cohen@xorcom.com>2009-08-13 15:15:49 +0000
committerTzafrir Cohen <tzafrir.cohen@xorcom.com>2009-08-13 15:15:49 +0000
commita114167f3ee6db22217d70a48267871ee08e6c4b (patch)
tree681330f32689007ff556b2d3372a391b840f812d /xpp/astribank_is_starting.c
parentabe5babe4742bf03b49971407b82ec3bc02fb14c (diff)
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
Diffstat (limited to 'xpp/astribank_is_starting.c')
-rw-r--r--xpp/astribank_is_starting.c120
1 files changed, 120 insertions, 0 deletions
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 <sys/types.h>
+#include <sys/ipc.h>
+#include <sys/sem.h>
+#include <errno.h>
+#include <getopt.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+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;
+}