1 /* $Header: /p/tcsh/cvsroot/tcsh/tc.who.c,v 3.57 2012/01/17 20:53:38 christos Exp $ */
3 * tc.who.c: Watch logins and logouts...
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 RCSID("$tcsh: tc.who.c,v 3.57 2012/01/17 20:53:38 christos Exp $")
41 * kfk 26 Jan 1984 - for login watch functions.
47 # define UTNAMLEN sizeof(((struct utmpx *) 0)->ut_name)
48 # define UTLINLEN sizeof(((struct utmpx *) 0)->ut_line)
49 # ifdef HAVE_STRUCT_UTMPX_UT_HOST
50 # define UTHOSTLEN sizeof(((struct utmpx *) 0)->ut_host)
52 /* I just redefine a few words here. Changing every occurrence below
53 * seems like too much of work. All UTMP functions have equivalent
54 * UTMPX counterparts, so they can be added all here when needed.
55 * Kimmo Suominen, Oct 14 1991
57 # if defined(__UTMPX_FILE) && !defined(UTMPX_FILE)
58 # define TCSH_PATH_UTMP __UTMPX_FILE
59 # elif defined(_PATH_UTMPX)
60 # define TCSH_PATH_UTMP _PATH_UTMPX
61 # elif defined(UTMPX_FILE)
62 # define TCSH_PATH_UTMP UTMPX_FILE
63 # elif __FreeBSD_version >= 900000
64 # /* Why isn't this defined somewhere? */
65 # define TCSH_PATH_UTMP "/var/run/utx.active"
66 # elif defined(__hpux)
67 # define TCSH_PATH_UTMP "/etc/utmpx"
69 # if defined(TCSH_PATH_UTMP) || !defined(HAVE_UTMP_H)
71 # define TCSH_USE_UTMPX
72 # if defined(HAVE_GETUTENT) || defined(HAVE_GETUTXENT)
73 # define getutent getutxent
74 # define setutent setutxent
75 # define endutent endutxent
76 # endif /* HAVE_GETUTENT || HAVE_GETUTXENT */
77 # if defined(HAVE_STRUCT_UTMPX_UT_TV)
78 # define ut_time ut_tv.tv_sec
79 # elif defined(HAVE_STRUCT_UTMPX_UT_XTIME)
80 # define ut_time ut_xtime
82 # if defined(HAVE_STRUCT_UTMPX_UT_USER)
83 # define ut_name ut_user
85 # endif /* TCSH_PATH_UTMP || !HAVE_UTMP_H */
86 #endif /* HAVE_UTMPX_H */
88 #if !defined(TCSH_USE_UTMPX) && defined(HAVE_UTMP_H)
90 # if defined(HAVE_STRUCT_UTMP_UT_TV)
91 # define ut_time ut_tv.tv_sec
92 # elif defined(HAVE_STRUCT_UTMP_UT_XTIME)
93 # define ut_time ut_xtime
95 # if defined(HAVE_STRUCT_UTMP_UT_USER)
96 # define ut_name ut_user
99 # define UTNAMLEN sizeof(((struct utmp *) 0)->ut_name)
100 # define UTLINLEN sizeof(((struct utmp *) 0)->ut_line)
101 # ifdef HAVE_STRUCT_UTMP_UT_HOST
103 # define UTHOSTLEN 100
105 # define UTHOSTLEN sizeof(((struct utmp *) 0)->ut_host)
107 # endif /* HAVE_STRUCT_UTMP_UT_HOST */
109 /* give poor cc a little help if it needs it */
111 # define UTNAMLEN sizeof(__ut.ut_name)
112 # define UTLINLEN sizeof(__ut.ut_line)
113 # ifdef HAVE_STRUCT_UTMP_UT_HOST
115 # define UTHOSTLEN 100
117 # define UTHOSTLEN sizeof(__ut.ut_host)
119 # endif /* HAVE_STRUCT_UTMP_UT_HOST */
120 # endif /* BROKEN_CC */
121 # ifndef TCSH_PATH_UTMP
123 # define TCSH_PATH_UTMP UTMP_FILE
124 # elif defined(_PATH_UTMP)
125 # define TCSH_PATH_UTMP _PATH_UTMP
127 # define TCSH_PATH_UTMP "/etc/utmp"
128 # endif /* UTMP_FILE */
129 # endif /* TCSH_PATH_UTMP */
130 #endif /* !TCSH_USE_UTMPX && HAVE_UTMP_H */
140 struct who *who_next;
141 struct who *who_prev;
142 char who_name[UTNAMLEN + 1];
143 char who_new[UTNAMLEN + 1];
144 char who_tty[UTLINLEN + 1];
146 char who_host[UTHOSTLEN + 1];
147 #endif /* UTHOSTLEN */
152 static struct who whohead, whotail;
153 static time_t watch_period = 0;
154 static time_t stlast = 0;
156 static void debugwholist (struct who *, struct who *);
158 static void print_who (struct who *);
169 * Karl Kleinpaste, 26 Jan 1984.
170 * Initialize the dummy tty list for login watch.
171 * This dummy list eliminates boundary conditions
172 * when doing pointer-chase searches.
177 whohead.who_next = &whotail;
178 whotail.who_prev = &whohead;
181 debugwholist(NULL, NULL);
182 #endif /* WHODEBUG */
193 * Karl Kleinpaste, 26 Jan 1984.
194 * Watch /etc/utmp for login/logout changes.
197 watch_login(int force)
199 int comp = -1, alldone;
200 int firsttime = stlast == 1;
201 #if defined(HAVE_GETUTENT) || defined(HAVE_GETUTXENT)
207 struct who *wp, *wpnew;
210 time_t t, interval = MAILINTVL;
212 #if defined(HAVE_STRUCT_UTMP_UT_HOST) && defined(_SEQUENT_)
213 char *host, *ut_find_host();
216 static int ncbs_posted = 0;
221 #endif /* WINNT_NATIVE */
223 /* stop SIGINT, lest our login list get trashed. */
225 cleanup_push(&pintr_disabled, disabled_cleanup);
228 if ((v == NULL || v->vec == NULL) && !force) {
229 cleanup_until(&pintr_disabled);
230 return; /* no names to watch */
234 if (blklen(vp) % 2) /* odd # args: 1st == # minutes. */
235 interval = (number(*vp)) ? (getn(*vp++) * 60) : MAILINTVL;
243 * Since NCB_ASTATs take time, start em async at least 90 secs
244 * before we are due -amol 6/5/97
247 time_t tdiff = t - watch_period;
248 if (!watch_period || ((tdiff > 0) && (tdiff > (interval - 90)))) {
253 #endif /* WINNT_NATIVE */
254 if (t - watch_period < interval) {
255 cleanup_until(&pintr_disabled);
256 return; /* not long enough yet... */
261 #else /* !WINNT_NATIVE */
264 * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
265 * Don't open utmp all the time, stat it first...
267 if (stat(TCSH_PATH_UTMP, &sta)) {
270 "cannot stat %s. Please \"unset watch\".\n"),
272 cleanup_until(&pintr_disabled);
275 if (stlast == sta.st_mtime) {
276 cleanup_until(&pintr_disabled);
279 stlast = sta.st_mtime;
280 #if defined(HAVE_GETUTENT) || defined(HAVE_GETUTXENT)
283 if ((utmpfd = xopen(TCSH_PATH_UTMP, O_RDONLY|O_LARGEFILE)) < 0) {
286 "%s cannot be opened. Please \"unset watch\".\n"),
288 cleanup_until(&pintr_disabled);
291 cleanup_push(&utmpfd, open_cleanup);
295 * xterm clears the entire utmp entry - mark everyone on the status list
296 * OFFLINE or we won't notice X "logouts"
298 for (wp = whohead.who_next; wp->who_next != NULL; wp = wp->who_next)
299 wp->who_status = OFFLINE | CLEARED;
302 * Read in the utmp file, sort the entries, and update existing entries or
303 * add new entries to the status list.
305 #if defined(HAVE_GETUTENT) || defined(HAVE_GETUTXENT)
306 while ((uptr = getutent()) != NULL) {
307 memcpy(&utmp, uptr, sizeof (utmp));
309 while (xread(utmpfd, &utmp, sizeof utmp) == sizeof utmp) {
314 if (utmp.ut_type != USER_PROCESS)
317 /* Why is that? Cause the utmp file is always corrupted??? */
318 if (utmp.ut_type != USER_PROCESS && utmp.ut_type != DEAD_PROCESS)
321 # endif /* DEAD_PROCESS */
323 if (utmp.ut_name[0] == '\0' && utmp.ut_line[0] == '\0')
324 continue; /* completely void entry */
326 if (utmp.ut_type == DEAD_PROCESS && utmp.ut_line[0] == '\0')
328 # endif /* DEAD_PROCESS */
329 wp = whohead.who_next;
330 while (wp->who_next && (comp = strncmp(wp->who_tty, utmp.ut_line, UTLINLEN)) < 0)
331 wp = wp->who_next;/* find that tty! */
333 if (wp->who_next && comp == 0) { /* found the tty... */
334 if (utmp.ut_time < wp->who_time)
337 if (utmp.ut_type == DEAD_PROCESS) {
338 wp->who_time = utmp.ut_time;
339 wp->who_status = OFFLINE;
342 # endif /* DEAD_PROCESS */
343 if (utmp.ut_name[0] == '\0') {
344 wp->who_time = utmp.ut_time;
345 wp->who_status = OFFLINE;
347 else if (strncmp(utmp.ut_name, wp->who_name, UTNAMLEN) == 0) {
348 /* someone is logged in */
349 wp->who_time = utmp.ut_time;
350 wp->who_status = ONLINE | ANNOUNCE; /* same guy */
353 (void) strncpy(wp->who_new, utmp.ut_name, UTNAMLEN);
356 host = ut_find_host(wp->who_tty);
358 (void) strncpy(wp->who_host, host, UTHOSTLEN);
362 (void) strncpy(wp->who_host, utmp.ut_host, UTHOSTLEN);
364 # endif /* UTHOSTLEN */
365 wp->who_time = utmp.ut_time;
366 if (wp->who_name[0] == '\0')
367 wp->who_status = ONLINE;
369 wp->who_status = CHANGED;
372 else { /* new tty in utmp */
373 wpnew = xcalloc(1, sizeof *wpnew);
374 (void) strncpy(wpnew->who_tty, utmp.ut_line, UTLINLEN);
377 host = ut_find_host(wpnew->who_tty);
379 (void) strncpy(wpnew->who_host, host, UTHOSTLEN);
381 wpnew->who_host[0] = 0;
383 (void) strncpy(wpnew->who_host, utmp.ut_host, UTHOSTLEN);
385 # endif /* UTHOSTLEN */
386 wpnew->who_time = utmp.ut_time;
388 if (utmp.ut_type == DEAD_PROCESS)
389 wpnew->who_status = OFFLINE;
391 # endif /* DEAD_PROCESS */
392 if (utmp.ut_name[0] == '\0')
393 wpnew->who_status = OFFLINE;
395 (void) strncpy(wpnew->who_new, utmp.ut_name, UTNAMLEN);
396 wpnew->who_status = ONLINE;
399 debugwholist(wpnew, wp);
400 # endif /* WHODEBUG */
402 wpnew->who_next = wp; /* link in a new 'who' */
403 wpnew->who_prev = wp->who_prev;
404 wpnew->who_prev->who_next = wpnew;
405 wp->who_prev = wpnew; /* linked in now */
408 #if defined(HAVE_GETUTENT) || defined(HAVE_GETUTXENT)
411 cleanup_until(&utmpfd);
413 #endif /* !WINNT_NATIVE */
415 if (force || vp == NULL) {
416 cleanup_until(&pintr_disabled);
421 * The state of all logins is now known, so we can search the user's list
422 * of watchables to print the interesting ones.
424 for (alldone = 0; !alldone && *vp != NULL && **vp != '\0' &&
425 *(vp + 1) != NULL && **(vp + 1) != '\0';
426 vp += 2) { /* args used in pairs... */
428 if (eq(*vp, STRany) && eq(*(vp + 1), STRany))
431 for (wp = whohead.who_next; wp->who_next != NULL; wp = wp->who_next) {
432 if (wp->who_status & ANNOUNCE ||
433 (!eq(STRany, vp[0]) &&
434 !Gmatch(str2short(wp->who_name), vp[0]) &&
435 !Gmatch(str2short(wp->who_new), vp[0])) ||
436 (!Gmatch(str2short(wp->who_tty), vp[1]) &&
438 continue; /* entry doesn't qualify */
439 /* already printed or not right one to print */
442 if (wp->who_status & CLEARED) {/* utmp entry was cleared */
443 wp->who_time = watch_period;
444 wp->who_status &= ~CLEARED;
447 if ((wp->who_status & OFFLINE) &&
448 (wp->who_name[0] != '\0')) {
451 wp->who_name[0] = '\0';
452 wp->who_status |= ANNOUNCE;
455 if (wp->who_status & ONLINE) {
458 (void) strcpy(wp->who_name, wp->who_new);
459 wp->who_status |= ANNOUNCE;
462 if (wp->who_status & CHANGED) {
465 (void) strcpy(wp->who_name, wp->who_new);
466 wp->who_status |= ANNOUNCE;
471 cleanup_until(&pintr_disabled);
476 debugwholist(struct who *new, struct who *wp)
480 a = whohead.who_next;
481 while (a->who_next != NULL) {
482 xprintf("%s/%s -> ", a->who_name, a->who_tty);
487 xprintf(CGETS(26, 3, "BUG! last element is not whotail!\n"));
490 a = whotail.who_prev;
491 xprintf(CGETS(26, 4, "backward: "));
492 while (a->who_prev != NULL) {
493 xprintf("%s/%s -> ", a->who_name, a->who_tty);
498 xprintf(CGETS(26, 5, "BUG! first element is not whohead!\n"));
502 xprintf(CGETS(26, 6, "new: %s/%s\n"), new->who_name, new->who_tty);
504 xprintf("wp: %s/%s\n", wp->who_name, wp->who_tty);
506 #endif /* WHODEBUG */
510 print_who(struct who *wp)
513 Char *cp = str2short(CGETS(26, 7, "%n has %a %l from %m."));
515 Char *cp = str2short(CGETS(26, 8, "%n has %a %l."));
516 #endif /* UTHOSTLEN */
517 struct varent *vp = adrof(STRwho);
520 if (vp && vp->vec && vp->vec[0])
523 str = tprintf(FMT_WHO, cp, NULL, wp->who_time, wp);
524 cleanup_push(str, xfree);
529 } /* end print_who */
533 who_info(ptr_t ptr, int c)
535 struct who *wp = ptr;
541 #endif /* UTHOSTLEN */
544 case 'n': /* user name */
545 switch (wp->who_status & STMASK) {
548 return strsave(wp->who_new);
550 return strsave(wp->who_name);
557 switch (wp->who_status & STMASK) {
559 return strsave(CGETS(26, 9, "logged on"));
561 return strsave(CGETS(26, 10, "logged off"));
563 return xasprintf(CGETS(26, 11, "replaced %s on"), wp->who_name);
571 if (wp->who_host[0] == '\0')
572 return strsave(CGETS(26, 12, "local"));
575 wbuf = xmalloc(strlen(pb) + 1);
577 /* the ':' stuff is for <host>:<display>.<screen> */
578 for (flg = isdigit((unsigned char)*pb) ? '\0' : '.';
579 *pb != '\0' && (*pb != flg || ((pb = strchr(pb, ':')) != 0));
583 *wb++ = isupper((unsigned char)*pb) ?
584 tolower((unsigned char)*pb) : *pb;
591 if (wp->who_host[0] == '\0')
592 return strsave(CGETS(26, 12, "local"));
595 wbuf = xmalloc(strlen(pb) + 1);
597 for (; *pb != '\0'; pb++)
598 *wb++ = isupper((unsigned char)*pb) ?
599 tolower((unsigned char)*pb) : *pb;
603 #endif /* UTHOSTLEN */
606 return strsave(wp->who_tty);
620 dolog(Char **v, struct command *c)
627 vp = adrof(STRwatch); /* lint insists vp isn't used unless we */
628 if (vp == NULL) /* unless we assign it outside the if */
629 stderror(ERR_NOWATCH);
631 wp = whohead.who_next;
632 while (wp->who_next != NULL) {
633 wp->who_name[0] = '\0';
648 char *tty = short2str(varval(STRtty));
654 for (wp = whohead.who_next; wp->who_next != NULL; wp = wp->who_next) {
655 if (strcmp(tty, wp->who_tty) == 0)
657 wp->who_name[0] = '\0';
662 # endif /* UTHOSTLEN */
666 add_to_who_list(char *name, char *mach_nm)
669 struct who *wp, *wpnew;
672 wp = whohead.who_next;
673 while (wp->who_next && (comp = strncmp(wp->who_tty,mach_nm,UTLINLEN)) < 0)
674 wp = wp->who_next;/* find that tty! */
676 if (wp->who_next && comp == 0) { /* found the tty... */
680 wp->who_status = OFFLINE;
682 else if (strncmp(name, wp->who_name, UTNAMLEN) == 0) {
683 /* someone is logged in */
685 wp->who_status = 0; /* same guy */
688 (void) strncpy(wp->who_new, name, UTNAMLEN);
690 if (wp->who_name[0] == '\0')
691 wp->who_status = ONLINE;
693 wp->who_status = CHANGED;
697 wpnew = xcalloc(1, sizeof *wpnew);
698 (void) strncpy(wpnew->who_tty, mach_nm, UTLINLEN);
701 wpnew->who_status = OFFLINE;
703 (void) strncpy(wpnew->who_new, name, UTNAMLEN);
704 wpnew->who_status = ONLINE;
707 debugwholist(wpnew, wp);
708 #endif /* WHODEBUG */
710 wpnew->who_next = wp; /* link in a new 'who' */
711 wpnew->who_prev = wp->who_prev;
712 wpnew->who_prev->who_next = wpnew;
713 wp->who_prev = wpnew; /* linked in now */
716 #endif /* WINNT_NATIVE */
717 #endif /* HAVENOUTMP */