summaryrefslogtreecommitdiff
path: root/xpp/astribank_is_starting.c
diff options
context:
space:
mode:
authorTzafrir Cohen <tzafrir.cohen@xorcom.com>2009-11-04 20:32:25 +0000
committerTzafrir Cohen <tzafrir.cohen@xorcom.com>2009-11-04 20:32:25 +0000
commit9e682ffd0f98e23b83a2cb1dcfc4163a512111bb (patch)
tree21a96107d6bb45abbbadf03604d68fb15e9f400b /xpp/astribank_is_starting.c
parentef4399ff222f532ef427255e30ec9016b9e25c1e (diff)
Add optional "hotplug mode" to init scripts
The hotplug mode is only used in XPP_HOTPLUG_DAHDI=yes is set in /etc/dahdi/init.conf . If it is set and the system actually has Astribanks, most of the init script will actually be run from the context of a udev event. Merged revisions 6987,7014,7334-7335,7409,7476 via svnmerge from https://origsvn.digium.com/svn/dahdi/tools/trunk ........ r6987 | tzafrir | 2009-08-13 18:15:49 +0300 (Thu, 13 Aug 2009) | 9 lines 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. ........ r7014 | tzafrir | 2009-08-16 19:05:11 +0300 (Sun, 16 Aug 2009) | 4 lines xpp: A man page for astribank_is_starting xpp rev: 7321 ........ r7334 | tzafrir | 2009-10-04 15:35:51 +0200 (Sun, 04 Oct 2009) | 7 lines dahdi.init: move module loading to a separate function Move the module loading parts of the dahdi init script to a seperate function - load_modules. To be used by a later commit. There is not functional change here (but the diff is confusing). ........ r7335 | tzafrir | 2009-10-04 15:53:01 +0200 (Sun, 04 Oct 2009) | 16 lines Add XPP_HOTPLUG_DAHDI mode: cfg initiated mostly from hotplug If XPP_HOTPLUG_DAHDI=yes is set in /etc/dahdi/init.conf (which is not the default), the normal run of the 'start' operation of the dahdi init script will only be responsible for loading modules. The rest will be done (if there are indeed Astribanks on the system) by a second call to the init script once all of them have connected. The astribank_hook has also been mostly rewritten. Most of the functionality of twinstar_hook moved to it. The current twinstar_hook is a simple example script. XPP_HOTPLUG_DAHDI mode assumes that all Astribanks on the system are listed in /etc/dahdi/xpp_order (which can be generated using, e.g. 'dahdi_genconf xpporder') ........ r7409 | tzafrir | 2009-10-21 16:30:32 +0200 (Wed, 21 Oct 2009) | 21 lines Fix XPP_HOTPLUG_DAHDI: logic; end of init.d script 'Hotplug mode' was introduced in r7335. * The logic in the script was broken. - Negative logic is not such a grand idea to start with. * Interactive invocation of init.d ends when expected and not sooner. This change makes waitfor_xpds wait longer. Rather than waiting for all the Astribanks to load, it will now wait until the initialization of dahdi from the Astribanks hook script is run. This allows running e.g.: /etc/init.d/dahdi start; /etc/init.d/asterisk start It also means that 'astribank_is_starting' is actually used as a semaphore and not only as stamp file. As before, those changes have no effect if hotplug mode is not explicitly enabled (setting 'XPP_HOTPLUG_DAHDI=yes' in init.conf). ........ r7476 | tzafrir | 2009-11-04 22:12:35 +0200 (Wed, 04 Nov 2009) | 6 lines xpp: make astribank_is_starting build without warnings. autoconfig.h is needed as semtimedop() requires _GNU_SOURCE. xpp rev: 7471 ........ git-svn-id: http://svn.asterisk.org/svn/dahdi/tools/branches/2.2@7477 a0bf4364-ded3-4de4-8d8a-66a801d63aff
Diffstat (limited to 'xpp/astribank_is_starting.c')
-rw-r--r--xpp/astribank_is_starting.c190
1 files changed, 190 insertions, 0 deletions
diff --git a/xpp/astribank_is_starting.c b/xpp/astribank_is_starting.c
new file mode 100644
index 0000000..578ea9b
--- /dev/null
+++ b/xpp/astribank_is_starting.c
@@ -0,0 +1,190 @@
+#define _GNU_SOURCE /* 2.3 and later will define this from autoconfig.h */
+#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>
+#include <time.h>
+
+static char *progname;
+static const key_t key_astribanks = 0xAB11A0;
+static int debug;
+static int verbose;
+static int timeout_seconds = 60;
+
+
+static void usage(void)
+{
+ fprintf(stderr, "Usage: %s [-d] [-t <seconds>] [-a|-r|-w]\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);
+ if(verbose)
+ printf("Astribanks initialization is starting\n");
+ 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);
+ if(verbose)
+ printf("Astribanks initialization is done\n");
+ return 0;
+}
+
+static int absem_wait(void)
+{
+ int absem;
+ struct sembuf sops;
+ long now;
+ long start_wait;
+ struct timespec timeout;
+
+ if((absem = absem_get(0)) < 0) {
+ perror(__FUNCTION__);
+ return absem;
+ }
+ sops.sem_num = 0;
+ sops.sem_op = -1;
+ sops.sem_flg = 0;
+ start_wait = time(NULL);
+ timeout.tv_sec = timeout_seconds;
+ timeout.tv_nsec = 0;
+ if(semtimedop(absem, &sops, 1, &timeout) < 0) {
+ switch(errno) {
+ case EIDRM: /* Removed -- OK */
+ break;
+ case EAGAIN: /* Timeout -- Report */
+ fprintf(stderr, "Astribanks waiting timed out\n");
+ return -errno;
+ default: /* Unexpected errors */
+ perror("semop");
+ return -errno;
+ }
+ /* fall-thgough */
+ }
+ now = time(NULL);
+ if(debug)
+ fprintf(stderr, "%s: waited on absem %ld seconds\n", progname, now - start_wait);
+ if(verbose)
+ printf("Finished after %ld seconds\n", now - start_wait);
+ 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);
+ if(verbose)
+ printf("Astribanks are initializing...\n");
+ return 0;
+}
+
+int main(int argc, char *argv[])
+{
+ const char options[] = "dvarwt:h";
+ int val;
+
+ progname = argv[0];
+ while (1) {
+ int c;
+ int t;
+
+ c = getopt (argc, argv, options);
+ if (c == -1)
+ break;
+
+ switch (c) {
+ case 'd':
+ debug++;
+ break;
+ case 'v':
+ verbose++;
+ break;
+ case 't':
+ t = atoi(optarg);
+ if(t <= 0) {
+ fprintf(stderr,
+ "%s: -t expect a positive number of seconds: '%s'\n",
+ progname, optarg);
+ usage();
+ }
+ timeout_seconds = t;
+ 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 'w':
+ if((val = absem_wait()) < 0) {
+ fprintf(stderr, "%s: Wait 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;
+}