summaryrefslogtreecommitdiff
path: root/xpp/astribank_is_starting.c
blob: 072004f31aebfe3b121587c97988b64a2fc221b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "../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;

/* If libc provides no timeout variant: try to do without it: */
#ifndef HAVE_SEMTIMEDOP
#define  semtimedop(sem, ops, n, timeout)    semop(sem, ops, n)
#endif

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;
}