conf.c revision 1.27 1 /* $NetBSD: conf.c,v 1.27 2000/01/10 08:03:50 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1997-2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Simon Burge and Luke Mewburn.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __RCSID("$NetBSD: conf.c,v 1.27 2000/01/10 08:03:50 lukem Exp $");
42 #endif /* not lint */
43
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/stat.h>
47
48 #include <ctype.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <glob.h>
52 #include <signal.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <stringlist.h>
57 #include <syslog.h>
58 #include <time.h>
59 #include <unistd.h>
60 #include <util.h>
61
62 #ifdef KERBEROS5
63 #include <krb5/krb5.h>
64 #endif
65
66 #include "extern.h"
67 #include "pathnames.h"
68
69 static char *strend __P((const char *, char *));
70 static int filetypematch __P((char *, int));
71
72 struct ftpclass curclass;
73
74
75 /*
76 * Initialise curclass to an `empty' state
77 */
78 void
79 init_curclass()
80 {
81 struct ftpconv *conv, *cnext;
82
83 for (conv = curclass.conversions; conv != NULL; conv = cnext) {
84 REASSIGN(conv->suffix, NULL);
85 REASSIGN(conv->types, NULL);
86 REASSIGN(conv->disable, NULL);
87 REASSIGN(conv->command, NULL);
88 cnext = conv->next;
89 free(conv);
90 }
91 curclass.checkportcmd = 0;
92 REASSIGN(curclass.classname, NULL);
93 curclass.conversions = NULL;
94 REASSIGN(curclass.display, NULL);
95 curclass.limit = -1; /* unlimited connections */
96 REASSIGN(curclass.limitfile, NULL);
97 curclass.maxrateget = 0;
98 curclass.maxrateput = 0;
99 curclass.maxtimeout = 7200; /* 2 hours */
100 curclass.modify = 1;
101 REASSIGN(curclass.motd, xstrdup(_PATH_FTPLOGINMESG));
102 REASSIGN(curclass.notify, NULL);
103 curclass.passive = 1;
104 curclass.rateget = 0;
105 curclass.rateput = 0;
106 curclass.timeout = 900; /* 15 minutes */
107 curclass.umask = 027;
108 curclass.upload = 1;
109
110 }
111
112 /*
113 * Parse the configuration file, looking for the named class, and
114 * define curclass to contain the appropriate settings.
115 */
116 void
117 parse_conf(findclass)
118 char *findclass;
119 {
120 FILE *f;
121 char *buf, *p;
122 size_t len;
123 int none, match, rate;
124 char *endp;
125 char *class, *word, *arg, *template;
126 const char *infile;
127 size_t line;
128 unsigned int timeout;
129 struct ftpconv *conv, *cnext;
130
131 init_curclass();
132 REASSIGN(curclass.classname, xstrdup(findclass));
133 if (strcasecmp(findclass, "guest") == 0) {
134 curclass.modify = 0;
135 curclass.umask = 0707;
136 }
137
138 infile = conffilename(_PATH_FTPDCONF);
139 if ((f = fopen(infile, "r")) == NULL)
140 return;
141
142 line = 0;
143 template = NULL;
144 for (;
145 (buf = fparseln(f, &len, &line, NULL, FPARSELN_UNESCCOMM |
146 FPARSELN_UNESCCONT | FPARSELN_UNESCESC)) != NULL;
147 free(buf)) {
148 none = match = 0;
149 p = buf;
150 if (len < 1)
151 continue;
152 if (p[len - 1] == '\n')
153 p[--len] = '\0';
154 if (EMPTYSTR(p))
155 continue;
156
157 NEXTWORD(p, word);
158 NEXTWORD(p, class);
159 NEXTWORD(p, arg);
160 if (EMPTYSTR(word) || EMPTYSTR(class))
161 continue;
162 if (strcasecmp(class, "none") == 0)
163 none = 1;
164 if (! (strcasecmp(class, findclass) == 0 ||
165 (template != NULL && strcasecmp(class, template) == 0) ||
166 none ||
167 strcasecmp(class, "all") == 0) )
168 continue;
169
170 if (strcasecmp(word, "checkportcmd") == 0) {
171 if (none ||
172 (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
173 curclass.checkportcmd = 0;
174 else
175 curclass.checkportcmd = 1;
176
177 } else if (strcasecmp(word, "classtype") == 0) {
178 if (!none && !EMPTYSTR(arg)) {
179 if (strcasecmp(arg, "GUEST") == 0)
180 curclass.type = CLASS_GUEST;
181 else if (strcasecmp(arg, "CHROOT") == 0)
182 curclass.type = CLASS_CHROOT;
183 else if (strcasecmp(arg, "REAL") == 0)
184 curclass.type = CLASS_REAL;
185 else {
186 syslog(LOG_WARNING,
187 "%s line %d: unknown class type `%s'",
188 infile, (int)line, arg);
189 continue;
190 }
191 }
192
193 } else if (strcasecmp(word, "conversion") == 0) {
194 char *suffix, *types, *disable, *convcmd;
195
196 if (EMPTYSTR(arg)) {
197 syslog(LOG_WARNING,
198 "%s line %d: %s requires a suffix",
199 infile, (int)line, word);
200 continue; /* need a suffix */
201 }
202 NEXTWORD(p, types);
203 NEXTWORD(p, disable);
204 convcmd = p;
205 if (convcmd)
206 convcmd += strspn(convcmd, " \t");
207 suffix = xstrdup(arg);
208 if (none || EMPTYSTR(types) ||
209 EMPTYSTR(disable) || EMPTYSTR(convcmd)) {
210 types = NULL;
211 disable = NULL;
212 convcmd = NULL;
213 } else {
214 types = xstrdup(types);
215 disable = xstrdup(disable);
216 convcmd = xstrdup(convcmd);
217 }
218 for (conv = curclass.conversions; conv != NULL;
219 conv = conv->next) {
220 if (strcmp(conv->suffix, suffix) == 0)
221 break;
222 }
223 if (conv == NULL) {
224 conv = (struct ftpconv *)
225 calloc(1, sizeof(struct ftpconv));
226 if (conv == NULL) {
227 syslog(LOG_WARNING, "can't malloc");
228 continue;
229 }
230 conv->next = NULL;
231 for (cnext = curclass.conversions;
232 cnext != NULL; cnext = cnext->next)
233 if (cnext->next == NULL)
234 break;
235 if (cnext != NULL)
236 cnext->next = conv;
237 else
238 curclass.conversions = conv;
239 }
240 REASSIGN(conv->suffix, suffix);
241 REASSIGN(conv->types, types);
242 REASSIGN(conv->disable, disable);
243 REASSIGN(conv->command, convcmd);
244
245 } else if (strcasecmp(word, "display") == 0) {
246 if (none || EMPTYSTR(arg))
247 arg = NULL;
248 else
249 arg = xstrdup(arg);
250 REASSIGN(curclass.display, arg);
251
252 } else if (strcasecmp(word, "limit") == 0) {
253 int limit;
254
255 if (none || EMPTYSTR(arg))
256 continue;
257 limit = (int)strtol(arg, &endp, 10);
258 if (*endp != 0) {
259 syslog(LOG_WARNING,
260 "%s line %d: invalid limit %s",
261 infile, (int)line, arg);
262 continue;
263 }
264 curclass.limit = limit;
265 REASSIGN(curclass.limitfile,
266 EMPTYSTR(p) ? NULL : xstrdup(p));
267
268 } else if (strcasecmp(word, "maxtimeout") == 0) {
269 if (none || EMPTYSTR(arg))
270 continue;
271 timeout = (unsigned int)strtoul(arg, &endp, 10);
272 if (*endp != 0) {
273 syslog(LOG_WARNING,
274 "%s line %d: invalid maxtimeout %s",
275 infile, (int)line, arg);
276 continue;
277 }
278 if (timeout < 30) {
279 syslog(LOG_WARNING,
280 "%s line %d: maxtimeout %d < 30 seconds",
281 infile, (int)line, timeout);
282 continue;
283 }
284 if (timeout < curclass.timeout) {
285 syslog(LOG_WARNING,
286 "%s line %d: maxtimeout %d < timeout (%d)",
287 infile, (int)line, timeout,
288 curclass.timeout);
289 continue;
290 }
291 curclass.maxtimeout = timeout;
292
293 } else if (strcasecmp(word, "modify") == 0) {
294 if (none ||
295 (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
296 curclass.modify = 0;
297 else
298 curclass.modify = 1;
299
300 } else if (strcasecmp(word, "motd") == 0) {
301 if (none || EMPTYSTR(arg))
302 arg = NULL;
303 else
304 arg = xstrdup(arg);
305 REASSIGN(curclass.motd, arg);
306
307
308 } else if (strcasecmp(word, "notify") == 0) {
309 if (none || EMPTYSTR(arg))
310 arg = NULL;
311 else
312 arg = xstrdup(arg);
313 REASSIGN(curclass.notify, arg);
314
315 } else if (strcasecmp(word, "passive") == 0) {
316 if (none ||
317 (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0))
318 curclass.passive = 0;
319 else
320 curclass.passive = 1;
321
322 } else if (strcasecmp(word, "rateget") == 0) {
323 if (none || EMPTYSTR(arg))
324 continue;
325 rate = strsuftoi(arg);
326 if (rate == -1) {
327 syslog(LOG_WARNING,
328 "%s line %d: invalid rateget %s",
329 infile, (int)line, arg);
330 continue;
331 }
332 curclass.maxrateget = rate;
333 curclass.rateget = rate;
334
335 } else if (strcasecmp(word, "rateput") == 0) {
336 if (none || EMPTYSTR(arg))
337 continue;
338 rate = strsuftoi(arg);
339 if (rate == -1) {
340 syslog(LOG_WARNING,
341 "%s line %d: invalid rateput %s",
342 infile, (int)line, arg);
343 continue;
344 }
345 curclass.maxrateput = rate;
346 curclass.rateput = rate;
347
348 } else if (strcasecmp(word, "timeout") == 0) {
349 if (none || EMPTYSTR(arg))
350 continue;
351 timeout = (unsigned int)strtoul(arg, &endp, 10);
352 if (*endp != 0) {
353 syslog(LOG_WARNING,
354 "%s line %d: invalid timeout %s",
355 infile, (int)line, arg);
356 continue;
357 }
358 if (timeout < 30) {
359 syslog(LOG_WARNING,
360 "%s line %d: timeout %d < 30 seconds",
361 infile, (int)line, timeout);
362 continue;
363 }
364 if (timeout > curclass.maxtimeout) {
365 syslog(LOG_WARNING,
366 "%s line %d: timeout %d > maxtimeout (%d)",
367 infile, (int)line, timeout,
368 curclass.maxtimeout);
369 continue;
370 }
371 curclass.timeout = timeout;
372
373 } else if (strcasecmp(word, "template") == 0) {
374 if (none)
375 continue;
376 REASSIGN(template, EMPTYSTR(arg) ? NULL : xstrdup(arg));
377
378 } else if (strcasecmp(word, "umask") == 0) {
379 mode_t umask;
380
381 if (none || EMPTYSTR(arg))
382 continue;
383 umask = (mode_t)strtoul(arg, &endp, 8);
384 if (*endp != 0 || umask > 0777) {
385 syslog(LOG_WARNING,
386 "%s line %d: invalid umask %s",
387 infile, (int)line, arg);
388 continue;
389 }
390 curclass.umask = umask;
391
392 } else if (strcasecmp(word, "upload") == 0) {
393 if (none ||
394 (!EMPTYSTR(arg) && strcasecmp(arg, "off") == 0)) {
395 curclass.modify = 0;
396 curclass.upload = 0;
397 } else
398 curclass.upload = 1;
399
400 } else {
401 syslog(LOG_WARNING,
402 "%s line %d: unknown directive '%s'",
403 infile, (int)line, word);
404 continue;
405 }
406 }
407 REASSIGN(template, NULL);
408 fclose(f);
409 }
410
411 /*
412 * Show file listed in curclass.display first time in, and list all the
413 * files named in curclass.notify in the current directory. Send back
414 * responses with the prefix `code' + "-".
415 */
416 void
417 show_chdir_messages(code)
418 int code;
419 {
420 static StringList *slist = NULL;
421
422 struct stat st;
423 struct tm *t;
424 glob_t gl;
425 time_t now, then;
426 int age;
427 char cwd[MAXPATHLEN];
428 char *cp, **rlist;
429
430 /* Setup list for directory cache */
431 if (slist == NULL)
432 slist = sl_init();
433 if (slist == NULL) {
434 syslog(LOG_WARNING, "can't allocate memory for stringlist");
435 return;
436 }
437
438 /* Check if this directory has already been visited */
439 if (getcwd(cwd, sizeof(cwd) - 1) == NULL) {
440 syslog(LOG_WARNING, "can't getcwd: %s", strerror(errno));
441 return;
442 }
443 if (sl_find(slist, cwd) != NULL)
444 return;
445
446 cp = xstrdup(cwd);
447 if (sl_add(slist, cp) == -1)
448 syslog(LOG_WARNING, "can't add `%s' to stringlist", cp);
449
450 /* First check for a display file */
451 (void)format_file(curclass.display, code);
452
453 /* Now see if there are any notify files */
454 if (EMPTYSTR(curclass.notify))
455 return;
456
457 if (glob(curclass.notify, 0, NULL, &gl) != 0 || gl.gl_matchc == 0)
458 return;
459 time(&now);
460 for (rlist = gl.gl_pathv; *rlist != NULL; rlist++) {
461 if (stat(*rlist, &st) != 0)
462 continue;
463 if (!S_ISREG(st.st_mode))
464 continue;
465 then = st.st_mtime;
466 if (code != 0) {
467 lreply(code, "");
468 code = 0;
469 }
470 lreply(code, "Please read the file %s", *rlist);
471 t = localtime(&now);
472 age = 365 * t->tm_year + t->tm_yday;
473 t = localtime(&then);
474 age -= 365 * t->tm_year + t->tm_yday;
475 lreply(code, " it was last modified on %.24s - %d day%s ago",
476 ctime(&then), age, PLURAL(age));
477 }
478 globfree(&gl);
479 }
480
481 int
482 format_file(file, code)
483 const char *file;
484 int code;
485 {
486 FILE *f;
487 char *buf, *p, *cwd;
488 size_t len;
489 off_t b;
490 time_t now;
491
492 #define PUTC(x) putchar(x), b++
493
494 if (EMPTYSTR(file))
495 return(0);
496 if ((f = fopen(file, "r")) == NULL)
497 return (0);
498 lreply(code, "");
499
500 b = 0;
501 for (;
502 (buf = fparseln(f, &len, NULL, "\0\0\0", 0)) != NULL; free(buf)) {
503 if (len > 0)
504 if (buf[len - 1] == '\n')
505 buf[--len] = '\0';
506 b += printf(" ");
507
508 for (p = buf; *p; p++) {
509 if (*p == '%') {
510 p++;
511 switch (*p) {
512
513 case 'c':
514 b += printf("%s",
515 curclass.classname ?
516 curclass.classname : "<unknown>");
517 break;
518
519 case 'C':
520 if (getcwd(cwd, sizeof(cwd)-1) == NULL){
521 syslog(LOG_WARNING,
522 "can't getcwd: %s",
523 strerror(errno));
524 continue;
525 }
526 b += printf("%s", cwd);
527 break;
528
529 case 'E':
530 /* XXXX email address */
531 break;
532
533 case 'L':
534 b += printf("%s", hostname);
535 break;
536
537 case 'M':
538 if (curclass.limit == -1)
539 b += printf("unlimited");
540 else
541 b += printf("%d",
542 curclass.limit);
543 break;
544
545 case 'N':
546 if (connections > 0)
547 b += printf("%d", connections);
548 break;
549
550 case 'R':
551 b += printf("%s", remotehost);
552 break;
553
554 case 'T':
555 now = time(NULL);
556 b += printf("%.24s", ctime(&now));
557 break;
558
559 case 'U':
560 b += printf("%s",
561 pw ? pw->pw_name : "<unknown>");
562 break;
563
564 case '%':
565 PUTC('%');
566 break;
567
568 }
569 } else {
570 PUTC(*p);
571 }
572 }
573 PUTC('\r');
574 PUTC('\n');
575 }
576
577 total_bytes += b;
578 total_bytes_out += b;
579 (void)fflush(stdout);
580 (void)fclose(f);
581 return (1);
582 }
583
584 /*
585 * Find s2 at the end of s1. If found, return a string up to (but
586 * not including) s2, otherwise returns NULL.
587 */
588 static char *
589 strend(s1, s2)
590 const char *s1;
591 char *s2;
592 {
593 static char buf[MAXPATHLEN];
594
595 char *start;
596 size_t l1, l2;
597
598 l1 = strlen(s1);
599 l2 = strlen(s2);
600
601 if (l2 >= l1)
602 return(NULL);
603
604 strlcpy(buf, s1, sizeof(buf));
605 start = buf + (l1 - l2);
606
607 if (strcmp(start, s2) == 0) {
608 *start = '\0';
609 return(buf);
610 } else
611 return(NULL);
612 }
613
614 static int
615 filetypematch(types, mode)
616 char *types;
617 int mode;
618 {
619 for ( ; types[0] != '\0'; types++)
620 switch (*types) {
621 case 'd':
622 if (S_ISDIR(mode))
623 return(1);
624 break;
625 case 'f':
626 if (S_ISREG(mode))
627 return(1);
628 break;
629 }
630 return(0);
631 }
632
633 /*
634 * Look for a conversion. If we succeed, return a pointer to the
635 * command to execute for the conversion.
636 *
637 * The command is stored in a static array so there's no memory
638 * leak problems, and not too much to change in ftpd.c. This
639 * routine doesn't need to be re-entrant unless we start using a
640 * multi-threaded ftpd, and that's not likely for a while...
641 */
642 char **
643 do_conversion(fname)
644 const char *fname;
645 {
646 struct ftpconv *cp;
647 struct stat st;
648 int o_errno;
649 char *base = NULL;
650 char *cmd, *p, *lp, **argv;
651 StringList *sl;
652
653 o_errno = errno;
654 sl = NULL;
655 cmd = NULL;
656 for (cp = curclass.conversions; cp != NULL; cp = cp->next) {
657 if (cp->suffix == NULL) {
658 syslog(LOG_WARNING,
659 "cp->suffix==NULL in conv list; SHOULDN'T HAPPEN!");
660 continue;
661 }
662 if ((base = strend(fname, cp->suffix)) == NULL)
663 continue;
664 if (cp->types == NULL || cp->disable == NULL ||
665 cp->command == NULL)
666 continue;
667 /* Is it enabled? */
668 if (strcmp(cp->disable, ".") != 0 &&
669 stat(cp->disable, &st) == 0)
670 continue;
671 /* Does the base exist? */
672 if (stat(base, &st) < 0)
673 continue;
674 /* Is the file type ok */
675 if (!filetypematch(cp->types, st.st_mode))
676 continue;
677 break; /* "We have a winner!" */
678 }
679
680 /* If we got through the list, no conversion */
681 if (cp == NULL)
682 goto cleanup_do_conv;
683
684 /* Split up command into an argv */
685 if ((sl = sl_init()) == NULL)
686 goto cleanup_do_conv;
687 cmd = xstrdup(cp->command);
688 p = cmd;
689 while (p) {
690 NEXTWORD(p, lp);
691 if (strcmp(lp, "%s") == 0)
692 lp = base;
693 if (sl_add(sl, xstrdup(lp)) == -1)
694 goto cleanup_do_conv;
695 }
696
697 if (sl_add(sl, NULL) == -1)
698 goto cleanup_do_conv;
699 argv = sl->sl_str;
700 free(cmd);
701 free(sl);
702 return(argv);
703
704 cleanup_do_conv:
705 if (sl)
706 sl_free(sl, 1);
707 free(cmd);
708 errno = o_errno;
709 return(NULL);
710 }
711
712 /*
713 * Convert the string `arg' to an int, which may have an optional SI suffix
714 * (`b', `k', `m', `g'). Returns the number for success, -1 otherwise.
715 */
716 int
717 strsuftoi(arg)
718 const char *arg;
719 {
720 char *cp;
721 long val;
722
723 if (!isdigit((unsigned char)arg[0]))
724 return (-1);
725
726 val = strtol(arg, &cp, 10);
727 if (cp != NULL) {
728 if (cp[0] != '\0' && cp[1] != '\0')
729 return (-1);
730 switch (tolower((unsigned char)cp[0])) {
731 case '\0':
732 case 'b':
733 break;
734 case 'k':
735 val <<= 10;
736 break;
737 case 'm':
738 val <<= 20;
739 break;
740 case 'g':
741 val <<= 30;
742 break;
743 default:
744 return (-1);
745 }
746 }
747 if (val < 0 || val > INT_MAX)
748 return (-1);
749
750 return (val);
751 }
752
753 /*
754 * Count the number of current connections, reading from
755 * /var/run/ftpd.pids-<class>
756 * Does a kill -0 on each pid in that file, and only counts
757 * processes that exist (or frees the slot if it doesn't).
758 * Adds getpid() to the first free slot. Truncates the file
759 * if possible.
760 */
761 void
762 count_users()
763 {
764 char fn[MAXPATHLEN];
765 int fd, i, last;
766 size_t count;
767 pid_t *pids, mypid;
768 struct stat sb;
769
770 (void)strlcpy(fn, _PATH_CLASSPIDS, sizeof(fn));
771 (void)strlcat(fn, curclass.classname, sizeof(fn));
772 pids = NULL;
773 connections = 1;
774
775 if ((fd = open(fn, O_RDWR | O_CREAT | O_EXLOCK, 0600)) == -1)
776 return;
777 if (fstat(fd, &sb) == -1)
778 goto cleanup_count;
779 if ((pids = malloc(sb.st_size + sizeof(pid_t))) == NULL)
780 goto cleanup_count;
781 count = read(fd, pids, sb.st_size);
782 if (count < 0 || count != sb.st_size)
783 goto cleanup_count;
784 count /= sizeof(pid_t);
785 mypid = getpid();
786 last = 0;
787 for (i = 0; i < count; i++) {
788 if (pids[i] == 0)
789 continue;
790 if (kill(pids[i], 0) == -1 && errno != EPERM) {
791 if (mypid != 0) {
792 pids[i] = mypid;
793 mypid = 0;
794 last = i;
795 }
796 } else {
797 connections++;
798 last = i;
799 }
800 }
801 if (mypid != 0) {
802 if (pids[last] != 0)
803 last++;
804 pids[last] = mypid;
805 }
806 count = (last + 1) * sizeof(pid_t);
807 if (lseek(fd, 0, SEEK_SET) == -1)
808 goto cleanup_count;
809 if (write(fd, pids, count) == -1)
810 goto cleanup_count;
811 (void)ftruncate(fd, count);
812
813 cleanup_count:
814 (void)flock(fd, LOCK_UN);
815 close(fd);
816 REASSIGN(pids, NULL);
817 }
818