1
#define _GNU_SOURCE /* 2.3 and later will define this from autoconfig.h */
12
static char *progname;
13
static const key_t key_astribanks = 0xAB11A0;
16
static int timeout_seconds = 60;
19
static void usage(void)
21
fprintf(stderr, "Usage: %s [-d] [-t <seconds>] [-a|-r|-w]\n", progname);
25
static int absem_get(int createit)
27
int flags = (createit) ? IPC_CREAT | 0644 : 0;
30
if((absem = semget(key_astribanks, 1, flags)) < 0)
35
static int absem_touch(void)
39
if((absem = absem_get(1)) < 0) {
43
if(semctl(absem, 0, SETVAL, 0) < 0) {
48
fprintf(stderr, "%s: touched absem\n", progname);
50
printf("Astribanks initialization is starting\n");
54
static int absem_remove(void)
58
if((absem = absem_get(0)) < 0) {
59
if(absem == -ENOENT) {
61
fprintf(stderr, "%s: absem already removed\n", progname);
67
if(semctl(absem, 0, IPC_RMID, 0) < 0) {
72
fprintf(stderr, "%s: removed absem\n", progname);
74
printf("Astribanks initialization is done\n");
78
static int absem_wait(void)
84
struct timespec timeout;
86
if((absem = absem_get(0)) < 0) {
93
start_wait = time(NULL);
94
timeout.tv_sec = timeout_seconds;
96
if(semtimedop(absem, &sops, 1, &timeout) < 0) {
98
case EIDRM: /* Removed -- OK */
100
case EAGAIN: /* Timeout -- Report */
101
fprintf(stderr, "Astribanks waiting timed out\n");
103
default: /* Unexpected errors */
111
fprintf(stderr, "%s: waited on absem %ld seconds\n", progname, now - start_wait);
113
printf("Finished after %ld seconds\n", now - start_wait);
117
static int absem_detected(void)
121
if((absem = absem_get(0)) < 0) {
123
fprintf(stderr, "%s: absem does not exist\n", progname);
127
fprintf(stderr, "%s: absem exists\n", progname);
129
printf("Astribanks are initializing...\n");
133
int main(int argc, char *argv[])
135
const char options[] = "dvarwt:h";
143
c = getopt (argc, argv, options);
158
"%s: -t expect a positive number of seconds: '%s'\n",
165
if((val = absem_touch()) < 0) {
166
fprintf(stderr, "%s: Add failed: %d\n", progname, val);
171
if((val = absem_remove()) < 0) {
172
fprintf(stderr, "%s: Remove failed: %d\n", progname, val);
177
if((val = absem_wait()) < 0) {
178
fprintf(stderr, "%s: Wait failed: %d\n", progname, val);
184
fprintf(stderr, "Unknown option '%c'\n", c);
188
val = absem_detected();
189
return (val == 0) ? 0 : 1;