4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2014, 2015 Shruti V Sampat <shrutisampat@gmail.com>
23 * Copyright (c) 2016 by Delphix. All rights reserved.
27 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
32 /* All Rights Reserved */
35 * Portions of such source code were derived from Berkeley 4.3 BSD
36 * under license from the Regents of the University of California.
42 * This program receives requests from pututxline(3)
43 * via a named pipe to watch the process to make sure it cleans up
44 * its utmpx entry on termination.
45 * The program keeps a list of procs
46 * and uses poll() on their /proc files to detect termination.
47 * Also the program periodically scans the /etc/utmpx file for
48 * processes that aren't in the table so they can be watched.
50 * If utmpd doesn't hear back over the pipe from pututline(3) that
51 * the process has removed its entry it cleans the entry when the
52 * the process terminates.
53 * The AT&T Copyright above is there since we borrowed the pipe
54 * mechanism from init(1m).
58 #include <sys/types.h>
61 #include <stdio_ext.h>
66 #include <sys/termios.h>
70 #include <sys/statvfs.h>
73 #include <sys/stropts.h>
81 #include <sys/resource.h>
84 #define dprintf(x) if (Debug) (void) printf x
87 * Memory allocation keyed off MAX_FDS
89 #define MAX_FDS 4064 /* Maximum # file descriptors */
90 #define EXTRA_MARGIN 32 /* Allocate this many more FDS over Max_Fds */
92 * MAX_POLLNV & RESETS - paranoia to cover an error case that might not exist
94 #define MAX_POLL_ERRS 1024 /* Count of bad errors */
95 #define MAX_RESETS 1024 /* Maximum times to reload tables */
96 #define POLL_TIMEOUT 300 /* Default Timeout for poll() in seconds */
97 #define CLEANIT 1 /* Used by rem_pid() */
98 #define DONT_CLEAN 0 /* Used by rem_pid() */
99 #define UTMP_DEFAULT "/etc/default/utmpd"
100 #define WARN_TIME 3600 /* seconds between utmp checks */
101 #define WTMPX_UFREQ 60 /* seconds between updating WTMPX's atime */
105 * The pidrec structure describes the data shipped down the pipe to
106 * us from the pututxline() library in
107 * lib/libc/port/gen/getutx.c
117 int pd_type
; /* Command type */
118 pid_t pd_pid
; /* pid to add or remove */
123 * Since this program uses poll(2) and poll takes an array of file descriptors
124 * as an argument we maintain our data in tables.
125 * One table is the file descriptor array for poll, another parallel
126 * array is a table which contains the process ID of the corresponding
127 * open fd. These tables are kept sorted by process ID for quick lookups.
131 pid_t pl_pid
; /* pid to watch for */
132 int pl_status
; /* Exit status of proc */
135 static struct pidentry
*pidtable
= NULL
;
137 static pollfd_t
*fdtable
= NULL
;
139 static int pidcnt
= 0; /* Number of procs being watched */
140 static char *prog_name
; /* To save the invocation name away */
141 static char *UTMPPIPE_DIR
= "/var/run";
142 static char *UTMPPIPE
= "/var/run/utmppipe";
143 static int Pfd
= -1; /* File descriptor of named pipe */
144 static int Poll_timeout
= POLL_TIMEOUT
;
145 static int WTMPXfd
= -1; /* File descriptor of WTMPX_FILE */
146 static int WTMPX_ufreq
= WTMPX_UFREQ
;
147 static int Debug
= 0; /* Set by command line argument */
148 static int Max_fds
= MAX_FDS
;
151 * This program has three main components plus utilities and debug routines
152 * Receiver - receives the process ID or process for us to watch.
153 * (Uses a named pipe to get messages)
154 * Watcher - Use poll(2) to watch for processes to die so they
155 * can be cleaned up (get marked as DEAD_PROCESS)
156 * Scanner - periodically scans the utmpx file for stale entries
157 * or live entries that we don't know about.
160 static int wait_for_pids(); /* Watcher - uses poll */
161 static void scan_utmps(); /* Scanner, reads utmpx file */
162 static void drain_pipe(); /* Receiver - reads mesgs over UTMPPIPE */
163 static void setup_pipe(); /* For setting up receiver */
165 static void add_pid(); /* Adds a process to the table */
166 static void rem_pid(); /* Removes a process from the table */
167 static int find_pid(); /* Finds a process in the table */
168 static int proc_to_fd(); /* Takes a pid and returns an fd for its proc */
169 static void load_tables(); /* Loads up the tables the first time around */
170 static int pidcmp(); /* For sorting pids */
172 static void clean_entry(); /* Removes entry from our table and calls ... */
173 static void clean_utmpx_ent(); /* Cleans a utmpx entry */
175 static void fatal() __NORETURN
; /* Prints error message and calls exit */
176 static void nonfatal(); /* Prints error message */
177 static void print_tables(); /* Prints out internal tables for Debug */
178 static int proc_is_alive(pid_t pid
); /* Check if a process is alive */
179 static void warn_utmp(void);
181 /* Validate defaults from file and assign */
182 static int validate_default(char *defp
, int *flag
);
185 * main() - Main does basic setup and calls wait_for_pids() to do the work
189 main(int argc
, char *argv
[])
197 prog_name
= argv
[0]; /* Save invocation name */
200 (void) fprintf(stderr
,
201 "You must be root to run this program\n");
202 fatal("You must be root to run this program");
206 if ((argc
== 2 && (int)strlen(argv
[1]) >= 2) &&
207 (argv
[1][0] == '-' && argv
[1][1] == 'd')) {
210 (void) fprintf(stderr
,
211 "%s: Wrong number of arguments\n", prog_name
);
212 (void) fprintf(stderr
,
213 "Usage: %s [-debug]\n", prog_name
);
219 * Read defaults file for poll timeout, WTMPX update frequency
220 * and maximum number of processes to monitor.
222 if (defopen(UTMP_DEFAULT
) == 0) {
223 if ((defp
= defread("SCAN_PERIOD=")) != NULL
)
224 if (validate_default(defp
, &Poll_timeout
) == -1) {
225 (void) snprintf(msg
, sizeof (msg
), "SCAN_PERIOD"
226 " should be a positive integer, found %s",
230 dprintf(("Poll timeout set to %d\n", Poll_timeout
));
232 if ((defp
= defread("WTMPX_UPDATE_FREQ=")) != NULL
)
233 if (validate_default(defp
, &WTMPX_ufreq
) == -1) {
234 (void) snprintf(msg
, sizeof (msg
),
235 "WTMPX_UPDATE_FREQ should be a positive "
236 "integer, found %s", defp
);
239 dprintf(("WTMPX update frequency set to %d\n", WTMPX_ufreq
));
242 * Paranoia - if polling on large number of FDs is expensive /
243 * buggy the number can be set lower in the field.
245 if ((defp
= defread("MAX_FDS=")) != NULL
)
246 if (validate_default(defp
, &Max_fds
) == -1) {
247 (void) snprintf(msg
, sizeof (msg
), "MAX_FDS "
248 "should be a positive integer, found %s",
252 dprintf(("Max fds set to %d\n", Max_fds
));
253 (void) defopen(NULL
);
258 * Daemonize ourselves
267 * We open these to avoid accidentally writing to a proc file
269 (void) open("/dev/null", O_RDONLY
);
270 (void) open("/dev/null", O_WRONLY
);
271 (void) open("/dev/null", O_WRONLY
);
272 (void) setsid(); /* release process from tty */
275 openlog(prog_name
, LOG_PID
, LOG_DAEMON
); /* For error messages */
276 warn_utmp(); /* check to see if utmp came back by accident */
279 * Allocate the pidtable and fdtable. An earlier version did
280 * this as we go, but this is simpler.
282 if ((pidtable
= malloc(Max_fds
* sizeof (struct pidentry
))) == NULL
)
283 fatal("Malloc failed");
284 if ((fdtable
= malloc(Max_fds
* sizeof (pollfd_t
))) == NULL
)
285 fatal("Malloc failed");
288 * Up the limit on FDs
290 if (getrlimit(RLIMIT_NOFILE
, &rlim
) == 0) {
291 rlim
.rlim_cur
= Max_fds
+ EXTRA_MARGIN
+ 1;
292 rlim
.rlim_max
= Max_fds
+ EXTRA_MARGIN
+ 1;
293 if (setrlimit(RLIMIT_NOFILE
, &rlim
) != 0) {
294 fatal("Out of File Descriptors");
297 fatal("getrlimit returned failure");
299 (void) enable_extended_FILE_stdio(-1, -1);
301 if ((WTMPXfd
= open(WTMPX_FILE
, O_RDONLY
)) < 0)
302 nonfatal("WARNING: unable to open " WTMPX_FILE
" for update.");
305 * Loop here scanning the utmpx file and waiting for processes
306 * to terminate. Most of the activity is directed out of wait_for_pids.
307 * If wait_for_pids fails we reload the table and try again.
310 curtime
= time(NULL
);
311 dprintf(("utmp warning timer set to %d seconds\n", WARN_TIME
));
313 for (i
= 0; i
< MAX_RESETS
; i
++) {
315 while (wait_for_pids() == 1) {
317 if ((now
- curtime
) >= WARN_TIME
) {
318 dprintf(("utmp warning timer expired\n"));
325 (void) close(WTMPXfd
);
328 * We only get here if we had a bunch of resets - so give up
330 fatal("Too many resets, giving up");
335 * load_tables() - Designed to be called repeatedly if we need to
336 * restart things. Zeros the pidcount, and loads
337 * the tables by scanning utmpx
345 dprintf(("Load tables\n"));
348 * Close any open files.
350 for (i
= 0; i
< pidcnt
; i
++)
351 (void) close(fdtable
[i
].fd
);
355 setup_pipe(); /* Setup the pipe to receive messages */
356 scan_utmps(); /* Read in USER procs entries to watch */
361 * *** The Watcher ***
363 * Wait_for_pids - wait for the termination of a process in the table.
364 * Returns 1 on normal exist, 0 on failure.
370 register struct pollfd
*pfd
;
375 static time_t last_timeout
= 0;
376 static int bad_error
= 0; /* Count of POLL errors */
379 * First time through we initialize last_timeout to now.
381 if (last_timeout
== 0)
382 last_timeout
= time(NULL
);
385 * Recalculate timeout - checking to see if time expired.
388 if ((timeout
= Poll_timeout
- (time(NULL
) - last_timeout
)) <= 0) {
389 timeout
= Poll_timeout
;
390 last_timeout
= time(NULL
);
394 fdtable
[0].events
= POLLRDNORM
;
396 for (i
= 0; i
< (timeout
/ WTMPX_ufreq
); i
++) {
399 * Loop here while getting EAGAIN
402 while ((ret_val
= poll(fdtable
, pidcnt
, WTMPX_ufreq
*1000)) < 0)
408 * The results of pread(2) are discarded; we only want
409 * to update the access time of WTMPX_FILE.
410 * Periodically touching WTMPX helps determine when the
411 * OS became unavailable when the OS boots again .
412 * See PSARC 2004/462 for more information.
415 (void) pread(WTMPXfd
, (void *)&pid
, sizeof (pid
), 0);
417 if (ret_val
) /* file descriptor(s) need attention */
422 * If ret_val == 0 the poll timed out - reset last_time and
426 last_timeout
= time(NULL
);
432 * Check the pipe file descriptor
434 if (fdtable
[0].revents
& POLLRDNORM
) {
436 fdtable
[0].revents
= 0;
440 (void) sleep(5); /* Give parents time to cleanup children */
443 * We got here because the status of one of the pids that
444 * we are polling on has changed, so search the table looking
447 * The table is scanned backwards so that entries can be removed
448 * while we go since the table is compacted from high down to low
450 for (i
= pidcnt
- 1; i
> 0; i
--) {
452 * Break out of the loop if we've processed all the entries.
460 rem_pid((pid_t
)0, i
, DONT_CLEAN
);
464 * POLLHUP - Process terminated
466 if (pfd
->revents
& POLLHUP
) {
469 if (pread(pfd
->fd
, &psinfo
, sizeof (psinfo
), (off_t
)0)
470 != sizeof (psinfo
)) {
471 dprintf(("! %d: terminated, status 0x%.4x\n", \
472 (int)pidtable
[i
].pl_pid
, psinfo
.pr_wstat
));
473 pidtable
[i
].pl_status
= psinfo
.pr_wstat
;
476 dprintf(("! %d: terminated\n", \
477 (int)pidtable
[i
].pl_pid
));
478 pidtable
[i
].pl_status
= 0;
481 * PID gets removed when terminated only
483 rem_pid((pid_t
)0, i
, CLEANIT
);
488 * POLLNVAL and POLLERR
489 * These error's shouldn't occurr but until their fixed
490 * we perform some simple error recovery.
492 if (pfd
->revents
& (POLLNVAL
|POLLERR
)) {
493 dprintf(("Poll Err = %d pid = %d i = %d\n", \
494 pfd
->revents
, (int)pidtable
[i
].pl_pid
, i
));
496 pid
= pidtable
[i
].pl_pid
; /* Save pid for below */
498 * If its POLLNVAL we just remove the process for
499 * now, it will get picked up in the next scan.
500 * POLLERR pids get re-added after being deleted.
502 if (pfd
->revents
& POLLNVAL
) {
503 rem_pid((pid_t
)0, i
, DONT_CLEAN
);
504 } else { /* Else... POLLERR */
505 rem_pid((pid_t
)0, i
, DONT_CLEAN
);
509 if (bad_error
++ > MAX_POLL_ERRS
) {
511 return (0); /* 0 Indicates severe error */
518 * No more bits should be set in revents but check anyway
520 if (pfd
->revents
!= 0) {
521 dprintf(("%d: unknown err %d\n", \
522 (int)pidtable
[i
].pl_pid
, pfd
->revents
));
524 rem_pid((pid_t
)0, i
, DONT_CLEAN
);
527 if (bad_error
++ > MAX_POLL_ERRS
) {
529 return (0); /* 0 Indicates severe error */
534 return (1); /* 1 Indicates Everything okay */
538 * *** The Scanner ***
540 * scan_utmps() - Scan the utmpx file.
541 * For each USER_PROCESS check
542 * if its alive or dead. If alive and its not in
543 * our table to be watched, put it there. If its
544 * dead, remove it from our table and clean it up.
553 dprintf(("Scan utmps\n"));
558 while ((utmpx
= getutxent()) != NULL
) {
559 if (utmpx
->ut_type
== USER_PROCESS
) {
561 * Is the process alive?
563 if (proc_is_alive(utmpx
->ut_pid
)) {
565 * Yes, the process is alive, so add it if we
566 * don't have it in our table.
568 if (find_pid(utmpx
->ut_pid
, &i
) == 0)
569 add_pid(utmpx
->ut_pid
); /* No, add it */
572 * No, the process is dead, so remove it if its
573 * in our table, otherwise just clean it.
575 if (find_pid(utmpx
->ut_pid
, &i
) == 1)
576 rem_pid(utmpx
->ut_pid
, i
, CLEANIT
);
578 clean_utmpx_ent(utmpx
);
583 * Close it to flush the buffer.
590 * *** Receiver Routines ***
594 * setup_pipe - Set up the pipe to read pids over
601 struct statvfs statvfs_buf
;
603 * This code & comments swiped from init and left stock since it works
607 if ((statvfs(UTMPPIPE_DIR
, &statvfs_buf
) == 0) &&
608 ((statvfs_buf
.f_flag
& ST_RDONLY
) == 0)) {
609 (void) unlink(UTMPPIPE
);
610 (void) mknod(UTMPPIPE
, S_IFIFO
| 0600, 0);
612 Pfd
= open(UTMPPIPE
, O_RDWR
| O_NDELAY
);
617 * This code from init modified to be poll based instead of SIGPOLL,
623 * Read pipe in message discard mode. When read reads a
624 * pidrec size record, the remainder of the message will
625 * be discarded. Though there shouldn't be any it will
626 * help resynch if someone else wrote some garbage.
628 (void) ioctl(Pfd
, I_SRDOPT
, RMSGD
);
632 * My code. We use slot 0 in the table to hold the fd of the pipe
634 add_pid(0); /* Proc 0 guaranteed to get slot 0 */
635 fdtable
[0].fd
= Pfd
; /* Pfd could be -1, should be okay */
636 fdtable
[0].events
= POLLRDNORM
;
640 * drain_pipe() - The receiver routine that reads the pipe
647 register struct pidrec
*p
= &prec
;
653 * Important Note: Either read will really fail (in which case
654 * return is all we can do) or will get EAGAIN (Pfd was opened
655 * O_NDELAY), in which case we also want to return.
658 if ((bytes_read
= read(Pfd
, p
, sizeof (struct pidrec
))) !=
659 sizeof (struct pidrec
)) {
661 * Something went wrong reading, so read until pipe
665 while (read(Pfd
, p
, sizeof (struct pidrec
)) > 0)
670 dprintf(("drain_pipe: Recd command %d, pid %d\n",
671 p
->pd_type
, (int)p
->pd_pid
));
672 switch (p
->pd_type
) {
675 * Check if we already have the process, adding it
678 if (find_pid(p
->pd_pid
, &i
) == 0)
683 rem_pid(p
->pd_pid
, -1, DONT_CLEAN
);
686 nonfatal("Bad message on utmppipe\n");
694 * *** Utilities for add and removing entries in the tables ***
698 * add_pid - add a pid to the fd table and the pidtable.
699 * these tables are sorted tables for quick lookups.
708 static int first_time
= 1;
711 * Check to see if the pid is already in our table, or being passed
714 if (pidcnt
!= 0 && (find_pid(pid
, &j
) == 1 || pid
== 0))
717 if (pidcnt
>= Max_fds
) {
718 if (first_time
== 1) {
720 * Print this error only once
722 nonfatal("File Descriptor limit exceeded");
728 * Open the /proc file checking if there's still a valid proc file.
730 if (pid
!= 0 && (fd
= proc_to_fd(pid
)) == -1) {
732 * No, so the process died before we got to watch for it.
738 * We only do this code if we're not putting in the first element
739 * Which we know will be for proc zero which is used by setup_pipe
743 for (i
= 0; i
< pidcnt
; i
++) {
744 if (pid
<= pidtable
[i
].pl_pid
)
749 * Handle the case where we're not sticking our entry on the
750 * the end, or overwriting an existing entry.
752 if (i
!= pidcnt
&& pid
!= pidtable
[i
].pl_pid
) {
754 move_amt
= pidcnt
- i
;
759 (void) memmove(&pidtable
[i
+1], &pidtable
[i
],
760 move_amt
* sizeof (struct pidentry
));
761 (void) memmove(&fdtable
[i
+1], &fdtable
[i
],
762 move_amt
* sizeof (pollfd_t
));
768 * Fill in the events field for poll and copy the entry into the array
770 fdtable
[i
].events
= 0;
771 fdtable
[i
].revents
= 0;
775 * Likewise, setup pid field and pointer (index) to the fdtable entry
777 pidtable
[i
].pl_pid
= pid
;
779 pidcnt
++; /* Bump the pid count */
780 dprintf((" add_pid: pid = %d fd = %d index = %d pidcnt = %d\n",
781 (int)pid
, fd
, i
, pidcnt
));
786 * rem_pid - Remove an entry from the table and check to see if its
787 * not in the utmpx file.
788 * If i != -1 don't look up the pid, use i as index
790 * pid - Pid of process to clean or 0 if we don't know it
792 * i - Index into table or -1 if we need to look it up
794 * clean_it - Clean the entry, or just remove from table?
798 rem_pid(pid_t pid
, int i
, int clean_it
)
802 dprintf((" rem_pid: pid = %d i = %d", (int)pid
, i
));
805 * Don't allow slot 0 in the table to be removed - utmppipe fd
807 if ((i
== -1 && pid
== 0) || (i
== 0)) {
808 dprintf((" - attempted to remove proc 0\n"));
812 if (i
!= -1 || find_pid(pid
, &i
) == 1) { /* Found the entry */
813 (void) close(fdtable
[i
].fd
); /* We're done with the fd */
815 dprintf((" fd = %d\n", fdtable
[i
].fd
));
817 if (clean_it
== CLEANIT
)
820 move_amt
= (pidcnt
- i
) - 1;
822 * Remove entries from the tables.
824 (void) memmove(&pidtable
[i
], &pidtable
[i
+1],
825 move_amt
* sizeof (struct pidentry
));
827 (void) memmove(&fdtable
[i
], &fdtable
[i
+1],
828 move_amt
* sizeof (pollfd_t
));
831 * decrement the pid count - one less pid to worry about
836 dprintf((" - entry not found \n"));
841 * find_pid - Returns an index into the pidtable of the specifed pid,
842 * else -1 if not found
846 find_pid(pid_t pid
, int *i
)
852 p
= bsearch(&pe
, pidtable
, pidcnt
, sizeof (struct pidentry
), pidcmp
);
857 *i
= p
- (struct pidentry
*)pidtable
;
864 * Pidcmp - Used by besearch for sorting and finding process IDs.
868 pidcmp(struct pidentry
*a
, struct pidentry
*b
)
870 if (b
== NULL
|| a
== NULL
)
872 return (a
->pl_pid
- b
->pl_pid
);
877 * proc_to_fd - Take a process ID and return an open file descriptor to the
878 * /proc file for the specified process.
881 proc_to_fd(pid_t pid
)
886 (void) sprintf(procname
, "/proc/%d/psinfo", (int)pid
);
888 if ((fd
= open(procname
, O_RDONLY
)) >= 0) {
890 * dup the fd above the low order values to assure
891 * stdio works for other fds - paranoia.
893 if (fd
< EXTRA_MARGIN
) {
894 dfd
= fcntl(fd
, F_DUPFD
, EXTRA_MARGIN
);
901 * More paranoia - set the close on exec flag
903 (void) fcntl(fd
, F_SETFD
, 1);
909 if (errno
== EMFILE
) {
911 * This is fatal, since libc won't be able to allocate
912 * any fds for the pututxline() routines
914 fatal("Out of file descriptors");
916 fatal(procname
); /* Only get here on error */
922 * *** Utmpx Cleaning Utilities ***
926 * Clean_entry - Cleans the specified entry - where i is an index
927 * into the pid_table.
937 dprintf((" Cleaning %d\n", (int)pidtable
[i
].pl_pid
));
940 * Double check if the process is dead.
942 if (proc_is_alive(pidtable
[i
].pl_pid
)) {
943 dprintf((" Bad attempt to clean %d\n",
944 (int)pidtable
[i
].pl_pid
));
949 * Find the entry that corresponds to this pid.
950 * Do nothing if entry not found in utmpx file.
953 while ((u
= getutxent()) != NULL
) {
954 if (u
->ut_pid
== pidtable
[i
].pl_pid
) {
955 if (u
->ut_type
== USER_PROCESS
) {
965 * clean_utmpx_ent - Clean a utmpx entry
969 clean_utmpx_ent(struct utmpx
*u
)
971 dprintf((" clean_utmpx_ent: %d\n", (int)u
->ut_pid
));
972 u
->ut_type
= DEAD_PROCESS
;
973 (void) time(&u
->ut_xtime
);
974 (void) pututxline(u
);
975 updwtmpx(WTMPX_FILE
, u
);
977 * XXX update wtmp for ! nonuserx entries?
982 * *** Error Handling and Debugging Routines ***
986 * fatal - Catastrophic failure
994 syslog(LOG_ALERT
, "%s", str
);
996 if ((errno
= oerrno
) != 0)
998 dprintf(("%s\n", str
));
1004 * nonfatal - Non-Catastrophic failure - print message and errno
1010 syslog(LOG_WARNING
, "%s", str
);
1015 dprintf(("%c%s\n", 7, str
));
1017 (void) sleep(5); /* Time to read debug messages */
1022 * print_tables - Print internal tables - for debugging
1033 dprintf(("pidtable: "));
1034 for (i
= 0; i
< pidcnt
; i
++)
1035 dprintf(("%d: %d ", i
, (int)pidtable
[i
].pl_pid
));
1037 dprintf(("fdtable: "));
1038 for (i
= 0; i
< pidcnt
; i
++)
1039 dprintf(("%d: %d ", i
, fdtable
[i
].fd
));
1044 * proc_is_alive - Check to see if a process is alive AND its
1045 * not a zombie. Returns 1 if process is alive
1046 * and zero if it is dead or a zombie.
1050 proc_is_alive(pid_t pid
)
1052 char psinfoname
[64];
1056 if (kill(pid
, 0) != 0)
1057 return (0); /* Kill failed - no process */
1060 * The process exists, so check if it's a zombie.
1062 (void) sprintf(psinfoname
, "/proc/%d/psinfo", (int)pid
);
1064 if ((fd
= open(psinfoname
, O_RDONLY
)) < 0 ||
1065 read(fd
, &psinfo
, sizeof (psinfo
)) != sizeof (psinfo
)) {
1067 * We either couldn't open the proc, or we did but the
1068 * read of the psinfo file failed, so pid is nonexistent.
1075 /* if pr_nlwp == 0, process is a zombie */
1076 return (psinfo
.pr_nlwp
!= 0);
1080 * warn_utmp - /var/adm/utmp has been deprecated. It should no longer
1081 * be used. Applications that try to directly manipulate
1082 * it may cause problems. Since the file is no longer
1083 * shipped, if it appears on a system it's because an
1084 * old application created it. We'll have utmpd
1085 * complain about it periodically.
1093 if (lstat(UTMP_FILE
, &s
) == 0 &&
1094 s
.st_size
% sizeof (struct utmp
) == 0) {
1095 nonfatal("WARNING: /var/adm/utmp exists!\nSee "
1096 "utmp(4) for more information");
1101 * validate_default - validate and assign defaults.
1105 validate_default(char *defp
, int *flag
)
1111 lval
= strtol(defp
, &endptr
, 10);
1113 if (errno
!= 0 || lval
> INT_MAX
|| lval
<= 0)
1116 while (isspace(*endptr
) != 0)
1119 if (*endptr
!= '\0')