Merge branch 'vendor/DIFFUTILS'
[dragonfly.git] / contrib / amd / hlfsd / homedir.c
1 /*
2  * Copyright (c) 1997-1999 Erez Zadok
3  * Copyright (c) 1989 Jan-Simon Pendry
4  * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
5  * Copyright (c) 1989 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Jan-Simon Pendry at Imperial College, London.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgment:
21  *      This product includes software developed by the University of
22  *      California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *      %W% (Berkeley) %G%
40  *
41  * $Id: homedir.c,v 1.4 1999/01/13 23:31:19 ezk Exp $
42  * $FreeBSD: src/contrib/amd/hlfsd/homedir.c,v 1.4 1999/09/15 05:45:15 obrien Exp $
43  * $DragonFly: src/contrib/amd/hlfsd/homedir.c,v 1.2 2003/06/17 04:23:57 dillon Exp $
44  *
45  * HLFSD was written at Columbia University Computer Science Department, by
46  * Erez Zadok <ezk@cs.columbia.edu> and Alexander Dupuy <dupuy@cs.columbia.edu>
47  * It is being distributed under the same terms and conditions as amd does.
48  */
49
50 #ifdef HAVE_CONFIG_H
51 # include <config.h>
52 #endif /* HAVE_CONFIG_H */
53 #include <am_defs.h>
54 #include <hlfsd.h>
55
56
57 /*
58  * STATIC VARIABLES AND FUNCTIONS:
59  */
60 static FILE *passwd_fp = NULL;
61 static char pw_name[16], pw_dir[128];
62 static int cur_pwtab_num = 0, max_pwtab_num = 0;
63 static int hlfsd_diskspace(char *);
64 static int hlfsd_stat(char *, struct stat *);
65 static int passwd_line = 0;
66 static int plt_reset(void);
67 static struct passwd passwd_ent;
68 static uid2home_t *lastchild;
69 static uid2home_t *pwtab;
70 static void delay(uid2home_t *, int);
71 static void table_add(int, const char *, const char *);
72
73 /* GLOBAL FUNCTIONS */
74 char *homeof(char *username);
75 int uidof(char *username);
76
77 /* GLOBALS VARIABLES */
78 char mboxfile[MAXPATHLEN];
79 username2uid_t *untab;          /* user name table */
80
81
82 /*
83  * Return the home directory pathname for the user with uid "userid".
84  */
85 char *
86 homedir(int userid)
87 {
88   static char linkval[MAXPATHLEN + 1];
89   static struct timeval tp;
90   uid2home_t *found;
91   char *homename;
92   struct stat homestat;
93
94   clock_valid = 0;              /* invalidate logging clock */
95
96   if ((int) userid == 0) {      /* force superuser to use "/" as home */
97     sprintf(linkval, "/%s", home_subdir);
98     return linkval;
99   }
100   if ((found = plt_search(userid)) == (uid2home_t *) NULL) {
101     return alt_spooldir;        /* use alt spool for unknown uid */
102   }
103   homename = found->home;
104
105   if (homename[0] != '/' || homename[1] == '\0') {
106     found->last_status = 1;
107     return alt_spooldir;        /* use alt spool for / or rel. home */
108   }
109   sprintf(linkval, "%s/%s", homename, home_subdir);
110
111   if (noverify) {
112     found->last_status = 0;
113     return linkval;
114   }
115
116   /*
117    * To optimize hlfsd, we don't actually check the validity of the
118    * symlink if it has been in checked in the last N seconds.  It is
119    * very likely that the link, machine, and filesystem are still
120    * valid, as long as N is small.  But if N ls large, that may not be
121    * true.  That's why the default N is 5 minutes, but we allow the
122    * user to override this value via a command line option.  Note that
123    * we do not update the last_access_time each time it is accessed,
124    * but only once every N seconds.
125    */
126   if (gettimeofday(&tp, (struct timezone *) NULL) < 0) {
127     tp.tv_sec = 0;
128   } else {
129     if ((tp.tv_sec - found->last_access_time) < cache_interval) {
130       if (found->last_status == 0) {
131         return linkval;
132       } else {
133         return alt_spooldir;
134       }
135     } else {
136       found->last_access_time = tp.tv_sec;
137     }
138   }
139
140 #ifdef DEBUG
141   /*
142    * only run this forking code if asked for -D fork
143    * or if did not ask for -D nofork
144    */
145   amuDebug(D_FORK) {
146 #endif /* DEBUG */
147     /* fork child to process request if none in progress */
148     if (found->child && kill(found->child, 0))
149       found->child = 0;
150
151     if (found->child)
152       delay(found, 5);          /* wait a bit if in progress */
153     if (found->child) {         /* better safe than sorry - maybe */
154       found->last_status = 1;
155       return alt_spooldir;
156     }
157     if ((found->child = fork()) < 0) {
158       found->last_status = 1;
159       return alt_spooldir;
160     }
161     if (found->child) {         /* PARENT */
162 #ifdef DEBUG
163       if (lastchild)
164         plog(XLOG_INFO, "cache spill uid = %ld, pid = %ld, home = %s",
165              (long) lastchild->uid, (long) lastchild->child,
166              lastchild->home);
167 #endif /* DEBUG */
168       lastchild = found;
169       return (char *) NULL;     /* return NULL to parent, so it can continue */
170     }
171 #ifdef DEBUG
172   }                             /* end of Debug(D_FORK) */
173 #endif /* DEBUG */
174
175   /*
176    * CHILD: (or parent if -D nofork)
177    *
178    * Check and create dir if needed.
179    * Check disk space and/or quotas too.
180    *
181    * We don't need to set the _last_status field of found after the fork
182    * in the child, b/c that information would be later determined in
183    * nfsproc_readlink_2() and the correct exit status would be returned
184    * to the parent upon SIGCHLD in interlock().
185    *
186    */
187   am_set_mypid();               /* for logging routines */
188   if (seteuid(userid) < 0) {
189     plog(XLOG_WARNING, "could not seteuid to %d: %m", userid);
190     return linkval;
191   }
192   if (hlfsd_stat(linkval, &homestat) < 0) {
193     if (errno == ENOENT) {      /* make the spool dir if possible */
194       /* don't use recursive mkdirs here */
195       if (mkdir(linkval, PERS_SPOOLMODE) < 0) {
196         seteuid(0);
197         plog(XLOG_WARNING, "can't make directory %s: %m", linkval);
198         return alt_spooldir;
199       }
200       /* fall through to testing the disk space / quota */
201     } else {                    /* the home dir itself must not exist then */
202       seteuid(0);
203       plog(XLOG_WARNING, "bad link to %s: %m", linkval);
204       return alt_spooldir;
205     }
206   }
207
208   /*
209    * If gets here, then either the spool dir in the home dir exists,
210    * or it was just created.  In either case, we now need to
211    * test if we can create a small file and write at least one
212    * byte into it.  This will test that we have both enough inodes
213    * and disk blocks to spare, or they fall within the user's quotas too.
214    * We are still seteuid to the user at this point.
215    */
216   if (hlfsd_diskspace(linkval) < 0) {
217     seteuid(0);
218     plog(XLOG_WARNING, "no more space in %s: %m", linkval);
219     return alt_spooldir;
220   } else {
221     seteuid(0);
222     return linkval;
223   }
224 }
225
226
227 static int
228 hlfsd_diskspace(char *path)
229 {
230   char buf[MAXPATHLEN];
231   int fd, len;
232
233   clock_valid = 0;              /* invalidate logging clock */
234
235   sprintf(buf, "%s/._hlfstmp_%lu", path, (long) getpid());
236   if ((fd = open(buf, O_RDWR | O_CREAT, 0600)) < 0) {
237     plog(XLOG_ERROR, "cannot open %s: %m", buf);
238     return -1;
239   }
240   len = strlen(buf);
241   if (write(fd, buf, len) < len) {
242     plog(XLOG_ERROR, "cannot write \"%s\" (%d bytes) to %s : %m", buf, len, buf);
243     close(fd);
244     unlink(buf);                /* cleanup just in case */
245     return -1;
246   }
247   if (unlink(buf) < 0) {
248     plog(XLOG_ERROR, "cannot unlink %s : %m", buf);
249   }
250   close(fd);
251   return 0;
252 }
253
254
255 static int
256 hlfsd_stat(char *path, struct stat *statp)
257 {
258   if (stat(path, statp) < 0)
259     return -1;
260   else if (!S_ISDIR(statp->st_mode)) {
261     errno = ENOTDIR;
262     return -1;
263   }
264   return 0;
265 }
266
267
268 static void
269 delay(uid2home_t *found, int secs)
270 {
271   struct timeval tv;
272
273 #ifdef DEBUG
274   if (found)
275     dlog("delaying on child %ld for %d seconds", (long) found->child, secs);
276 #endif /* DEBUG */
277
278   tv.tv_usec = 0;
279
280   do {
281     tv.tv_sec = secs;
282     if (select(0, 0, 0, 0, &tv) == 0)
283       break;
284   } while (--secs && found->child);
285 }
286
287
288 /*
289  * This function is called when a child has terminated after
290  * servicing an nfs request.  We need to check the exit status and
291  * update the last_status field of the requesting user.
292  */
293 RETSIGTYPE
294 interlock(int signum)
295 {
296   int child;
297   uid2home_t *lostchild;
298   int status;
299
300 #ifdef HAVE_WAITPID
301   while ((child = waitpid((pid_t) -1, &status, WNOHANG)) > 0) {
302 #else /* not HAVE_WAITPID */
303   while ((child = wait3(&status, WNOHANG, (struct rusage *) 0)) > 0) {
304 #endif /* not HAVE_WAITPID */
305
306     /* high chances this was the last child forked */
307     if (lastchild && lastchild->child == child) {
308       lastchild->child = 0;
309
310       if (WIFEXITED(status))
311         lastchild->last_status = WEXITSTATUS(status);
312       lastchild = (uid2home_t *) NULL;
313     } else {
314       /* and if not, we have to search for it... */
315       for (lostchild = pwtab; lostchild < &pwtab[cur_pwtab_num]; lostchild++) {
316         if (lostchild->child == child) {
317           if (WIFEXITED(status))
318             lostchild->last_status = WEXITSTATUS(status);
319           lostchild->child = 0;
320           break;
321         }
322       }
323     }
324   }
325 }
326
327
328 /*
329  * PASSWORD AND USERNAME LOOKUP TABLES FUNCTIONS
330  */
331
332 /*
333  * get index of UserName table entry which matches username.
334  * must not return uid_t because we want to return a negative number.
335  */
336 int
337 untab_index(char *username)
338 {
339   int max, min, mid, cmp;
340
341   max = cur_pwtab_num - 1;
342   min = 0;
343
344   do {
345     mid = (max + min) / 2;
346     cmp = strcmp(untab[mid].username, username);
347     if (cmp == 0)               /* record found! */
348       return mid;
349     if (cmp > 0)
350       max = mid;
351     else
352       min = mid;
353   } while (max > min + 1);
354
355   if (STREQ(untab[max].username, username))
356     return max;
357   if (STREQ(untab[min].username, username))
358     return min;
359
360   /* if gets here then record was not found */
361   return -1;
362 }
363
364
365 /*
366  * Don't make this return a uid_t, because we need to return negative
367  * numbers as well (error codes.)
368  */
369 int
370 uidof(char *username)
371 {
372   int idx;
373
374   if ((idx = untab_index(username)) < 0)        /* not found */
375     return INVALIDID;                   /* an invalid user id */
376   return untab[idx].uid;
377 }
378
379
380 /*
381  * Don't make this return a uid_t, because we need to return negative
382  * numbers as well (error codes.)
383  */
384 char *
385 homeof(char *username)
386 {
387   int idx;
388
389   if ((idx = untab_index(username)) < 0)        /* not found */
390     return (char *) NULL;       /* an invalid user id */
391   return untab[idx].home;
392 }
393
394
395 char *
396 mailbox(int uid, char *username)
397 {
398   char *home;
399
400   if (uid < 0)
401     return (char *) NULL;       /* not found */
402
403   if ((home = homeof(username)) == (char *) NULL)
404     return (char *) NULL;
405   if (STREQ(home, "/"))
406     sprintf(mboxfile, "/%s/%s", home_subdir, username);
407   else
408     sprintf(mboxfile, "%s/%s/%s", home, home_subdir, username);
409   return mboxfile;
410 }
411
412
413 static int
414 plt_compare_fxn(const voidp x, const voidp y)
415
416 {
417   uid2home_t *i = (uid2home_t *) x;
418   uid2home_t *j = (uid2home_t *) y;
419
420   return i->uid - j->uid;
421 }
422
423
424 static int
425 unt_compare_fxn(const voidp x, const voidp y)
426 {
427   username2uid_t *i = (username2uid_t *) x;
428   username2uid_t *j = (username2uid_t *) y;
429
430   return strcmp(i->username, j->username);
431 }
432
433
434 /* perform initialization of user passwd database */
435 static void
436 hlfsd_setpwent(void)
437 {
438   if (!passwdfile) {
439     setpwent();
440     return;
441   }
442
443   passwd_fp = fopen(passwdfile, "r");
444   if (!passwd_fp) {
445     plog(XLOG_ERROR, "unable to read passwd file %s: %m", passwdfile);
446     return;
447   }
448   plog(XLOG_INFO, "reading password entries from file %s", passwdfile);
449
450   passwd_line = 0;
451   memset((char *) &passwd_ent, 0, sizeof(struct passwd));
452   passwd_ent.pw_name = (char *) &pw_name;
453   passwd_ent.pw_dir = (char *) &pw_dir;
454 }
455
456
457 /* perform de-initialization of user passwd database */
458 static void
459 hlfsd_endpwent(void)
460 {
461   if (!passwdfile) {
462     /*
463      * Don't actually run this because we will be making more passwd calls
464      * afterwards.  On Solaris 2.5.1, making getpwent() calls after calling
465      * endpwent() results in a memory leak! (and no, even Purify didn't
466      * detect it...)
467      *
468      endpwent();
469      */
470     return;
471   }
472
473   if (passwd_fp) {
474     fclose(passwd_fp);
475   }
476 }
477
478
479 /* perform record reading/parsing of individual passwd database records */
480 static struct passwd *
481 hlfsd_getpwent(void)
482 {
483   char buf[256], *cp;
484
485   /* check if to perform standard unix function */
486   if (!passwdfile) {
487     return getpwent();
488   }
489
490   clock_valid = 0;              /* invalidate logging clock */
491
492   /* return here to read another entry */
493 readent:
494
495   /* return NULL if reached end of file */
496   if (feof(passwd_fp))
497     return NULL;
498
499   pw_name[0] = pw_dir[0] = '\0';
500
501   /* read records */
502   buf[0] = '\0';
503   fgets(buf, 256, passwd_fp);
504   passwd_line++;
505   if (!buf || buf[0] == '\0')
506     goto readent;
507
508   /* read user name */
509   cp = strtok(buf, ":");
510   if (!cp || cp[0] == '\0') {
511     plog(XLOG_ERROR, "no user name on line %d of %s", passwd_line, passwdfile);
512     goto readent;
513   }
514   strcpy(pw_name, cp);          /* will show up in passwd_ent.pw_name */
515
516   /* skip passwd */
517   strtok(NULL, ":");
518
519   /* read uid */
520   cp = strtok(NULL, ":");
521   if (!cp || cp[0] == '\0') {
522     plog(XLOG_ERROR, "no uid on line %d of %s", passwd_line, passwdfile);
523     goto readent;
524   }
525   passwd_ent.pw_uid = atoi(cp);
526
527   /* skip gid and gcos */
528   strtok(NULL, ":");
529   strtok(NULL, ":");
530
531   /* read home dir */
532   cp = strtok(NULL, ":");
533   if (!cp || cp[0] == '\0') {
534     plog(XLOG_ERROR, "no home dir on line %d of %s", passwd_line,  passwdfile);
535     goto readent;
536   }
537   strcpy(pw_dir, cp);   /* will show up in passwd_ent.pw_dir */
538
539   /* the rest of the fields are unimportant and not being considered */
540
541   plog(XLOG_USER, "hlfsd_getpwent: name=%s, uid=%ld, dir=%s",
542        passwd_ent.pw_name, (long) passwd_ent.pw_uid, passwd_ent.pw_dir);
543
544   return &passwd_ent;
545 }
546
547
548 /*
549  * read and hash the passwd file or NIS map
550  */
551 void
552 plt_init(void)
553 {
554   struct passwd *pent_p;
555
556   if (plt_reset() < 0)          /* could not reset table. skip. */
557     return;
558
559   plog(XLOG_INFO, "reading password map");
560
561   hlfsd_setpwent();                     /* prepare to read passwd entries */
562   while ((pent_p = hlfsd_getpwent()) != (struct passwd *) NULL) {
563     table_add(pent_p->pw_uid, pent_p->pw_dir, pent_p->pw_name);
564   }
565   hlfsd_endpwent();
566
567   qsort((char *) pwtab, cur_pwtab_num, sizeof(uid2home_t),
568         plt_compare_fxn);
569   qsort((char *) untab, cur_pwtab_num, sizeof(username2uid_t),
570         unt_compare_fxn);
571
572   plog(XLOG_INFO, "password map read and sorted");
573 }
574
575
576 /*
577  * This is essentially so that we don't reset known good lookup tables when a
578  * YP server goes down.
579  */
580 static int
581 plt_reset(void)
582 {
583   int i;
584
585   clock_valid = 0;              /* invalidate logging clock */
586
587   hlfsd_setpwent();
588   if (hlfsd_getpwent() == (struct passwd *) NULL) {
589     hlfsd_endpwent();
590     return -1;                  /* did not reset table */
591   }
592   hlfsd_endpwent();
593
594   lastchild = (uid2home_t *) NULL;
595
596   if (max_pwtab_num > 0)        /* was used already. cleanup old table */
597     for (i = 0; i < cur_pwtab_num; ++i) {
598       if (pwtab[i].home) {
599         XFREE(pwtab[i].home);
600         pwtab[i].home = (char *) NULL;
601       }
602       pwtab[i].uid = INVALIDID; /* not a valid uid (yet...) */
603       pwtab[i].child = (pid_t) 0;
604       pwtab[i].uname = (char *) NULL;   /* only a ptr to untab[i].username */
605       if (untab[i].username) {
606         XFREE(untab[i].username);
607         untab[i].username = (char *) NULL;
608       }
609       untab[i].uid = INVALIDID; /* invalid uid */
610       untab[i].home = (char *) NULL;    /* only a ptr to pwtab[i].home  */
611     }
612   cur_pwtab_num = 0;            /* zero current size */
613
614   return 0;                     /* resetting ok */
615 }
616
617
618 /*
619  * u: uid number
620  * h: home directory
621  * n: user ID name
622  */
623 static void
624 table_add(int u, const char *h, const char *n)
625 {
626   int i;
627
628   clock_valid = 0;              /* invalidate logging clock */
629
630   if (max_pwtab_num <= 0) {     /* was never initialized */
631     max_pwtab_num = 1;
632     pwtab = (uid2home_t *) xmalloc(max_pwtab_num *
633                                    sizeof(uid2home_t));
634     memset((char *) &pwtab[0], 0, max_pwtab_num * sizeof(uid2home_t));
635     untab = (username2uid_t *) xmalloc(max_pwtab_num *
636                                        sizeof(username2uid_t));
637     memset((char *) &untab[0], 0, max_pwtab_num * sizeof(username2uid_t));
638   }
639
640   /* check if need more space. */
641   if (cur_pwtab_num + 1 > max_pwtab_num) {
642     /* need more space in table */
643     max_pwtab_num *= 2;
644     plog(XLOG_INFO, "reallocating table spaces to %d entries", max_pwtab_num);
645     pwtab = (uid2home_t *) xrealloc(pwtab,
646                                     sizeof(uid2home_t) * max_pwtab_num);
647     untab = (username2uid_t *) xrealloc(untab,
648                                         sizeof(username2uid_t) *
649                                         max_pwtab_num);
650     /* zero out newly added entries */
651     for (i=cur_pwtab_num; i<max_pwtab_num; ++i) {
652       memset((char *) &pwtab[i], 0, sizeof(uid2home_t));
653       memset((char *) &untab[i], 0, sizeof(username2uid_t));
654     }
655   }
656
657   /* do NOT add duplicate entries (this is an O(N^2) algorithm... */
658   for (i=0; i<cur_pwtab_num; ++i)
659     if (u == pwtab[i].uid  &&  u != 0 ) {
660 #ifdef DEBUG
661       dlog("ignoring duplicate home %s for uid %d (already %s)",
662            h, u, pwtab[i].home);
663 #endif /* DEBUG */
664       return;
665     }
666
667   /* add new password entry */
668   pwtab[cur_pwtab_num].home = strdup(h);
669   pwtab[cur_pwtab_num].child = 0;
670   pwtab[cur_pwtab_num].last_access_time = 0;
671   pwtab[cur_pwtab_num].last_status = 0; /* assume best: used homedir */
672   pwtab[cur_pwtab_num].uid = u;
673
674   /* add new userhome entry */
675   untab[cur_pwtab_num].username = strdup(n);
676
677   /* just a second pointer */
678   pwtab[cur_pwtab_num].uname = untab[cur_pwtab_num].username;
679   untab[cur_pwtab_num].uid = u;
680   untab[cur_pwtab_num].home = pwtab[cur_pwtab_num].home;        /* a ptr */
681
682   /* increment counter */
683   ++cur_pwtab_num;
684 }
685
686
687 /*
688  * return entry in lookup table
689  */
690 uid2home_t *
691 plt_search(int u)
692 {
693   int max, min, mid;
694
695   /*
696    * empty table should not happen,
697    * but I have a bug with signals to trace...
698    */
699   if (pwtab == (uid2home_t *) NULL)
700     return (uid2home_t *) NULL;
701
702   max = cur_pwtab_num - 1;
703   min = 0;
704
705   do {
706     mid = (max + min) / 2;
707     if (pwtab[mid].uid == u)    /* record found! */
708       return &pwtab[mid];
709     if (pwtab[mid].uid > u)
710       max = mid;
711     else
712       min = mid;
713   } while (max > min + 1);
714
715   if (pwtab[max].uid == u)
716     return &pwtab[max];
717   if (pwtab[min].uid == u)
718     return &pwtab[min];
719
720   /* if gets here then record was not found */
721   return (uid2home_t *) NULL;
722 }
723
724
725 #if defined(DEBUG) || defined(DEBUG_PRINT)
726 void
727 plt_print(int signum)
728 {
729   FILE *dumpfile;
730   int dumpfd;
731   char dumptmp[] = "/usr/tmp/hlfsd.dump.XXXXXX";
732   int i;
733
734 #ifdef HAVE_MKSTEMP
735   dumpfd = mkstemp(dumptmp);
736 #else /* not HAVE_MKSTEMP */
737   mktemp(dumptmp);
738   if (!dumptmp) {
739     plot(XLOG_ERROR, "cannot create temporary dump file");
740     return;
741   }
742   dumpfd = open(dumptmp, O_RDONLY);
743 #endif /* not HAVE_MKSTEMP */
744   if (dumpfd < 0) {
745     plog(XLOG_ERROR, "cannot open temporary dump file");
746     return;
747   }
748   if ((dumpfile = fdopen(dumpfd, "a")) != NULL) {
749     plog(XLOG_INFO, "dumping internal state to file %s", dumptmp);
750     fprintf(dumpfile, "\n\nNew plt_dump():\n");
751     for (i = 0; i < cur_pwtab_num; ++i)
752       fprintf(dumpfile,
753               "%4d %5lu %10lu %1d %4lu \"%s\" uname=\"%s\"\n",
754               i,
755               (long) pwtab[i].child,
756               pwtab[i].last_access_time,
757               pwtab[i].last_status,
758               (long) pwtab[i].uid,
759               pwtab[i].home,
760               pwtab[i].uname);
761     fprintf(dumpfile, "\nUserName table by plt_print():\n");
762     for (i = 0; i < cur_pwtab_num; ++i)
763       fprintf(dumpfile, "%4d : \"%s\" %4lu \"%s\"\n", i,
764               untab[i].username, (long) untab[i].uid, untab[i].home);
765     close(dumpfd);
766     fclose(dumpfile);
767   }
768 }
769
770
771 void
772 plt_dump(uid2home_t *lastc, pid_t this)
773 {
774   FILE *dumpfile;
775   int i;
776
777   if ((dumpfile = fopen("/var/tmp/hlfsdump", "a")) != NULL) {
778     fprintf(dumpfile, "\n\nNEW PLT_DUMP -- ");
779     fprintf(dumpfile, "lastchild->child=%d ",
780             (int) (lastc ? lastc->child : -999));
781     fprintf(dumpfile, ", child from wait3=%lu:\n", (long) this);
782     for (i = 0; i < cur_pwtab_num; ++i)
783       fprintf(dumpfile, "%4d %5lu: %4lu \"%s\" uname=\"%s\"\n", i,
784               (long) pwtab[i].child, (long) pwtab[i].uid,
785               pwtab[i].home, pwtab[i].uname);
786     fprintf(dumpfile, "\nUserName table by plt_dump():\n");
787     for (i = 0; i < cur_pwtab_num; ++i)
788       fprintf(dumpfile, "%4d : \"%s\" %4lu \"%s\"\n", i,
789               untab[i].username, (long) untab[i].uid, untab[i].home);
790     fprintf(dumpfile, "ezk: ent=%d, uid=%lu, home=\"%s\"\n",
791             untab_index("ezk"),
792             (long) untab[untab_index("ezk")].uid,
793             pwtab[untab[untab_index("ezk")].uid].home);
794     fprintf(dumpfile, "rezk: ent=%d, uid=%lu, home=\"%s\"\n",
795             untab_index("rezk"),
796             (long) untab[untab_index("rezk")].uid,
797             pwtab[untab[untab_index("rezk")].uid].home);
798     fclose(dumpfile);
799   }
800 }
801 #endif /* defined(DEBUG) || defined(DEBUG_PRINT) */