summaryrefslogtreecommitdiffstats
path: root/src/squeue/squeue.c
blob: 0ed8815d9639d3b0bbd31ce264093b9c3661910b (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "squeue.h"
#define _XOPEN_SOURCE 500     /* Or: #define _BSD_SOURCE */
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <signal.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#define POOL_SIZE ITEM_MAX*ITEM_SIZE
#define ITEM_SIZE 512
#define ITEM_MAX 10

int squeue_release(const char *file)
{
	int shmid;
	key_t k = ftok(file, 0xa3);
	
	if (k == -1) {
		perror("ftok");
		return -1;
	}
	
	shmid = shmget(k, POOL_SIZE, 0666);
	if (shmid == -1)
		return -1; // not released
	return shmctl(shmid, IPC_RMID,NULL);
}

extern int errno;
struct squeue_t *squeue_open(const char *file, int mode)
{
	struct squeue_t *q;
	char *pool;
	int shmid;
	int fd;
	key_t k;

	k = ftok(file, 0x34);
	if (k == -1) {
		perror("ftok");
		squeue_release(file);
		if ((fd = creat(file, 0666)) == -1) {
			perror("creat");
			return NULL;
		}
		close(fd);
		chmod(file, 0666);
		k = ftok(file, 0xa3);
		if (k == -1) {
			perror("ftok");
			return NULL;
		}
	}
_retry:
	shmid = shmget(k, POOL_SIZE, 0666|(mode==Q_CREAT)?IPC_CREAT:0);
	if (shmid == -1) {
		perror("shmget1");
		shmid = shmget(k, POOL_SIZE, 0666|((mode==Q_CREAT)?IPC_CREAT:0));
		if (shmid == -1) {
			perror("shmget2");
			if (errno == EEXIST)
				shmid = shmget(k, POOL_SIZE, 0666 |IPC_CREAT);

			if (shmid == -1) {
				if (mode == Q_WAIT) {
					usleep(100);
					goto _retry;
				}
				perror("shmget");
				return NULL;
			}
		}
	}

	if (mode == Q_WAIT)
		mode = Q_OPEN;

	/* fix perms */
	if (mode == Q_CREAT) {
		struct shmid_ds sd;
		shmctl(shmid, IPC_STAT, &sd);
		sd.shm_perm.uid = 1000;
		sd.shm_perm.gid = 1000;
		sd.shm_perm.mode = sd.shm_perm.mode|0666;
		shmctl(shmid, IPC_SET, &sd);
	}

	pool = shmat(shmid, NULL, 0);
	if (pool == (void *)-1) {
		perror("shmat");
		return NULL;
	}

	if (mode == Q_CREAT)
		memset(pool, '\0', POOL_SIZE);

	q = (struct squeue_t *)malloc(sizeof(struct squeue_t));
	memset(q, '\0', sizeof(struct squeue_t));
	q->shmid = shmid;
	q->pool  = pool;
	q->mode  = mode;

	return q;
}

void squeue_free(struct squeue_t *q)
{
	free(q);
}

int squeue_close(struct squeue_t *q)
{
	char *pool;

	if (q==NULL)
		return -1;

	pool = q->pool;
	shmctl(q->shmid, IPC_RMID, NULL);
	free(q);
	return shmdt(pool);
}

int squeue_push(struct squeue_t *q, const char *str, int lock)
{
	int i;

	if (q==NULL)
		return -1;
//	if (q->mode == Q_CREAT) {
//		printf("squeue_push: cannot push from the creator\n");
//		return -1;
//	}

	if (str==NULL||strlen(str)>ITEM_SIZE)
		return -1;
	do {
		if (q->squeue_idx >= ITEM_MAX) {
			while (q->pool[0]!='\0') {
				q->squeue_locks++;
				if (!lock) {
					printf("buffer is full\n");
					return -1;
				}
				usleep(100);
			}
			q->squeue_idx = 0;
		}
		
		for(i=q->squeue_idx*ITEM_SIZE;i<POOL_SIZE;i+=ITEM_SIZE) {
			if (q->pool[i]=='\0') {
				strcpy(q->pool+i, str);
				q->squeue_idx++;
				return 1;
			}
		}
		q->squeue_locks++;
	} while (lock);
	q->squeue_lost++;

	return 0;
}

int squeue_push2(struct squeue_t *q, const char *head, const char *str, int lock)
{
	int ret;
	char *buf = malloc(strlen(head) + strlen(str) + 2);
	strcpy(buf, head);
	strcat(buf, ":");
	strcat(buf, str);
	ret = squeue_push(q, buf, lock);
	free(buf);
	return ret;
}

int squeue_pop(struct squeue_t *q)
{
	int i;
	int two = 1;

	if (q==NULL)
		return -1;
//	if (q->mode == Q_OPEN) { // cannot pop when not owner of squeue
//		fprintf(stderr, "squeue_pop: cannot pop when you are not owner.\n");
//		return -1;
//	}
	for(two=1;two--;q->head_idx = 0)
		for(i=q->head_idx*ITEM_SIZE;i<POOL_SIZE;i+=ITEM_SIZE) {
			if (q->pool[i]!='\0') {
				q->pool[i]='\0';
				q->head_idx++;
				q->squeue_pops++;
				return 1;
			}
		}
	q->squeue_oops++;
	fprintf(stderr, "WARNING: race condition detected in squeue.\n");
	return 0;
}

char *squeue_get(struct squeue_t *q, int lock)
{
	int i=0;
	int two = 1;

	if(q==NULL)
		return NULL;
//	if (q->mode == Q_OPEN) { // cannot pop when not owner of squeue
//		fprintf(stderr, "squeue_get: cannot pop when you are not owner.\n");
//		return NULL;
//	}
	do {
		for(two=1;two--;q->head_idx = 0) {
			for(i=q->head_idx*ITEM_SIZE;i<POOL_SIZE;i+=ITEM_SIZE)
				if (q->pool[i]!='\0')
					return q->pool+i;
		}
		if (lock)
			usleep(100);
	} while (lock);
	return NULL;
}

void squeue_stats(struct squeue_t *q)
{
	if(q==NULL)
		return;
	printf("Queue locks: %d\n", q->squeue_locks);
	printf("Queue lost: %d\n", q->squeue_lost);
	printf("Queue oops: %d\n", q->squeue_oops);
	printf("Queue pops: %d\n", q->squeue_pops);
}

#if _MAIN_
struct squeue_t *q;

int sigc()
{
	squeue_stats(q);
	exit(1);
}

int main()
{
	char buf[102];
	int i;
	int pid;

	signal(SIGINT, sigc);

	squeue_release("/dev/null");

	pid = fork();
	if (pid) {
		q = squeue_open("/dev/null", Q_CREAT);
		if (q == NULL) {
			perror("oops");
			kill(pid, SIGINT);
			return;
		}
		squeue_push(q, "Hello World", 0);
		squeue_push(q, "jeje msg 2", 0);

		while(1) {
			char *uh = squeue_get(q,1);
			if (uh) {
				printf("get: (%s)\n", uh);
				squeue_pop(q);
			}
			usleep(100);
		}
		squeue_close(q);
	} else {
		q = squeue_open("/dev/null", Q_WAIT);
		if (q == NULL) {
			perror("oops");
			return;
		}
		for(i=0;i<128;i++) {
			sprintf(buf, "puta%d", i);
			squeue_push(q, buf, 1);
			usleep(100);
		}
		squeue_stats(q);
		printf("cya\n");
		squeue_close(q);
	}

	return 0;
}
#endif