~ubuntu-branches/ubuntu/saucy/dahdi-tools/saucy

« back to all changes in this revision

Viewing changes to xpp/astribank_is_starting.c

  • Committer: Bazaar Package Importer
  • Author(s): Jean-Michel Dault, Tzafrir Cohen
  • Date: 2010-02-16 13:44:09 UTC
  • mfrom: (1.1.2 upstream) (2.1.1 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100216134409-4y4k26mgzyra537o
Tags: 1:2.2.1-0ubuntu1
* Merge from Debian pkg-voip.
  * Changes from Debian:
  - debian/control: Change Maintainer
  - debian/control: Removed Uploaders field.
  - debian/control: Removed Debian Vcs-Svn entry and replaced with
      ubuntu-voip Vcs-Bzr, to reflect divergence in packages.
  - debian/control: Package dahdi Depends on  dahdi-dkms | dahdi-source

* From Debian pkg-voip:
[ Tzafrir Cohen ]
* New upstream release (Closes: #536257, #564381).
* Patch 'bashism' dropped: merged upstream. 
* Patch xpp_no_extra_at dropped: merged upstream. 
* Add an example genconf_parameters.
* Compat level 7.
* Bump standars version to 3.8.3.0 (no change needed)
* Udev rules are now in dahdi-linux.
* Patches perl_fix_noserial, perl_fix_transportdir: Fixes for some
  minor perl issues.
* Add the missing ${misc:Depends}, as per lintian. 
* Patch astribank_allow_ignoreend: an extra missing patch from upstream. 
* Patches init_unload_modules and init_unload_oslec: also unload OSLEC
  when unloading all modules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#define _GNU_SOURCE /* 2.3 and later will define this from autoconfig.h */
 
2
#include <sys/types.h>
 
3
#include <sys/ipc.h>
 
4
#include <sys/sem.h>
 
5
#include <errno.h>
 
6
#include <getopt.h>
 
7
#include <stdio.h>
 
8
#include <stdlib.h>
 
9
#include <string.h>
 
10
#include <time.h>
 
11
 
 
12
static char             *progname;
 
13
static const key_t      key_astribanks = 0xAB11A0;
 
14
static int              debug;
 
15
static int              verbose;
 
16
static int              timeout_seconds = 60;
 
17
 
 
18
 
 
19
static void usage(void)
 
20
{
 
21
        fprintf(stderr, "Usage: %s [-d] [-t <seconds>] [-a|-r|-w]\n", progname);
 
22
        exit(1);
 
23
}
 
24
 
 
25
static int absem_get(int createit)
 
26
{
 
27
        int     flags = (createit) ? IPC_CREAT | 0644 : 0;
 
28
        int     absem;
 
29
 
 
30
        if((absem = semget(key_astribanks, 1, flags)) < 0)
 
31
                absem = -errno;
 
32
        return absem;
 
33
}
 
34
 
 
35
static int absem_touch(void)
 
36
{
 
37
        int             absem;
 
38
 
 
39
        if((absem = absem_get(1)) < 0) {
 
40
                perror(__FUNCTION__);
 
41
                return absem;
 
42
        }
 
43
        if(semctl(absem, 0, SETVAL, 0) < 0) {
 
44
                perror("SETVAL");
 
45
                return -errno;
 
46
        }
 
47
        if(debug)
 
48
                fprintf(stderr, "%s: touched absem\n", progname);
 
49
        if(verbose)
 
50
                printf("Astribanks initialization is starting\n");
 
51
        return 0;
 
52
}
 
53
 
 
54
static int absem_remove(void)
 
55
{
 
56
        int     absem;
 
57
 
 
58
        if((absem = absem_get(0)) < 0) {
 
59
                if(absem == -ENOENT) {
 
60
                        if(debug)
 
61
                                fprintf(stderr, "%s: absem already removed\n", progname);
 
62
                        return 0;
 
63
                }
 
64
                perror(__FUNCTION__);
 
65
                return absem;
 
66
        }
 
67
        if(semctl(absem, 0, IPC_RMID, 0) < 0) {
 
68
                perror("RMID");
 
69
                return -errno;
 
70
        }
 
71
        if(debug)
 
72
                fprintf(stderr, "%s: removed absem\n", progname);
 
73
        if(verbose)
 
74
                printf("Astribanks initialization is done\n");
 
75
        return 0;
 
76
}
 
77
 
 
78
static int absem_wait(void)
 
79
{
 
80
        int             absem;
 
81
        struct sembuf   sops;
 
82
        long            now;
 
83
        long            start_wait;
 
84
        struct timespec timeout;
 
85
 
 
86
        if((absem = absem_get(0)) < 0) {
 
87
                perror(__FUNCTION__);
 
88
                return absem;
 
89
        }
 
90
        sops.sem_num = 0;
 
91
        sops.sem_op = -1;
 
92
        sops.sem_flg = 0;
 
93
        start_wait = time(NULL);
 
94
        timeout.tv_sec = timeout_seconds;
 
95
        timeout.tv_nsec = 0;
 
96
        if(semtimedop(absem, &sops, 1, &timeout) < 0) {
 
97
                switch(errno) {
 
98
                case EIDRM:     /* Removed -- OK */
 
99
                        break;
 
100
                case EAGAIN:    /* Timeout -- Report */
 
101
                        fprintf(stderr, "Astribanks waiting timed out\n");
 
102
                        return -errno;
 
103
                default:        /* Unexpected errors */
 
104
                        perror("semop");
 
105
                        return -errno;
 
106
                }
 
107
                /* fall-thgough */
 
108
        }
 
109
        now = time(NULL);
 
110
        if(debug)
 
111
                fprintf(stderr, "%s: waited on absem %ld seconds\n", progname, now - start_wait);
 
112
        if(verbose)
 
113
                printf("Finished after %ld seconds\n", now - start_wait);
 
114
        return 0;
 
115
}
 
116
 
 
117
static int absem_detected(void)
 
118
{
 
119
        int     absem;
 
120
 
 
121
        if((absem = absem_get(0)) < 0) {
 
122
                if(debug)
 
123
                        fprintf(stderr, "%s: absem does not exist\n", progname);
 
124
                return absem;
 
125
        }
 
126
        if(debug)
 
127
                fprintf(stderr, "%s: absem exists\n", progname);
 
128
        if(verbose)
 
129
                printf("Astribanks are initializing...\n");
 
130
        return 0;
 
131
}
 
132
 
 
133
int main(int argc, char *argv[])
 
134
{
 
135
        const char      options[] = "dvarwt:h";
 
136
        int             val;
 
137
 
 
138
        progname = argv[0];
 
139
        while (1) {
 
140
                int     c;
 
141
                int     t;
 
142
 
 
143
                c = getopt (argc, argv, options);
 
144
                if (c == -1)
 
145
                        break;
 
146
 
 
147
                switch (c) {
 
148
                        case 'd':
 
149
                                debug++;
 
150
                                break;
 
151
                        case 'v':
 
152
                                verbose++;
 
153
                                break;
 
154
                        case 't':
 
155
                                t = atoi(optarg);
 
156
                                if(t <= 0) {
 
157
                                        fprintf(stderr,
 
158
                                                "%s: -t expect a positive number of seconds: '%s'\n",
 
159
                                                progname, optarg);
 
160
                                        usage();
 
161
                                }
 
162
                                timeout_seconds = t;
 
163
                                break;
 
164
                        case 'a':
 
165
                                if((val = absem_touch()) < 0) {
 
166
                                        fprintf(stderr, "%s: Add failed: %d\n", progname, val);
 
167
                                        return 1;
 
168
                                }
 
169
                                return 0;
 
170
                        case 'r':
 
171
                                if((val = absem_remove()) < 0) {
 
172
                                        fprintf(stderr, "%s: Remove failed: %d\n", progname, val);
 
173
                                        return 1;
 
174
                                }
 
175
                                return 0;
 
176
                        case 'w':
 
177
                                if((val = absem_wait()) < 0) {
 
178
                                        fprintf(stderr, "%s: Wait failed: %d\n", progname, val);
 
179
                                        return 1;
 
180
                                }
 
181
                                return 0;
 
182
                        case 'h':
 
183
                        default:
 
184
                                fprintf(stderr, "Unknown option '%c'\n", c);
 
185
                                usage();
 
186
                }
 
187
        }
 
188
        val = absem_detected();
 
189
        return (val == 0) ? 0 : 1;
 
190
}