Remove the RCSID macro
[openafs.git] / src / vol / salvaged.c
1 /*
2  * Copyright 2006-2007, Sine Nomine Associates and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /* 
11  * demand attach fs
12  * online salvager daemon
13  */
14
15 /* Main program file. Define globals. */
16 #define MAIN 1
17
18 #include <afsconfig.h>
19 #include <afs/param.h>
20
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <dirent.h>
26 #include <sys/stat.h>
27 #include <time.h>
28 #include <errno.h>
29 #ifdef AFS_NT40_ENV
30 #include <io.h>
31 #include <WINNT/afsevent.h>
32 #else
33 #include <sys/param.h>
34 #include <sys/file.h>
35 #ifndef ITIMER_REAL
36 #include <sys/time.h>
37 #endif /* ITIMER_REAL */
38 #endif
39 #if     defined(AFS_AIX_ENV) || defined(AFS_SUN4_ENV)
40 #define WCOREDUMP(x)    (x & 0200)
41 #endif
42 #include <rx/xdr.h>
43 #include <afs/afsint.h>
44 #include <afs/assert.h>
45 #if !defined(AFS_SGI_ENV) && !defined(AFS_NT40_ENV)
46 #if defined(AFS_VFSINCL_ENV)
47 #include <sys/vnode.h>
48 #ifdef  AFS_SUN5_ENV
49 #include <sys/fs/ufs_inode.h>
50 #else
51 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
52 #include <ufs/ufs/dinode.h>
53 #include <ufs/ffs/fs.h>
54 #else
55 #include <ufs/inode.h>
56 #endif
57 #endif
58 #else /* AFS_VFSINCL_ENV */
59 #ifdef  AFS_OSF_ENV
60 #include <ufs/inode.h>
61 #else /* AFS_OSF_ENV */
62 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_XBSD_ENV)
63 #include <sys/inode.h>
64 #endif
65 #endif
66 #endif /* AFS_VFSINCL_ENV */
67 #endif /* AFS_SGI_ENV */
68 #ifdef  AFS_AIX_ENV
69 #include <sys/vfs.h>
70 #include <sys/lockf.h>
71 #else
72 #ifdef  AFS_HPUX_ENV
73 #include <unistd.h>
74 #include <checklist.h>
75 #else
76 #if defined(AFS_SGI_ENV)
77 #include <unistd.h>
78 #include <fcntl.h>
79 #include <mntent.h>
80 #else
81 #if     defined(AFS_SUN_ENV) || defined(AFS_SUN5_ENV)
82 #ifdef    AFS_SUN5_ENV
83 #include <unistd.h>
84 #include <sys/mnttab.h>
85 #include <sys/mntent.h>
86 #else
87 #include <mntent.h>
88 #endif
89 #else
90 #endif /* AFS_SGI_ENV */
91 #endif /* AFS_HPUX_ENV */
92 #endif
93 #endif
94 #include <fcntl.h>
95 #ifndef AFS_NT40_ENV
96 #include <afs/osi_inode.h>
97 #endif
98 #include <afs/cmd.h>
99 #include <afs/afsutil.h>
100 #include <afs/fileutil.h>
101 #include <afs/procmgmt.h>       /* signal(), kill(), wait(), etc. */
102 #ifndef AFS_NT40_ENV
103 #include <syslog.h>
104 #endif
105
106 #include "nfs.h"
107 #include "lwp.h"
108 #include "lock.h"
109 #include <afs/afssyscalls.h>
110 #include "ihandle.h"
111 #include "vnode.h"
112 #include "volume.h"
113 #include "partition.h"
114 #include "daemon_com.h"
115 #include "fssync.h"
116 #include "salvsync.h"
117 #include "viceinode.h"
118 #include "salvage.h"
119 #include "volinodes.h"          /* header magic number, etc. stuff */
120 #include "vol-salvage.h"
121 #ifdef AFS_NT40_ENV
122 #include <pthread.h>
123 #endif
124
125
126 #if !defined(AFS_DEMAND_ATTACH_FS)
127 #error "online salvager only supported for demand attach fileserver"
128 #endif /* AFS_DEMAND_ATTACH_FS */
129
130 #if defined(AFS_NT40_ENV)
131 #error "online salvager not supported on NT"
132 #endif /* AFS_NT40_ENV */
133
134
135 /* Forward declarations */
136 /*@printflike@*/ void Log(const char *format, ...);
137 /*@printflike@*/ void Abort(const char *format, ...);
138
139
140 /*@+fcnmacros +macrofcndecl@*/
141 #ifdef O_LARGEFILE
142 #define afs_fopen       fopen64
143 #else /* !O_LARGEFILE */
144 #define afs_fopen       fopen
145 #endif /* !O_LARGEFILE */
146 /*@=fcnmacros =macrofcndecl@*/
147
148
149
150 static volatile int current_workers = 0;
151 static volatile struct rx_queue pending_q;
152 static pthread_mutex_t worker_lock;
153 static pthread_cond_t worker_cv;
154
155 static void * SalvageChildReaperThread(void *);
156 static int DoSalvageVolume(struct SalvageQueueNode * node, int slot);
157
158 static void SalvageServer(void);
159 static void SalvageClient(VolumeId vid, char * pname);
160
161 static int ChildFailed(int status);
162
163 static int Reap_Child(char * prog, int * pid, int * status);
164
165 static void * SalvageLogCleanupThread(void *);
166 static int SalvageLogCleanup(int pid);
167
168 struct log_cleanup_node {
169     struct rx_queue q;
170     int pid;
171 };
172
173 struct {
174     struct rx_queue queue_head;
175     pthread_cond_t queue_change_cv;
176 } log_cleanup_queue;
177
178
179 #define DEFAULT_PARALLELISM 4 /* allow 4 parallel salvage workers by default */
180
181 static int
182 handleit(struct cmd_syndesc *as, void *arock)
183 {
184     register struct cmd_item *ti;
185     char pname[100], *temp;
186     afs_int32 seenpart = 0, seenvol = 0, vid = 0, seenany = 0;
187     struct DiskPartition64 *partP;
188
189
190 #ifdef AFS_SGI_VNODE_GLUE
191     if (afs_init_kernel_config(-1) < 0) {
192         printf
193             ("Can't determine NUMA configuration, not starting salvager.\n");
194         exit(1);
195     }
196 #endif
197
198     if (as->parms[2].items)     /* -debug */
199         debug = 1;
200     if (as->parms[3].items)     /* -nowrite */
201         Testing = 1;
202     if (as->parms[4].items)     /* -inodes */
203         ListInodeOption = 1;
204     if (as->parms[5].items)     /* -oktozap */
205         OKToZap = 1;
206     if (as->parms[6].items)     /* -rootinodes */
207         ShowRootFiles = 1;
208     if (as->parms[8].items)     /* -ForceReads */
209         forceR = 1;
210     if ((ti = as->parms[9].items)) {    /* -Parallel # */
211         temp = ti->data;
212         if (strncmp(temp, "all", 3) == 0) {
213             PartsPerDisk = 1;
214             temp += 3;
215         }
216         if (strlen(temp) != 0) {
217             Parallel = atoi(temp);
218             if (Parallel < 1)
219                 Parallel = 1;
220             if (Parallel > MAXPARALLEL) {
221                 printf("Setting parallel salvages to maximum of %d \n",
222                        MAXPARALLEL);
223                 Parallel = MAXPARALLEL;
224             }
225         }
226     } else {
227         Parallel = MIN(DEFAULT_PARALLELISM, MAXPARALLEL);
228     }
229     if ((ti = as->parms[10].items)) {   /* -tmpdir */
230         DIR *dirp;
231
232         tmpdir = ti->data;
233         dirp = opendir(tmpdir);
234         if (!dirp) {
235             printf
236                 ("Can't open temporary placeholder dir %s; using current partition \n",
237                  tmpdir);
238             tmpdir = NULL;
239         } else
240             closedir(dirp);
241     }
242     if ((ti = as->parms[11].items))     /* -showlog */
243         ShowLog = 1;
244     if ((ti = as->parms[12].items)) {   /* -orphans */
245         if (Testing)
246             orphans = ORPH_IGNORE;
247         else if (strcmp(ti->data, "remove") == 0
248                  || strcmp(ti->data, "r") == 0)
249             orphans = ORPH_REMOVE;
250         else if (strcmp(ti->data, "attach") == 0
251                  || strcmp(ti->data, "a") == 0)
252             orphans = ORPH_ATTACH;
253     }
254 #ifndef AFS_NT40_ENV            /* ignore options on NT */
255     if ((ti = as->parms[13].items)) {   /* -syslog */
256         useSyslog = 1;
257         ShowLog = 0;
258     }
259     if ((ti = as->parms[14].items)) {   /* -syslogfacility */
260         useSyslogFacility = atoi(ti->data);
261     }
262
263     if ((ti = as->parms[15].items)) {   /* -datelogs */
264         TimeStampLogFile(AFSDIR_SERVER_SALSRVLOG_FILEPATH);
265     }
266 #endif
267
268     if ((ti = as->parms[16].items)) {   /* -client */
269         if ((ti = as->parms[0].items)) {        /* -partition */
270             seenpart = 1;
271             strlcpy(pname, ti->data, sizeof(pname));
272         }
273         if ((ti = as->parms[1].items)) {        /* -volumeid */
274             seenvol = 1;
275             vid = atoi(ti->data);
276         }
277
278         if (!seenpart || !seenvol) {
279             printf("You must specify '-partition' and '-volumeid' with the '-client' option\n");
280             exit(-1);
281         }
282
283         SalvageClient(vid, pname);
284
285     } else {  /* salvageserver mode */
286         SalvageServer();
287     }
288     return (0);
289 }
290
291
292 #ifndef AFS_NT40_ENV
293 #include "AFS_component_version_number.c"
294 #endif
295 #define MAX_ARGS 128
296 #ifdef AFS_NT40_ENV
297 char *save_args[MAX_ARGS];
298 int n_save_args = 0;
299 pthread_t main_thread;
300 #endif
301
302 static char commandLine[150];
303
304 int
305 main(int argc, char **argv)
306 {
307     struct cmd_syndesc *ts;
308     int err = 0;
309
310     int i;
311     extern char cml_version_number[];
312
313 #ifdef  AFS_AIX32_ENV
314     /*
315      * The following signal action for AIX is necessary so that in case of a 
316      * crash (i.e. core is generated) we can include the user's data section 
317      * in the core dump. Unfortunately, by default, only a partial core is
318      * generated which, in many cases, isn't too useful.
319      */
320     struct sigaction nsa;
321
322     sigemptyset(&nsa.sa_mask);
323     nsa.sa_handler = SIG_DFL;
324     nsa.sa_flags = SA_FULLDUMP;
325     sigaction(SIGABRT, &nsa, NULL);
326     sigaction(SIGSEGV, &nsa, NULL);
327 #endif
328
329     /* Initialize directory paths */
330     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
331 #ifdef AFS_NT40_ENV
332         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
333 #endif
334         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
335                 argv[0]);
336         exit(2);
337     }
338 #ifdef AFS_NT40_ENV
339     main_thread = pthread_self();
340     if (spawnDatap && spawnDataLen) {
341         /* This is a child per partition salvager. Don't setup log or
342          * try to lock the salvager lock.
343          */
344         if (nt_SetupPartitionSalvage(spawnDatap, spawnDataLen) < 0)
345             exit(3);
346     } else {
347 #endif
348         for (commandLine[0] = '\0', i = 0; i < argc; i++) {
349             if (i > 0)
350                 strlcat(commandLine, " ", sizeof(commandLine));
351             strlcat(commandLine, argv[i], sizeof(commandLine));
352         }
353
354 #ifndef AFS_NT40_ENV
355         if (geteuid() != 0) {
356             printf("Salvager must be run as root.\n");
357             fflush(stdout);
358             Exit(0);
359         }
360 #endif
361
362         /* bad for normal help flag processing, but can do nada */
363
364 #ifdef AFS_NT40_ENV
365     }
366 #endif
367
368     ts = cmd_CreateSyntax("initcmd", handleit, NULL, "initialize the program");
369     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_OPTIONAL,
370                 "Name of partition to salvage");
371     cmd_AddParm(ts, "-volumeid", CMD_SINGLE, CMD_OPTIONAL,
372                 "Volume Id to salvage");
373     cmd_AddParm(ts, "-debug", CMD_FLAG, CMD_OPTIONAL,
374                 "Run in Debugging mode");
375     cmd_AddParm(ts, "-nowrite", CMD_FLAG, CMD_OPTIONAL,
376                 "Run readonly/test mode");
377     cmd_AddParm(ts, "-inodes", CMD_FLAG, CMD_OPTIONAL,
378                 "Just list affected afs inodes - debugging flag");
379     cmd_AddParm(ts, "-oktozap", CMD_FLAG, CMD_OPTIONAL,
380                 "Give permission to destroy bogus inodes/volumes - debugging flag");
381     cmd_AddParm(ts, "-rootinodes", CMD_FLAG, CMD_OPTIONAL,
382                 "Show inodes owned by root - debugging flag");
383     cmd_AddParm(ts, "-salvagedirs", CMD_FLAG, CMD_OPTIONAL,
384                 "Force rebuild/salvage of all directories");
385     cmd_AddParm(ts, "-blockreads", CMD_FLAG, CMD_OPTIONAL,
386                 "Read smaller blocks to handle IO/bad blocks");
387     cmd_AddParm(ts, "-parallel", CMD_SINGLE, CMD_OPTIONAL,
388                 "# of max parallel partition salvaging");
389     cmd_AddParm(ts, "-tmpdir", CMD_SINGLE, CMD_OPTIONAL,
390                 "Name of dir to place tmp files ");
391     cmd_AddParm(ts, "-showlog", CMD_FLAG, CMD_OPTIONAL,
392                 "Show log file upon completion");
393     cmd_AddParm(ts, "-orphans", CMD_SINGLE, CMD_OPTIONAL,
394                 "ignore | remove | attach");
395
396     /* note - syslog isn't avail on NT, but if we make it conditional, have
397      * to deal with screwy offsets for cmd params */
398     cmd_AddParm(ts, "-syslog", CMD_FLAG, CMD_OPTIONAL,
399                 "Write salvage log to syslogs");
400     cmd_AddParm(ts, "-syslogfacility", CMD_SINGLE, CMD_OPTIONAL,
401                 "Syslog facility number to use");
402     cmd_AddParm(ts, "-datelogs", CMD_FLAG, CMD_OPTIONAL,
403                 "Include timestamp in logfile filename");
404
405     cmd_AddParm(ts, "-client", CMD_FLAG, CMD_OPTIONAL,
406                 "Use SALVSYNC to ask salvageserver to salvage a volume");
407
408     err = cmd_Dispatch(argc, argv);
409     Exit(err);
410     return 0; /* not reached */
411 }
412
413 static void
414 SalvageClient(VolumeId vid, char * pname)
415 {
416     int done = 0;
417     afs_int32 code;
418     SYNC_response res;
419     SALVSYNC_response_hdr sres;
420
421     VInitVolumePackage(volumeUtility, 5, 5, DONT_CONNECT_FS, 0);
422     SALVSYNC_clientInit();
423     
424     code = SALVSYNC_SalvageVolume(vid, pname, SALVSYNC_SALVAGE, SALVSYNC_OPERATOR, 0, NULL);
425     if (code != SYNC_OK) {
426         goto sync_error;
427     }
428
429     res.payload.buf = (void *) &sres;
430     res.payload.len = sizeof(sres);
431
432     while(!done) {
433         sleep(2);
434         code = SALVSYNC_SalvageVolume(vid, pname, SALVSYNC_QUERY, SALVSYNC_WHATEVER, 0, &res);
435         if (code != SYNC_OK) {
436             goto sync_error;
437         }
438         switch (sres.state) {
439         case SALVSYNC_STATE_ERROR:
440             printf("salvageserver reports salvage ended in an error; check log files for more details\n");
441         case SALVSYNC_STATE_DONE:
442         case SALVSYNC_STATE_UNKNOWN:
443             done = 1;
444         }
445     }
446     SALVSYNC_clientFinis();
447     return;
448
449  sync_error:
450     if (code == SYNC_DENIED) {
451         printf("salvageserver refused to salvage volume %u on partition %s\n",
452                vid, pname);
453     } else if (code == SYNC_BAD_COMMAND) {
454         printf("SALVSYNC protocol mismatch; please make sure fileserver, volserver, salvageserver and salvager are same version\n");
455     } else if (code == SYNC_COM_ERROR) {
456         printf("SALVSYNC communications error\n");
457     }
458     SALVSYNC_clientFinis();
459     exit(-1);
460 }
461
462 static int * child_slot;
463
464 static void
465 SalvageServer(void)
466 {
467     int pid, ret;
468     struct SalvageQueueNode * node;
469     pthread_t tid;
470     pthread_attr_t attrs;
471     int slot;
472
473     /* All entries to the log will be appended.  Useful if there are
474      * multiple salvagers appending to the log.
475      */
476
477     CheckLogFile(AFSDIR_SERVER_SALSRVLOG_FILEPATH);
478 #ifndef AFS_NT40_ENV
479 #ifdef AFS_LINUX20_ENV
480     fcntl(fileno(logFile), F_SETFL, O_APPEND);  /* Isn't this redundant? */
481 #else
482     fcntl(fileno(logFile), F_SETFL, FAPPEND);   /* Isn't this redundant? */
483 #endif
484 #endif
485     setlinebuf(logFile);
486
487     fprintf(logFile, "%s\n", cml_version_number);
488     Log("Starting OpenAFS Online Salvage Server %s (%s)\n", SalvageVersion, commandLine);
489     
490     /* Get and hold a lock for the duration of the salvage to make sure
491      * that no other salvage runs at the same time.  The routine
492      * VInitVolumePackage (called below) makes sure that a file server or
493      * other volume utilities don't interfere with the salvage.
494      */
495     
496     /* even demand attach online salvager
497      * still needs this because we don't want
498      * a stand-alone salvager to conflict with
499      * the salvager daemon */
500     ObtainSalvageLock();
501
502     child_slot = (int *) malloc(Parallel * sizeof(int));
503     assert(child_slot != NULL);
504     memset(child_slot, 0, Parallel * sizeof(int));
505             
506     /* initialize things */
507     VInitVolumePackage(salvageServer, 5, 5,
508                        1, 0);
509     DInit(10);
510     queue_Init(&pending_q);
511     queue_Init(&log_cleanup_queue);
512     assert(pthread_mutex_init(&worker_lock, NULL) == 0);
513     assert(pthread_cond_init(&worker_cv, NULL) == 0);
514     assert(pthread_cond_init(&log_cleanup_queue.queue_change_cv, NULL) == 0);
515     assert(pthread_attr_init(&attrs) == 0);
516
517     /* start up the reaper and log cleaner threads */
518     assert(pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED) == 0);
519     assert(pthread_create(&tid, 
520                           &attrs, 
521                           &SalvageChildReaperThread,
522                           NULL) == 0);
523     assert(pthread_create(&tid, 
524                           &attrs, 
525                           &SalvageLogCleanupThread,
526                           NULL) == 0);
527
528     /* loop forever serving requests */
529     while (1) {
530         node = SALVSYNC_getWork();
531         assert(node != NULL);
532
533         Log("dispatching child to salvage volume %u...\n",
534             node->command.sop.parent);
535
536         VOL_LOCK;
537         /* find a slot */
538         for (slot = 0; slot < Parallel; slot++) {
539           if (!child_slot[slot])
540             break;
541         }
542         assert (slot < Parallel);
543
544     do_fork:
545         pid = Fork();
546         if (pid == 0) {
547             VOL_UNLOCK;
548             ret = DoSalvageVolume(node, slot);
549             Exit(ret);
550         } else if (pid < 0) {
551             Log("failed to fork child worker process\n");
552             sleep(1);
553             goto do_fork;
554         } else {
555             child_slot[slot] = pid;
556             node->pid = pid;
557             VOL_UNLOCK;
558             
559             assert(pthread_mutex_lock(&worker_lock) == 0);
560             current_workers++;
561             
562             /* let the reaper thread know another worker was spawned */
563             assert(pthread_cond_broadcast(&worker_cv) == 0);
564             
565             /* if we're overquota, wait for the reaper */
566             while (current_workers >= Parallel) {
567                 assert(pthread_cond_wait(&worker_cv, &worker_lock) == 0);
568             }
569             assert(pthread_mutex_unlock(&worker_lock) == 0);
570         }
571     }
572 }
573
574 static int
575 DoSalvageVolume(struct SalvageQueueNode * node, int slot)
576 {
577     char childLog[AFSDIR_PATH_MAX];
578     int ret;
579     struct DiskPartition64 * partP;
580
581     /* do not allow further forking inside salvager */
582     canfork = 0;
583
584     /* do not attempt to close parent's logFile handle as
585      * another thread may have held the lock on the FILE
586      * structure when fork was called! */
587
588     afs_snprintf(childLog, sizeof(childLog), "%s.%d", 
589                  AFSDIR_SERVER_SLVGLOG_FILEPATH, getpid());
590
591     logFile = afs_fopen(childLog, "a");
592     if (!logFile) {             /* still nothing, use stdout */
593         logFile = stdout;
594         ShowLog = 0;
595     }
596
597     if (node->command.sop.parent <= 0) {
598         Log("salvageServer: invalid volume id specified; salvage aborted\n");
599         return 1;
600     }
601     
602     partP = VGetPartition(node->command.sop.partName, 0);
603     if (!partP) {
604         Log("salvageServer: Unknown or unmounted partition %s; salvage aborted\n", 
605             node->command.sop.partName);
606         return 1;
607     }
608
609     /* Salvage individual volume; don't notify fs */
610     SalvageFileSys1(partP, node->command.sop.parent);
611
612     fclose(logFile);
613     return 0;
614 }
615
616
617 static void *
618 SalvageChildReaperThread(void * args)
619 {
620     int slot, pid, status, code, found;
621     struct SalvageQueueNode *qp, *nqp;
622     struct log_cleanup_node * cleanup;
623     SALVSYNC_command_info info;
624
625     assert(pthread_mutex_lock(&worker_lock) == 0);
626
627     /* loop reaping our children */
628     while (1) {
629         /* wait() won't block unless we have children, so
630          * block on the cond var if we're childless */
631         while (current_workers == 0) {
632             assert(pthread_cond_wait(&worker_cv, &worker_lock) == 0);
633         }
634
635         assert(pthread_mutex_unlock(&worker_lock) == 0);
636
637         cleanup = (struct log_cleanup_node *) malloc(sizeof(struct log_cleanup_node));
638
639         while (Reap_Child("salvageserver", &pid, &status) < 0) {
640             /* try to prevent livelock if something goes wrong */
641             sleep(1);
642         }
643
644         VOL_LOCK;
645         for (slot = 0; slot < Parallel; slot++) {
646             if (child_slot[slot] == pid)
647                 break;
648         }
649         assert(slot < Parallel);
650         child_slot[slot] = 0;
651         VOL_UNLOCK;
652
653         SALVSYNC_doneWorkByPid(pid, status);
654
655         assert(pthread_mutex_lock(&worker_lock) == 0);
656
657         if (cleanup) {
658             cleanup->pid = pid;
659             queue_Append(&log_cleanup_queue, cleanup);
660             assert(pthread_cond_signal(&log_cleanup_queue.queue_change_cv) == 0);
661         }
662
663         /* ok, we've reaped a child */
664         current_workers--;
665         assert(pthread_cond_broadcast(&worker_cv) == 0);
666     }
667
668     return NULL;
669 }
670
671 static int
672 Reap_Child(char *prog, int * pid, int * status)
673 {
674     int ret;
675     ret = wait(status);
676
677     if (ret >= 0) {
678         *pid = ret;
679         if (WCOREDUMP(*status))
680             Log("\"%s\" core dumped!\n", prog);
681         if ((WIFSIGNALED(*status) != 0) ||
682             ((WEXITSTATUS(*status) != 0) &&
683              (WEXITSTATUS(*status) != SALSRV_EXIT_VOLGROUP_LINK)))
684             Log("\"%s\" (pid=%d) terminated abnormally!\n", prog, ret);
685     } else {
686         Log("wait returned -1\n");
687     }
688     return ret;
689 }
690
691 /*
692  * thread to combine salvager child logs
693  * back into the main salvageserver log
694  */
695 static void *
696 SalvageLogCleanupThread(void * arg)
697 {
698     struct log_cleanup_node * cleanup;
699
700     assert(pthread_mutex_lock(&worker_lock) == 0);
701
702     while (1) {
703         while (queue_IsEmpty(&log_cleanup_queue)) {
704             assert(pthread_cond_wait(&log_cleanup_queue.queue_change_cv, &worker_lock) == 0);
705         }
706
707         while (queue_IsNotEmpty(&log_cleanup_queue)) {
708             cleanup = queue_First(&log_cleanup_queue, log_cleanup_node);
709             queue_Remove(cleanup);
710             assert(pthread_mutex_unlock(&worker_lock) == 0);
711             SalvageLogCleanup(cleanup->pid);
712             free(cleanup);
713             assert(pthread_mutex_lock(&worker_lock) == 0);
714         }           
715     }
716
717     assert(pthread_mutex_unlock(&worker_lock) == 0);
718     return NULL;
719 }
720
721 #define LOG_XFER_BUF_SIZE 65536
722 static int
723 SalvageLogCleanup(int pid)
724 {
725     int pidlog, len;
726     char fn[AFSDIR_PATH_MAX];
727     static char buf[LOG_XFER_BUF_SIZE];
728
729     afs_snprintf(fn, sizeof(fn), "%s.%d", 
730                  AFSDIR_SERVER_SLVGLOG_FILEPATH, pid);
731     
732
733     pidlog = open(fn, O_RDONLY);
734     unlink(fn);
735     if (pidlog < 0)
736         return 1;
737
738     len = read(pidlog, buf, LOG_XFER_BUF_SIZE);
739     while (len) {
740         fwrite(buf, len, 1, logFile);
741         len = read(pidlog, buf, LOG_XFER_BUF_SIZE);
742     }
743
744     close(pidlog);
745
746     return 0;
747 }