]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/DiskIO/DiskDaemon/diskd.cc
4 * DEBUG: section -- External DISKD process implementation.
5 * AUTHOR: Harvest Derived
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
36 #include "DiskIO/DiskDaemon/diomsg.h"
44 xassert(const char *msg
, const char *file
, int line
)
46 fprintf(stderr
,"assertion failed: %s:%d: \"%s\"\n", file
, line
, msg
);
51 const int diomsg::msg_snd_rcv_sz
= sizeof(diomsg
) - sizeof(mtyp_t
);
52 #define DEBUG(LEVEL) if ((LEVEL) <= DebugLevel)
54 typedef struct _file_state file_state
;
64 static hash_table
*hash
= NULL
;
67 static int DebugLevel
= 0;
70 do_open(diomsg
* r
, int len
, const char *buf
)
75 * note r->offset holds open() flags
77 fd
= open(buf
, r
->offset
, 0600);
81 fprintf(stderr
, "%d %s: ", (int) mypid
, buf
);
88 fs
= (file_state
*)xcalloc(1, sizeof(*fs
));
90 fs
->key
= &fs
->id
; /* gack */
92 hash_join(hash
, (hash_link
*) fs
);
94 fprintf(stderr
, "%d OPEN id %d, FD %d, fs %p\n",
104 do_close(diomsg
* r
, int len
)
108 fs
= (file_state
*) hash_lookup(hash
, &r
->id
);
113 fprintf(stderr
, "%d CLOSE id %d: ", (int) mypid
, r
->id
);
121 hash_remove_link(hash
, (hash_link
*) fs
);
123 fprintf(stderr
, "%d CLOSE id %d, FD %d, fs %p\n",
134 do_read(diomsg
* r
, int len
, char *buf
)
137 int readlen
= r
->size
;
139 fs
= (file_state
*) hash_lookup(hash
, &r
->id
);
144 fprintf(stderr
, "%d READ id %d: ", (int) mypid
, r
->id
);
151 if (r
->offset
> -1 && r
->offset
!= fs
->offset
) {
153 fprintf(stderr
, "seeking to %"PRId64
"\n", (int64_t)r
->offset
);
156 if (lseek(fs
->fd
, r
->offset
, SEEK_SET
) < 0) {
158 fprintf(stderr
, "%d FD %d, offset %"PRId64
": ", (int) mypid
, fs
->fd
, (int64_t)r
->offset
);
164 x
= read(fs
->fd
, buf
, readlen
);
166 fprintf(stderr
, "%d READ %d,%d,%"PRId64
" ret %d\n", (int) mypid
,
167 fs
->fd
, readlen
, (int64_t)r
->offset
, x
);
172 fprintf(stderr
, "%d FD %d: ", (int) mypid
, fs
->fd
);
179 fs
->offset
= r
->offset
+ x
;
184 do_write(diomsg
* r
, int len
, const char *buf
)
186 int wrtlen
= r
->size
;
189 fs
= (file_state
*) hash_lookup(hash
, &r
->id
);
194 fprintf(stderr
, "%d WRITE id %d: ", (int) mypid
, r
->id
);
201 if (r
->offset
> -1 && r
->offset
!= fs
->offset
) {
202 if (lseek(fs
->fd
, r
->offset
, SEEK_SET
) < 0) {
204 fprintf(stderr
, "%d FD %d, offset %"PRId64
": ", (int) mypid
, fs
->fd
, (int64_t)r
->offset
);
211 fprintf(stderr
, "%d WRITE %d,%d,%"PRId64
"\n", (int) mypid
,
212 fs
->fd
, wrtlen
, (int64_t)r
->offset
);
214 x
= write(fs
->fd
, buf
, wrtlen
);
218 fprintf(stderr
, "%d FD %d: ", (int) mypid
, fs
->fd
);
225 fs
->offset
= r
->offset
+ x
;
230 do_unlink(diomsg
* r
, int len
, const char *buf
)
232 if (unlink(buf
) < 0) {
234 fprintf(stderr
, "%d UNLNK id %d %s: ", (int) mypid
, r
->id
, buf
);
242 fprintf(stderr
, "%d UNLNK %s\n", (int) mypid
, buf
);
248 msg_handle(diomsg
* r
, int rl
, diomsg
* s
)
253 s
->seq_no
= r
->seq_no
; /* optional, debugging */
254 s
->callback_data
= r
->callback_data
;
255 s
->requestor
= r
->requestor
;
256 s
->size
= 0; /* optional, debugging */
257 s
->offset
= 0; /* optional, debugging */
258 s
->shm_offset
= r
->shm_offset
;
259 s
->newstyle
= r
->newstyle
;
261 if (s
->shm_offset
> -1)
262 buf
= shmbuf
+ s
->shm_offset
;
269 s
->status
= do_open(r
, rl
, buf
);
273 s
->status
= do_close(r
, rl
);
277 s
->status
= do_read(r
, rl
, buf
);
281 s
->status
= do_write(r
, rl
, buf
);
285 s
->status
= do_unlink(r
, rl
, buf
);
295 fsCmp(const void *a
, const void *b
)
297 const int *A
= (const int *)a
;
298 const int *B
= (const int *)b
;
303 fsHash(const void *key
, unsigned int n
)
305 /* note, n must be a power of 2! */
306 const int *k
= (const int *)key
;
312 alarm_handler(int sig
) {
318 main(int argc
, char *argv
[])
330 setbuf(stdout
, NULL
);
331 setbuf(stderr
, NULL
);
335 rmsgid
= msgget(key
, 0600);
343 smsgid
= msgget(key
, 0600);
351 shmid
= shmget(key
, 0, 0600);
358 shmbuf
= (char *)shmat(shmid
, NULL
, 0);
360 if (shmbuf
== (void *) -1) {
365 hash
= hash_create(fsCmp
, 1 << 4, fsHash
);
367 fcntl(0, F_SETFL
, SQUID_NONBLOCK
);
368 memset(&sa
, '\0', sizeof(sa
));
369 sa
.sa_handler
= alarm_handler
;
370 sa
.sa_flags
= SA_RESTART
;
371 sigaction(SIGALRM
, &sa
, NULL
);
375 memset(&rmsg
, '\0', sizeof(rmsg
));
377 std::cerr
<< "msgrcv: " << rmsgid
<< ", "
378 << &rmsg
<< ", " << diomsg::msg_snd_rcv_sz
379 << ", " << 0 << ", " << 0 << std::endl
;
381 rlen
= msgrcv(rmsgid
, &rmsg
, diomsg::msg_snd_rcv_sz
, 0, 0);
384 if (EINTR
== errno
) {
385 if (read(0, rbuf
, 512) <= 0) {
386 if (EWOULDBLOCK
== errno
)
388 else if (EAGAIN
== errno
)
395 if (EAGAIN
== errno
) {
404 msg_handle(&rmsg
, rlen
, &smsg
);
406 if (msgsnd(smsgid
, &smsg
, diomsg::msg_snd_rcv_sz
, 0) < 0) {
413 fprintf(stderr
, "%d diskd exiting\n", (int) mypid
);
416 if (msgctl(rmsgid
, IPC_RMID
, 0) < 0)
417 perror("msgctl IPC_RMID");
419 if (msgctl(smsgid
, IPC_RMID
, 0) < 0)
420 perror("msgctl IPC_RMID");
422 if (shmdt(shmbuf
) < 0)
425 if (shmctl(shmid
, IPC_RMID
, 0) < 0)
426 perror("shmctl IPC_RMID");