]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/DiskIO/DiskDaemon/diskd.cc
2 * DEBUG: section -- External DISKD process implementation.
3 * AUTHOR: Harvest Derived
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 #include "DiskIO/DiskDaemon/diomsg.h"
50 xassert(const char *msg
, const char *file
, int line
)
52 fprintf(stderr
,"assertion failed: %s:%d: \"%s\"\n", file
, line
, msg
);
57 const int diomsg::msg_snd_rcv_sz
= sizeof(diomsg
) - sizeof(mtyp_t
);
58 #define DEBUG(LEVEL) if ((LEVEL) <= DebugLevel)
60 typedef struct _file_state file_state
;
70 static hash_table
*hash
= NULL
;
73 static int DebugLevel
= 0;
76 do_open(diomsg
* r
, int len
, const char *buf
)
81 * note r->offset holds open() flags
83 fd
= open(buf
, r
->offset
, 0600);
87 fprintf(stderr
, "%d %s: ", (int) mypid
, buf
);
94 fs
= (file_state
*)xcalloc(1, sizeof(*fs
));
96 fs
->key
= &fs
->id
; /* gack */
98 hash_join(hash
, (hash_link
*) fs
);
100 fprintf(stderr
, "%d OPEN id %d, FD %d, fs %p\n",
110 do_close(diomsg
* r
, int len
)
114 fs
= (file_state
*) hash_lookup(hash
, &r
->id
);
119 fprintf(stderr
, "%d CLOSE id %d: ", (int) mypid
, r
->id
);
127 hash_remove_link(hash
, (hash_link
*) fs
);
129 fprintf(stderr
, "%d CLOSE id %d, FD %d, fs %p\n",
140 do_read(diomsg
* r
, int len
, char *buf
)
143 int readlen
= r
->size
;
145 fs
= (file_state
*) hash_lookup(hash
, &r
->id
);
150 fprintf(stderr
, "%d READ id %d: ", (int) mypid
, r
->id
);
157 if (r
->offset
> -1 && r
->offset
!= fs
->offset
) {
159 fprintf(stderr
, "seeking to %" PRId64
"\n", (int64_t)r
->offset
);
162 if (lseek(fs
->fd
, r
->offset
, SEEK_SET
) < 0) {
164 fprintf(stderr
, "%d FD %d, offset %" PRId64
": ", (int) mypid
, fs
->fd
, (int64_t)r
->offset
);
170 x
= read(fs
->fd
, buf
, readlen
);
172 fprintf(stderr
, "%d READ %d,%d,%" PRId64
" ret %d\n", (int) mypid
,
173 fs
->fd
, readlen
, (int64_t)r
->offset
, x
);
178 fprintf(stderr
, "%d FD %d: ", (int) mypid
, fs
->fd
);
185 fs
->offset
= r
->offset
+ x
;
190 do_write(diomsg
* r
, int len
, const char *buf
)
192 int wrtlen
= r
->size
;
195 fs
= (file_state
*) hash_lookup(hash
, &r
->id
);
200 fprintf(stderr
, "%d WRITE id %d: ", (int) mypid
, r
->id
);
207 if (r
->offset
> -1 && r
->offset
!= fs
->offset
) {
208 if (lseek(fs
->fd
, r
->offset
, SEEK_SET
) < 0) {
210 fprintf(stderr
, "%d FD %d, offset %" PRId64
": ", (int) mypid
, fs
->fd
, (int64_t)r
->offset
);
217 fprintf(stderr
, "%d WRITE %d,%d,%" PRId64
"\n", (int) mypid
,
218 fs
->fd
, wrtlen
, (int64_t)r
->offset
);
220 x
= write(fs
->fd
, buf
, wrtlen
);
224 fprintf(stderr
, "%d FD %d: ", (int) mypid
, fs
->fd
);
231 fs
->offset
= r
->offset
+ x
;
236 do_unlink(diomsg
* r
, int len
, const char *buf
)
238 if (unlink(buf
) < 0) {
240 fprintf(stderr
, "%d UNLNK id %d %s: ", (int) mypid
, r
->id
, buf
);
248 fprintf(stderr
, "%d UNLNK %s\n", (int) mypid
, buf
);
254 msg_handle(diomsg
* r
, int rl
, diomsg
* s
)
259 s
->seq_no
= r
->seq_no
; /* optional, debugging */
260 s
->callback_data
= r
->callback_data
;
261 s
->requestor
= r
->requestor
;
262 s
->size
= 0; /* optional, debugging */
263 s
->offset
= 0; /* optional, debugging */
264 s
->shm_offset
= r
->shm_offset
;
265 s
->newstyle
= r
->newstyle
;
267 if (s
->shm_offset
> -1)
268 buf
= shmbuf
+ s
->shm_offset
;
269 else if (r
->mtype
!= _MQD_CLOSE
) {
270 fprintf(stderr
, "%d UNLNK id(%u) Error: no filename in shm buffer\n", (int) mypid
, s
->id
);
279 s
->status
= do_open(r
, rl
, buf
);
283 s
->status
= do_close(r
, rl
);
287 s
->status
= do_read(r
, rl
, buf
);
291 s
->status
= do_write(r
, rl
, buf
);
295 s
->status
= do_unlink(r
, rl
, buf
);
305 fsCmp(const void *a
, const void *b
)
307 const int *A
= (const int *)a
;
308 const int *B
= (const int *)b
;
313 fsHash(const void *key
, unsigned int n
)
315 /* note, n must be a power of 2! */
316 const int *k
= (const int *)key
;
322 alarm_handler(int sig
) {
328 main(int argc
, char *argv
[])
340 setbuf(stdout
, NULL
);
341 setbuf(stderr
, NULL
);
345 rmsgid
= msgget(key
, 0600);
353 smsgid
= msgget(key
, 0600);
361 shmid
= shmget(key
, 0, 0600);
368 shmbuf
= (char *)shmat(shmid
, NULL
, 0);
370 if (shmbuf
== (void *) -1) {
375 hash
= hash_create(fsCmp
, 1 << 4, fsHash
);
377 if (fcntl(0, F_SETFL
, SQUID_NONBLOCK
) < 0) {
381 memset(&sa
, '\0', sizeof(sa
));
382 sa
.sa_handler
= alarm_handler
;
383 sa
.sa_flags
= SA_RESTART
;
384 sigaction(SIGALRM
, &sa
, NULL
);
388 memset(&rmsg
, '\0', sizeof(rmsg
));
390 std::cerr
<< "msgrcv: " << rmsgid
<< ", "
391 << &rmsg
<< ", " << diomsg::msg_snd_rcv_sz
392 << ", " << 0 << ", " << 0 << std::endl
;
394 rlen
= msgrcv(rmsgid
, &rmsg
, diomsg::msg_snd_rcv_sz
, 0, 0);
397 if (EINTR
== errno
) {
398 if (read(0, rbuf
, 512) <= 0) {
399 if (EWOULDBLOCK
== errno
)
401 else if (EAGAIN
== errno
)
408 if (EAGAIN
== errno
) {
417 msg_handle(&rmsg
, rlen
, &smsg
);
419 if (msgsnd(smsgid
, &smsg
, diomsg::msg_snd_rcv_sz
, 0) < 0) {
426 fprintf(stderr
, "%d diskd exiting\n", (int) mypid
);
429 if (msgctl(rmsgid
, IPC_RMID
, 0) < 0)
430 perror("msgctl IPC_RMID");
432 if (msgctl(smsgid
, IPC_RMID
, 0) < 0)
433 perror("msgctl IPC_RMID");
435 if (shmdt(shmbuf
) < 0)
438 if (shmctl(shmid
, IPC_RMID
, 0) < 0)
439 perror("shmctl IPC_RMID");