1 /* $Header: /p/tcsh/cvsroot/tcsh/tc.who.c,v 3.59 2012/11/15 02:55:08 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.59 2012/11/15 02:55:08 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"
68 # elif defined(IBMAIX) && defined(UTMP_FILE)
69 # define TCSH_PATH_UTMP UTMP_FILE
71 # if defined(TCSH_PATH_UTMP) || !defined(HAVE_UTMP_H)
73 # define TCSH_USE_UTMPX
74 # if defined(HAVE_GETUTENT) || defined(HAVE_GETUTXENT)
75 # define getutent getutxent
76 # define setutent setutxent
77 # define endutent endutxent
78 # endif /* HAVE_GETUTENT || HAVE_GETUTXENT */
79 # if defined(HAVE_STRUCT_UTMPX_UT_TV)
80 # define ut_time ut_tv.tv_sec
81 # elif defined(HAVE_STRUCT_UTMPX_UT_XTIME)
82 # define ut_time ut_xtime
84 # if defined(HAVE_STRUCT_UTMPX_UT_USER)
85 # define ut_name ut_user
87 # endif /* TCSH_PATH_UTMP || !HAVE_UTMP_H */
88 #endif /* HAVE_UTMPX_H */
90 #if !defined(TCSH_USE_UTMPX) && defined(HAVE_UTMP_H)
92 # if defined(HAVE_STRUCT_UTMP_UT_TV)
93 # define ut_time ut_tv.tv_sec
94 # elif defined(HAVE_STRUCT_UTMP_UT_XTIME)
95 # define ut_time ut_xtime
97 # if defined(HAVE_STRUCT_UTMP_UT_USER)
98 # define ut_name ut_user
101 # define UTNAMLEN sizeof(((struct utmp *) 0)->ut_name)
102 # define UTLINLEN sizeof(((struct utmp *) 0)->ut_line)
103 # ifdef HAVE_STRUCT_UTMP_UT_HOST
105 # define UTHOSTLEN 100
107 # define UTHOSTLEN sizeof(((struct utmp *) 0)->ut_host)
109 # endif /* HAVE_STRUCT_UTMP_UT_HOST */
111 /* give poor cc a little help if it needs it */
113 # define UTNAMLEN sizeof(__ut.ut_name)
114 # define UTLINLEN sizeof(__ut.ut_line)
115 # ifdef HAVE_STRUCT_UTMP_UT_HOST
117 # define UTHOSTLEN 100
119 # define UTHOSTLEN sizeof(__ut.ut_host)
121 # endif /* HAVE_STRUCT_UTMP_UT_HOST */
122 # endif /* BROKEN_CC */
123 # ifndef TCSH_PATH_UTMP
125 # define TCSH_PATH_UTMP UTMP_FILE
126 # elif defined(_PATH_UTMP)
127 # define TCSH_PATH_UTMP _PATH_UTMP
129 # define TCSH_PATH_UTMP "/etc/utmp"
130 # endif /* UTMP_FILE */
131 # endif /* TCSH_PATH_UTMP */
132 #endif /* !TCSH_USE_UTMPX && HAVE_UTMP_H */
142 struct who
*who_next
;
143 struct who
*who_prev
;
144 char who_name
[UTNAMLEN
+ 1];
145 char who_new
[UTNAMLEN
+ 1];
146 char who_tty
[UTLINLEN
+ 1];
148 char who_host
[UTHOSTLEN
+ 1];
149 #endif /* UTHOSTLEN */
154 static struct who whohead
, whotail
;
155 static time_t watch_period
= 0;
156 static time_t stlast
= 0;
158 static void debugwholist (struct who
*, struct who
*);
160 static void print_who (struct who
*);
171 * Karl Kleinpaste, 26 Jan 1984.
172 * Initialize the dummy tty list for login watch.
173 * This dummy list eliminates boundary conditions
174 * when doing pointer-chase searches.
179 whohead
.who_next
= &whotail
;
180 whotail
.who_prev
= &whohead
;
183 debugwholist(NULL
, NULL
);
184 #endif /* WHODEBUG */
195 * Karl Kleinpaste, 26 Jan 1984.
196 * Watch /etc/utmp for login/logout changes.
199 watch_login(int force
)
201 int comp
= -1, alldone
;
202 int firsttime
= stlast
== 1;
203 #if defined(HAVE_GETUTENT) || defined(HAVE_GETUTXENT)
209 struct who
*wp
, *wpnew
;
212 time_t t
, interval
= MAILINTVL
;
214 #if defined(HAVE_STRUCT_UTMP_UT_HOST) && defined(_SEQUENT_)
215 char *host
, *ut_find_host();
218 static int ncbs_posted
= 0;
223 #endif /* WINNT_NATIVE */
225 /* stop SIGINT, lest our login list get trashed. */
227 cleanup_push(&pintr_disabled
, disabled_cleanup
);
230 if ((v
== NULL
|| v
->vec
== NULL
) && !force
) {
231 cleanup_until(&pintr_disabled
);
232 return; /* no names to watch */
236 if (blklen(vp
) % 2) /* odd # args: 1st == # minutes. */
237 interval
= (number(*vp
)) ? (getn(*vp
++) * 60) : MAILINTVL
;
245 * Since NCB_ASTATs take time, start em async at least 90 secs
246 * before we are due -amol 6/5/97
249 time_t tdiff
= t
- watch_period
;
250 if (!watch_period
|| ((tdiff
> 0) && (tdiff
> (interval
- 90)))) {
255 #endif /* WINNT_NATIVE */
256 if (t
- watch_period
< interval
) {
257 cleanup_until(&pintr_disabled
);
258 return; /* not long enough yet... */
263 #else /* !WINNT_NATIVE */
266 * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
267 * Don't open utmp all the time, stat it first...
269 if (stat(TCSH_PATH_UTMP
, &sta
)) {
272 "cannot stat %s. Please \"unset watch\".\n"),
274 cleanup_until(&pintr_disabled
);
277 if (stlast
== sta
.st_mtime
) {
278 cleanup_until(&pintr_disabled
);
281 stlast
= sta
.st_mtime
;
282 #if defined(HAVE_GETUTENT) || defined(HAVE_GETUTXENT)
285 if ((utmpfd
= xopen(TCSH_PATH_UTMP
, O_RDONLY
|O_LARGEFILE
)) < 0) {
288 "%s cannot be opened. Please \"unset watch\".\n"),
290 cleanup_until(&pintr_disabled
);
293 cleanup_push(&utmpfd
, open_cleanup
);
297 * xterm clears the entire utmp entry - mark everyone on the status list
298 * OFFLINE or we won't notice X "logouts"
300 for (wp
= whohead
.who_next
; wp
->who_next
!= NULL
; wp
= wp
->who_next
)
301 wp
->who_status
= OFFLINE
| CLEARED
;
304 * Read in the utmp file, sort the entries, and update existing entries or
305 * add new entries to the status list.
307 #if defined(HAVE_GETUTENT) || defined(HAVE_GETUTXENT)
308 while ((uptr
= getutent()) != NULL
) {
309 memcpy(&utmp
, uptr
, sizeof (utmp
));
311 while (xread(utmpfd
, &utmp
, sizeof utmp
) == sizeof utmp
) {
316 if (utmp
.ut_type
!= USER_PROCESS
)
319 /* Why is that? Cause the utmp file is always corrupted??? */
320 if (utmp
.ut_type
!= USER_PROCESS
&& utmp
.ut_type
!= DEAD_PROCESS
)
323 # endif /* DEAD_PROCESS */
325 if (utmp
.ut_name
[0] == '\0' && utmp
.ut_line
[0] == '\0')
326 continue; /* completely void entry */
328 if (utmp
.ut_type
== DEAD_PROCESS
&& utmp
.ut_line
[0] == '\0')
330 # endif /* DEAD_PROCESS */
331 wp
= whohead
.who_next
;
332 while (wp
->who_next
&& (comp
= strncmp(wp
->who_tty
, utmp
.ut_line
, UTLINLEN
)) < 0)
333 wp
= wp
->who_next
;/* find that tty! */
335 if (wp
->who_next
&& comp
== 0) { /* found the tty... */
336 if (utmp
.ut_time
< wp
->who_time
)
339 if (utmp
.ut_type
== DEAD_PROCESS
) {
340 wp
->who_time
= utmp
.ut_time
;
341 wp
->who_status
= OFFLINE
;
344 # endif /* DEAD_PROCESS */
345 if (utmp
.ut_name
[0] == '\0') {
346 wp
->who_time
= utmp
.ut_time
;
347 wp
->who_status
= OFFLINE
;
349 else if (strncmp(utmp
.ut_name
, wp
->who_name
, UTNAMLEN
) == 0) {
350 /* someone is logged in */
351 wp
->who_time
= utmp
.ut_time
;
352 wp
->who_status
= ONLINE
| ANNOUNCE
; /* same guy */
355 (void) strncpy(wp
->who_new
, utmp
.ut_name
, UTNAMLEN
);
358 host
= ut_find_host(wp
->who_tty
);
360 (void) strncpy(wp
->who_host
, host
, UTHOSTLEN
);
364 (void) strncpy(wp
->who_host
, utmp
.ut_host
, UTHOSTLEN
);
366 # endif /* UTHOSTLEN */
367 wp
->who_time
= utmp
.ut_time
;
368 if (wp
->who_name
[0] == '\0')
369 wp
->who_status
= ONLINE
;
371 wp
->who_status
= CHANGED
;
374 else { /* new tty in utmp */
375 wpnew
= xcalloc(1, sizeof *wpnew
);
376 (void) strncpy(wpnew
->who_tty
, utmp
.ut_line
, UTLINLEN
);
379 host
= ut_find_host(wpnew
->who_tty
);
381 (void) strncpy(wpnew
->who_host
, host
, UTHOSTLEN
);
383 wpnew
->who_host
[0] = 0;
385 (void) strncpy(wpnew
->who_host
, utmp
.ut_host
, UTHOSTLEN
);
387 # endif /* UTHOSTLEN */
388 wpnew
->who_time
= utmp
.ut_time
;
390 if (utmp
.ut_type
== DEAD_PROCESS
)
391 wpnew
->who_status
= OFFLINE
;
393 # endif /* DEAD_PROCESS */
394 if (utmp
.ut_name
[0] == '\0')
395 wpnew
->who_status
= OFFLINE
;
397 (void) strncpy(wpnew
->who_new
, utmp
.ut_name
, UTNAMLEN
);
398 wpnew
->who_status
= ONLINE
;
401 debugwholist(wpnew
, wp
);
402 # endif /* WHODEBUG */
404 wpnew
->who_next
= wp
; /* link in a new 'who' */
405 wpnew
->who_prev
= wp
->who_prev
;
406 wpnew
->who_prev
->who_next
= wpnew
;
407 wp
->who_prev
= wpnew
; /* linked in now */
410 #if defined(HAVE_GETUTENT) || defined(HAVE_GETUTXENT)
413 cleanup_until(&utmpfd
);
415 #endif /* !WINNT_NATIVE */
417 if (force
|| vp
== NULL
) {
418 cleanup_until(&pintr_disabled
);
423 * The state of all logins is now known, so we can search the user's list
424 * of watchables to print the interesting ones.
426 for (alldone
= 0; !alldone
&& *vp
!= NULL
&& **vp
!= '\0' &&
427 *(vp
+ 1) != NULL
&& **(vp
+ 1) != '\0';
428 vp
+= 2) { /* args used in pairs... */
430 if (eq(*vp
, STRany
) && eq(*(vp
+ 1), STRany
))
433 for (wp
= whohead
.who_next
; wp
->who_next
!= NULL
; wp
= wp
->who_next
) {
434 if (wp
->who_status
& ANNOUNCE
||
435 (!eq(STRany
, vp
[0]) &&
436 !Gmatch(str2short(wp
->who_name
), vp
[0]) &&
437 !Gmatch(str2short(wp
->who_new
), vp
[0])) ||
438 (!Gmatch(str2short(wp
->who_tty
), vp
[1]) &&
440 continue; /* entry doesn't qualify */
441 /* already printed or not right one to print */
444 if (wp
->who_status
& CLEARED
) {/* utmp entry was cleared */
445 wp
->who_time
= watch_period
;
446 wp
->who_status
&= ~CLEARED
;
449 if ((wp
->who_status
& OFFLINE
) &&
450 (wp
->who_name
[0] != '\0')) {
453 wp
->who_name
[0] = '\0';
454 wp
->who_status
|= ANNOUNCE
;
457 if (wp
->who_status
& ONLINE
) {
460 (void) strcpy(wp
->who_name
, wp
->who_new
);
461 wp
->who_status
|= ANNOUNCE
;
464 if (wp
->who_status
& CHANGED
) {
467 (void) strcpy(wp
->who_name
, wp
->who_new
);
468 wp
->who_status
|= ANNOUNCE
;
473 cleanup_until(&pintr_disabled
);
478 debugwholist(struct who
*new, struct who
*wp
)
482 a
= whohead
.who_next
;
483 while (a
->who_next
!= NULL
) {
484 xprintf("%s/%s -> ", a
->who_name
, a
->who_tty
);
489 xprintf(CGETS(26, 3, "BUG! last element is not whotail!\n"));
492 a
= whotail
.who_prev
;
493 xprintf(CGETS(26, 4, "backward: "));
494 while (a
->who_prev
!= NULL
) {
495 xprintf("%s/%s -> ", a
->who_name
, a
->who_tty
);
500 xprintf(CGETS(26, 5, "BUG! first element is not whohead!\n"));
504 xprintf(CGETS(26, 6, "new: %s/%s\n"), new->who_name
, new->who_tty
);
506 xprintf("wp: %s/%s\n", wp
->who_name
, wp
->who_tty
);
508 #endif /* WHODEBUG */
512 print_who(struct who
*wp
)
515 Char
*cp
= str2short(CGETS(26, 7, "%n has %a %l from %m."));
517 Char
*cp
= str2short(CGETS(26, 8, "%n has %a %l."));
518 #endif /* UTHOSTLEN */
519 struct varent
*vp
= adrof(STRwho
);
522 if (vp
&& vp
->vec
&& vp
->vec
[0])
525 str
= tprintf(FMT_WHO
, cp
, NULL
, wp
->who_time
, wp
);
526 cleanup_push(str
, xfree
);
531 } /* end print_who */
535 who_info(ptr_t ptr
, int c
)
537 struct who
*wp
= ptr
;
543 #endif /* UTHOSTLEN */
546 case 'n': /* user name */
547 switch (wp
->who_status
& STMASK
) {
550 return strsave(wp
->who_new
);
552 return strsave(wp
->who_name
);
559 switch (wp
->who_status
& STMASK
) {
561 return strsave(CGETS(26, 9, "logged on"));
563 return strsave(CGETS(26, 10, "logged off"));
565 return xasprintf(CGETS(26, 11, "replaced %s on"), wp
->who_name
);
573 if (wp
->who_host
[0] == '\0')
574 return strsave(CGETS(26, 12, "local"));
577 wbuf
= xmalloc(strlen(pb
) + 1);
579 /* the ':' stuff is for <host>:<display>.<screen> */
580 for (flg
= isdigit((unsigned char)*pb
) ? '\0' : '.';
581 *pb
!= '\0' && (*pb
!= flg
|| ((pb
= strchr(pb
, ':')) != 0));
585 *wb
++ = isupper((unsigned char)*pb
) ?
586 tolower((unsigned char)*pb
) : *pb
;
593 if (wp
->who_host
[0] == '\0')
594 return strsave(CGETS(26, 12, "local"));
597 wbuf
= xmalloc(strlen(pb
) + 1);
599 for (; *pb
!= '\0'; pb
++)
600 *wb
++ = isupper((unsigned char)*pb
) ?
601 tolower((unsigned char)*pb
) : *pb
;
605 #endif /* UTHOSTLEN */
608 return strsave(wp
->who_tty
);
622 dolog(Char
**v
, struct command
*c
)
629 vp
= adrof(STRwatch
); /* lint insists vp isn't used unless we */
630 if (vp
== NULL
) /* unless we assign it outside the if */
631 stderror(ERR_NOWATCH
);
633 wp
= whohead
.who_next
;
634 while (wp
->who_next
!= NULL
) {
635 wp
->who_name
[0] = '\0';
650 char *tty
= short2str(varval(STRtty
));
656 for (wp
= whohead
.who_next
; wp
->who_next
!= NULL
; wp
= wp
->who_next
) {
657 if (strcmp(tty
, wp
->who_tty
) == 0)
659 wp
->who_name
[0] = '\0';
664 # endif /* UTHOSTLEN */
668 add_to_who_list(char *name
, char *mach_nm
)
671 struct who
*wp
, *wpnew
;
674 wp
= whohead
.who_next
;
675 while (wp
->who_next
&& (comp
= strncmp(wp
->who_tty
,mach_nm
,UTLINLEN
)) < 0)
676 wp
= wp
->who_next
;/* find that tty! */
678 if (wp
->who_next
&& comp
== 0) { /* found the tty... */
682 wp
->who_status
= OFFLINE
;
684 else if (strncmp(name
, wp
->who_name
, UTNAMLEN
) == 0) {
685 /* someone is logged in */
687 wp
->who_status
= 0; /* same guy */
690 (void) strncpy(wp
->who_new
, name
, UTNAMLEN
);
692 if (wp
->who_name
[0] == '\0')
693 wp
->who_status
= ONLINE
;
695 wp
->who_status
= CHANGED
;
699 wpnew
= xcalloc(1, sizeof *wpnew
);
700 (void) strncpy(wpnew
->who_tty
, mach_nm
, UTLINLEN
);
703 wpnew
->who_status
= OFFLINE
;
705 (void) strncpy(wpnew
->who_new
, name
, UTNAMLEN
);
706 wpnew
->who_status
= ONLINE
;
709 debugwholist(wpnew
, wp
);
710 #endif /* WHODEBUG */
712 wpnew
->who_next
= wp
; /* link in a new 'who' */
713 wpnew
->who_prev
= wp
->who_prev
;
714 wpnew
->who_prev
->who_next
= wpnew
;
715 wp
->who_prev
= wpnew
; /* linked in now */
718 #endif /* WINNT_NATIVE */
719 #endif /* HAVENOUTMP */