-
Notifications
You must be signed in to change notification settings - Fork 493
/
diskd.cc
422 lines (350 loc) · 9.41 KB
/
diskd.cc
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* $Id: diskd.cc,v 1.6 2007/04/12 23:51:57 wessels Exp $
*
* DEBUG: section -- External DISKD process implementation.
* AUTHOR: Harvest Derived
*
* SQUID Web Proxy Cache http://www.squid-cache.org/
* ----------------------------------------------------------
*
* Squid is the result of efforts by numerous individuals from
* the Internet community; see the CONTRIBUTORS file for full
* details. Many organizations have provided support for Squid's
* development; see the SPONSORS file for full details. Squid is
* Copyrighted (C) 2001 by the Regents of the University of
* California; see the COPYRIGHT file for full details. Squid
* incorporates software developed and/or copyrighted by other
* sources; see the CREDITS file for full details.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
*/
#include "config.h"
#include "squid.h"
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include "DiskIO/DiskDaemon/diomsg.h"
void
xassert(const char *msg, const char *file, int line)
{
fprintf(stderr,"assertion failed: %s:%d: \"%s\"\n", file, line, msg);
abort();
}
const int diomsg::msg_snd_rcv_sz = sizeof(diomsg) - sizeof(mtyp_t);
#define DEBUG(LEVEL) if ((LEVEL) <= DebugLevel)
typedef struct _file_state file_state;
struct _file_state
{
void *key;
file_state *next;
int id;
int fd;
off_t offset;
};
static hash_table *hash = NULL;
static pid_t mypid;
static char *shmbuf;
static int DebugLevel = 1;
static int
do_open(diomsg * r, int len, const char *buf)
{
int fd;
file_state *fs;
/*
* note r->offset holds open() flags
*/
fd = open(buf, r->offset, 0600);
if (fd < 0) {
DEBUG(1) {
fprintf(stderr, "%d %s: ", (int) mypid, buf);
perror("open");
}
return -errno;
}
fs = (file_state *)xcalloc(1, sizeof(*fs));
fs->id = r->id;
fs->key = &fs->id; /* gack */
fs->fd = fd;
hash_join(hash, (hash_link *) fs);
DEBUG(2)
fprintf(stderr, "%d OPEN id %d, FD %d, fs %p\n",
(int) mypid,
fs->id,
fs->fd,
fs);
return fd;
}
static int
do_close(diomsg * r, int len)
{
int fd;
file_state *fs;
fs = (file_state *) hash_lookup(hash, &r->id);
if (NULL == fs) {
errno = EBADF;
DEBUG(1) {
fprintf(stderr, "%d CLOSE id %d: ", (int) mypid, r->id);
perror("do_close");
}
return -errno;
}
fd = fs->fd;
hash_remove_link(hash, (hash_link *) fs);
DEBUG(2)
fprintf(stderr, "%d CLOSE id %d, FD %d, fs %p\n",
(int) mypid,
r->id,
fs->fd,
fs);
xfree(fs);
return close(fd);
}
static int
do_read(diomsg * r, int len, char *buf)
{
int x;
int readlen = r->size;
file_state *fs;
fs = (file_state *) hash_lookup(hash, &r->id);
if (NULL == fs) {
errno = EBADF;
DEBUG(1) {
fprintf(stderr, "%d READ id %d: ", (int) mypid, r->id);
perror("do_read");
}
return -errno;
}
if (r->offset > -1 && r->offset != fs->offset) {
DEBUG(2)
fprintf(stderr, "seeking to %d\n", r->offset);
if (lseek(fs->fd, r->offset, SEEK_SET) < 0) {
DEBUG(1) {
fprintf(stderr, "%d FD %d, offset %d: ", (int) mypid, fs->fd, r->offset);
perror("lseek");
}
}
}
x = read(fs->fd, buf, readlen);
DEBUG(2)
fprintf(stderr, "%d READ %d,%d,%d ret %d\n", (int) mypid,
fs->fd, readlen, r->offset, x);
if (x < 0) {
DEBUG(1) {
fprintf(stderr, "%d FD %d: ", (int) mypid, fs->fd);
perror("read");
}
return -errno;
}
fs->offset = r->offset + x;
return x;
}
static int
do_write(diomsg * r, int len, const char *buf)
{
int wrtlen = r->size;
int x;
file_state *fs;
fs = (file_state *) hash_lookup(hash, &r->id);
if (NULL == fs) {
errno = EBADF;
DEBUG(1) {
fprintf(stderr, "%d WRITE id %d: ", (int) mypid, r->id);
perror("do_write");
}
return -errno;
}
if (r->offset > -1 && r->offset != fs->offset) {
if (lseek(fs->fd, r->offset, SEEK_SET) < 0) {
DEBUG(1) {
fprintf(stderr, "%d FD %d, offset %d: ", (int) mypid, fs->fd, r->offset);
perror("lseek");
}
}
}
DEBUG(2)
fprintf(stderr, "%d WRITE %d,%d,%d\n", (int) mypid,
fs->fd, wrtlen, r->offset);
x = write(fs->fd, buf, wrtlen);
if (x < 0) {
DEBUG(1) {
fprintf(stderr, "%d FD %d: ", (int) mypid, fs->fd);
perror("write");
}
return -errno;
}
fs->offset = r->offset + x;
return x;
}
static int
do_unlink(diomsg * r, int len, const char *buf)
{
if (unlink(buf) < 0) {
DEBUG(1) {
fprintf(stderr, "%d UNLNK id %d %s: ", (int) mypid, r->id, buf);
perror("unlink");
}
return -errno;
}
DEBUG(2)
fprintf(stderr, "%d UNLNK %s\n", (int) mypid, buf);
return 0;
}
static void
msg_handle(diomsg * r, int rl, diomsg * s)
{
char *buf = NULL;
s->mtype = r->mtype;
s->id = r->id;
s->seq_no = r->seq_no; /* optional, debugging */
s->callback_data = r->callback_data;
s->requestor = r->requestor;
s->size = 0; /* optional, debugging */
s->offset = 0; /* optional, debugging */
s->shm_offset = r->shm_offset;
s->newstyle = r->newstyle;
if (s->shm_offset > -1)
buf = shmbuf + s->shm_offset;
switch (r->mtype) {
case _MQD_OPEN:
case _MQD_CREATE:
s->status = do_open(r, rl, buf);
break;
case _MQD_CLOSE:
s->status = do_close(r, rl);
break;
case _MQD_READ:
s->status = do_read(r, rl, buf);
break;
case _MQD_WRITE:
s->status = do_write(r, rl, buf);
break;
case _MQD_UNLINK:
s->status = do_unlink(r, rl, buf);
break;
default:
assert(0);
break;
}
}
static int
fsCmp(const void *a, const void *b)
{
const int *A = (const int *)a;
const int *B = (const int *)b;
return *A != *B;
}
static unsigned int
fsHash(const void *key, unsigned int n)
{
/* note, n must be a power of 2! */
const int *k = (const int *)key;
return (*k & (--n));
}
static void
alarm_handler(int sig)
{
(void) 0;
}
int
main(int argc, char *argv[])
{
int key;
int rmsgid;
int smsgid;
int shmid;
diomsg rmsg;
diomsg smsg;
int rlen;
char rbuf[512];
struct sigaction sa;
setbuf(stdout, NULL);
setbuf(stderr, NULL);
mypid = getpid();
assert(4 == argc);
key = atoi(argv[1]);
rmsgid = msgget(key, 0600);
if (rmsgid < 0) {
perror("msgget");
return 1;
}
key = atoi(argv[2]);
smsgid = msgget(key, 0600);
if (smsgid < 0) {
perror("msgget");
return 1;
}
key = atoi(argv[3]);
shmid = shmget(key, 0, 0600);
if (shmid < 0) {
perror("shmget");
return 1;
}
shmbuf = (char *)shmat(shmid, NULL, 0);
if (shmbuf == (void *) -1) {
perror("shmat");
return 1;
}
hash = hash_create(fsCmp, 1 << 4, fsHash);
assert(hash);
fcntl(0, F_SETFL, SQUID_NONBLOCK);
memset(&sa, '\0', sizeof(sa));
sa.sa_handler = alarm_handler;
sa.sa_flags = SA_RESTART;
sigaction(SIGALRM, &sa, NULL);
for (;;) {
alarm(1);
memset(&rmsg, '\0', sizeof(rmsg));
DEBUG(2)
std::cerr << "msgrcv: " << rmsgid << ", "
<< &rmsg << ", " << diomsg::msg_snd_rcv_sz
<< ", " << 0 << ", " << 0 << std::endl;
rlen = msgrcv(rmsgid, &rmsg, diomsg::msg_snd_rcv_sz, 0, 0);
if (rlen < 0) {
if (EINTR == errno) {
if (read(0, rbuf, 512) <= 0) {
if (EWOULDBLOCK == errno)
(void) 0;
else if (EAGAIN == errno)
(void) 0;
else
break;
}
}
if (EAGAIN == errno) {
continue;
}
perror("msgrcv");
break;
}
alarm(0);
msg_handle(&rmsg, rlen, &smsg);
if (msgsnd(smsgid, &smsg, diomsg::msg_snd_rcv_sz, 0) < 0) {
perror("msgsnd");
break;
}
}
DEBUG(2)
fprintf(stderr, "%d diskd exiting\n", (int) mypid);
if (msgctl(rmsgid, IPC_RMID, 0) < 0)
perror("msgctl IPC_RMID");
if (msgctl(smsgid, IPC_RMID, 0) < 0)
perror("msgctl IPC_RMID");
if (shmdt(shmbuf) < 0)
perror("shmdt");
if (shmctl(shmid, IPC_RMID, 0) < 0)
perror("shmctl IPC_RMID");
return 0;
}