1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
|
/* $Header: /src/pub/tcsh/tc.who.c,v 3.32 2000/11/12 02:18:07 christos Exp $ */
/*
* tc.who.c: Watch logins and logouts...
*/
/*-
* Copyright (c) 1980, 1991 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "sh.h"
RCSID("$Id: tc.who.c,v 3.32 2000/11/12 02:18:07 christos Exp $")
#include "tc.h"
#ifndef HAVENOUTMP
/*
* kfk 26 Jan 1984 - for login watch functions.
*/
#include <ctype.h>
#ifdef HAVEUTMPX
# include <utmpx.h>
/* I just redefine a few words here. Changing every occurrence below
* seems like too much of work. All UTMP functions have equivalent
* UTMPX counterparts, so they can be added all here when needed.
* Kimmo Suominen, Oct 14 1991
*/
# ifndef _PATH_UTMP
# if defined(__UTMPX_FILE) && !defined(UTMPX_FILE)
# define _PATH_UTMP __UTMPX_FILE
# else
# define _PATH_UTMP UTMPX_FILE
# endif /* __UTMPX_FILE && !UTMPX_FILE */
# endif /* _PATH_UTMP */
# define utmp utmpx
# ifdef __MVS__
# define ut_time ut_tv.tv_sec
# define ut_name ut_user
# else
# define ut_time ut_xtime
# endif /* __MVS__ */
#else /* !HAVEUTMPX */
# ifndef WINNT_NATIVE
# include <utmp.h>
# endif /* WINNT_NATIVE */
#endif /* HAVEUTMPX */
#ifndef BROKEN_CC
# define UTNAMLEN sizeof(((struct utmp *) 0)->ut_name)
# define UTLINLEN sizeof(((struct utmp *) 0)->ut_line)
# ifdef UTHOST
# ifdef _SEQUENT_
# define UTHOSTLEN 100
# else
# define UTHOSTLEN sizeof(((struct utmp *) 0)->ut_host)
# endif
# endif /* UTHOST */
#else
/* give poor cc a little help if it needs it */
struct utmp __ut;
# define UTNAMLEN sizeof(__ut.ut_name)
# define UTLINLEN sizeof(__ut.ut_line)
# ifdef UTHOST
# ifdef _SEQUENT_
# define UTHOSTLEN 100
# else
# define UTHOSTLEN sizeof(__ut.ut_host)
# endif
# endif /* UTHOST */
#endif /* BROKEN_CC */
#ifndef _PATH_UTMP
# ifdef UTMP_FILE
# define _PATH_UTMP UTMP_FILE
# else
# define _PATH_UTMP "/etc/utmp"
# endif /* UTMP_FILE */
#endif /* _PATH_UTMP */
struct who {
struct who *who_next;
struct who *who_prev;
char who_name[UTNAMLEN + 1];
char who_new[UTNAMLEN + 1];
char who_tty[UTLINLEN + 1];
#ifdef UTHOST
char who_host[UTHOSTLEN + 1];
#endif /* UTHOST */
time_t who_time;
int who_status;
};
static struct who whohead, whotail;
static time_t watch_period = 0;
static time_t stlast = 0;
#ifdef WHODEBUG
static void debugwholist __P((struct who *, struct who *));
#endif
static void print_who __P((struct who *));
#define ONLINE 01
#define OFFLINE 02
#define CHANGED 04
#define STMASK 07
#define ANNOUNCE 010
/*
* Karl Kleinpaste, 26 Jan 1984.
* Initialize the dummy tty list for login watch.
* This dummy list eliminates boundary conditions
* when doing pointer-chase searches.
*/
void
initwatch()
{
whohead.who_next = &whotail;
whotail.who_prev = &whohead;
stlast = 1;
#ifdef WHODEBUG
debugwholist(NULL, NULL);
#endif /* WHODEBUG */
}
void
resetwatch()
{
watch_period = 0;
stlast = 0;
}
/*
* Karl Kleinpaste, 26 Jan 1984.
* Watch /etc/utmp for login/logout changes.
*/
void
watch_login(force)
int force;
{
int utmpfd, comp = -1, alldone;
int firsttime = stlast == 1;
#ifdef BSDSIGS
sigmask_t omask;
#endif /* BSDSIGS */
struct utmp utmp;
struct who *wp, *wpnew;
struct varent *v;
Char **vp = NULL;
time_t t, interval = MAILINTVL;
struct stat sta;
#if defined(UTHOST) && defined(_SEQUENT_)
char *host, *ut_find_host();
#endif
#ifdef WINNT_NATIVE
static int ncbs_posted = 0;
USE(utmp);
USE(utmpfd);
USE(sta);
USE(wpnew);
#endif /* WINNT_NATIVE */
/* stop SIGINT, lest our login list get trashed. */
#ifdef BSDSIGS
omask = sigblock(sigmask(SIGINT));
#else
(void) sighold(SIGINT);
#endif
v = adrof(STRwatch);
if (v == NULL && !force) {
#ifdef BSDSIGS
(void) sigsetmask(omask);
#else
(void) sigrelse(SIGINT);
#endif
return; /* no names to watch */
}
if (!force) {
trim(vp = v->vec);
if (blklen(vp) % 2) /* odd # args: 1st == # minutes. */
interval = (number(*vp)) ? (getn(*vp++) * 60) : MAILINTVL;
}
else
interval = 0;
(void) time(&t);
#ifdef WINNT_NATIVE
/*
* Since NCB_ASTATs take time, start em async at least 90 secs
* before we are due -amol 6/5/97
*/
if (!ncbs_posted) {
unsigned long tdiff = t - watch_period;
if (!watch_period || ((tdiff > 0) && (tdiff > (interval - 90)))) {
start_ncbs(vp);
ncbs_posted = 1;
}
}
#endif /* WINNT_NATIVE */
if (t - watch_period < interval) {
#ifdef BSDSIGS
(void) sigsetmask(omask);
#else
(void) sigrelse(SIGINT);
#endif
return; /* not long enough yet... */
}
watch_period = t;
#ifdef WINNT_NATIVE
ncbs_posted = 0;
#else /* !WINNT_NATIVE */
/*
* From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
* Don't open utmp all the time, stat it first...
*/
if (stat(_PATH_UTMP, &sta)) {
if (!force)
xprintf(CGETS(26, 1,
"cannot stat %s. Please \"unset watch\".\n"),
_PATH_UTMP);
# ifdef BSDSIGS
(void) sigsetmask(omask);
# else
(void) sigrelse(SIGINT);
# endif
return;
}
if (stlast == sta.st_mtime) {
# ifdef BSDSIGS
(void) sigsetmask(omask);
# else
(void) sigrelse(SIGINT);
# endif
return;
}
stlast = sta.st_mtime;
if ((utmpfd = open(_PATH_UTMP, O_RDONLY)) < 0) {
if (!force)
xprintf(CGETS(26, 2,
"%s cannot be opened. Please \"unset watch\".\n"),
_PATH_UTMP);
# ifdef BSDSIGS
(void) sigsetmask(omask);
# else
(void) sigrelse(SIGINT);
# endif
return;
}
/*
* xterm clears the entire utmp entry - mark everyone on the status list
* OFFLINE or we won't notice X "logouts"
*/
for (wp = whohead.who_next; wp->who_next != NULL; wp = wp->who_next) {
wp->who_status = OFFLINE;
wp->who_time = 0;
}
/*
* Read in the utmp file, sort the entries, and update existing entries or
* add new entries to the status list.
*/
while (read(utmpfd, (char *) &utmp, sizeof utmp) == sizeof utmp) {
# ifdef DEAD_PROCESS
# ifndef IRIS4D
if (utmp.ut_type != USER_PROCESS)
continue;
# else
/* Why is that? Cause the utmp file is always corrupted??? */
if (utmp.ut_type != USER_PROCESS && utmp.ut_type != DEAD_PROCESS)
continue;
# endif /* IRIS4D */
# endif /* DEAD_PROCESS */
if (utmp.ut_name[0] == '\0' && utmp.ut_line[0] == '\0')
continue; /* completely void entry */
# ifdef DEAD_PROCESS
if (utmp.ut_type == DEAD_PROCESS && utmp.ut_line[0] == '\0')
continue;
# endif /* DEAD_PROCESS */
wp = whohead.who_next;
while (wp->who_next && (comp = strncmp(wp->who_tty, utmp.ut_line, UTLINLEN)) < 0)
wp = wp->who_next;/* find that tty! */
if (wp->who_next && comp == 0) { /* found the tty... */
# ifdef DEAD_PROCESS
if (utmp.ut_type == DEAD_PROCESS) {
wp->who_time = utmp.ut_time;
wp->who_status = OFFLINE;
}
else
# endif /* DEAD_PROCESS */
if (utmp.ut_name[0] == '\0') {
wp->who_time = utmp.ut_time;
wp->who_status = OFFLINE;
}
else if (strncmp(utmp.ut_name, wp->who_name, UTNAMLEN) == 0) {
/* someone is logged in */
wp->who_time = utmp.ut_time;
wp->who_status = 0; /* same guy */
}
else {
(void) strncpy(wp->who_new, utmp.ut_name, UTNAMLEN);
# ifdef UTHOST
# ifdef _SEQUENT_
host = ut_find_host(wp->who_tty);
if (host)
(void) strncpy(wp->who_host, host, UTHOSTLEN);
else
wp->who_host[0] = 0;
# else
(void) strncpy(wp->who_host, utmp.ut_host, UTHOSTLEN);
# endif
# endif /* UTHOST */
wp->who_time = utmp.ut_time;
if (wp->who_name[0] == '\0')
wp->who_status = ONLINE;
else
wp->who_status = CHANGED;
}
}
else { /* new tty in utmp */
wpnew = (struct who *) xcalloc(1, sizeof *wpnew);
(void) strncpy(wpnew->who_tty, utmp.ut_line, UTLINLEN);
# ifdef UTHOST
# ifdef _SEQUENT_
host = ut_find_host(wpnew->who_tty);
if (host)
(void) strncpy(wpnew->who_host, host, UTHOSTLEN);
else
wpnew->who_host[0] = 0;
# else
(void) strncpy(wpnew->who_host, utmp.ut_host, UTHOSTLEN);
# endif
# endif /* UTHOST */
wpnew->who_time = utmp.ut_time;
# ifdef DEAD_PROCESS
if (utmp.ut_type == DEAD_PROCESS)
wpnew->who_status = OFFLINE;
else
# endif /* DEAD_PROCESS */
if (utmp.ut_name[0] == '\0')
wpnew->who_status = OFFLINE;
else {
(void) strncpy(wpnew->who_new, utmp.ut_name, UTNAMLEN);
wpnew->who_status = ONLINE;
}
# ifdef WHODEBUG
debugwholist(wpnew, wp);
# endif /* WHODEBUG */
wpnew->who_next = wp; /* link in a new 'who' */
wpnew->who_prev = wp->who_prev;
wpnew->who_prev->who_next = wpnew;
wp->who_prev = wpnew; /* linked in now */
}
}
(void) close(utmpfd);
# if defined(UTHOST) && defined(_SEQUENT_)
endutent();
# endif
#endif /* !WINNT_NATIVE */
if (force || vp == NULL)
return;
/*
* The state of all logins is now known, so we can search the user's list
* of watchables to print the interesting ones.
*/
for (alldone = 0; !alldone && *vp != NULL && **vp != '\0' &&
*(vp + 1) != NULL && **(vp + 1) != '\0';
vp += 2) { /* args used in pairs... */
if (eq(*vp, STRany) && eq(*(vp + 1), STRany))
alldone = 1;
for (wp = whohead.who_next; wp->who_next != NULL; wp = wp->who_next) {
if (wp->who_status & ANNOUNCE ||
(!eq(STRany, vp[0]) &&
!Gmatch(str2short(wp->who_name), vp[0]) &&
!Gmatch(str2short(wp->who_new), vp[0])) ||
(!Gmatch(str2short(wp->who_tty), vp[1]) &&
!eq(STRany, vp[1])))
continue; /* entry doesn't qualify */
/* already printed or not right one to print */
if (wp->who_time == 0)/* utmp entry was cleared */
wp->who_time = watch_period;
if ((wp->who_status & OFFLINE) &&
(wp->who_name[0] != '\0')) {
if (!firsttime)
print_who(wp);
wp->who_name[0] = '\0';
wp->who_status |= ANNOUNCE;
continue;
}
if (wp->who_status & ONLINE) {
if (!firsttime)
print_who(wp);
(void) strcpy(wp->who_name, wp->who_new);
wp->who_status |= ANNOUNCE;
continue;
}
if (wp->who_status & CHANGED) {
if (!firsttime)
print_who(wp);
(void) strcpy(wp->who_name, wp->who_new);
wp->who_status |= ANNOUNCE;
continue;
}
}
}
#ifdef BSDSIGS
(void) sigsetmask(omask);
#else
(void) sigrelse(SIGINT);
#endif
}
#ifdef WHODEBUG
static void
debugwholist(new, wp)
register struct who *new, *wp;
{
register struct who *a;
a = whohead.who_next;
while (a->who_next != NULL) {
xprintf("%s/%s -> ", a->who_name, a->who_tty);
a = a->who_next;
}
xprintf("TAIL\n");
if (a != &whotail) {
xprintf(CGETS(26, 3, "BUG! last element is not whotail!\n"));
abort();
}
a = whotail.who_prev;
xprintf(CGETS(26, 4, "backward: "));
while (a->who_prev != NULL) {
xprintf("%s/%s -> ", a->who_name, a->who_tty);
a = a->who_prev;
}
xprintf("HEAD\n");
if (a != &whohead) {
xprintf(CGETS(26, 5, "BUG! first element is not whohead!\n"));
abort();
}
if (new)
xprintf(CGETS(26, 6, "new: %s/%s\n"), new->who_name, new->who_tty);
if (wp)
xprintf("wp: %s/%s\n", wp->who_name, wp->who_tty);
}
#endif /* WHODEBUG */
static void
print_who(wp)
struct who *wp;
{
#ifdef UTHOST
Char *cp = str2short(CGETS(26, 7, "%n has %a %l from %m."));
#else
Char *cp = str2short(CGETS(26, 8, "%n has %a %l."));
#endif /* UTHOST */
struct varent *vp = adrof(STRwho);
Char buf[BUFSIZE];
if (vp && vp->vec[0])
cp = vp->vec[0];
tprintf(FMT_WHO, buf, cp, BUFSIZE, NULL, wp->who_time, (ptr_t) wp);
for (cp = buf; *cp;)
xputchar(*cp++);
xputchar('\n');
} /* end print_who */
const char *
who_info(ptr, c, wbuf, wbufsiz)
ptr_t ptr;
int c;
char *wbuf;
size_t wbufsiz;
{
struct who *wp = (struct who *) ptr;
#ifdef UTHOST
char *wb = wbuf;
int flg;
char *pb;
#endif /* UTHOST */
switch (c) {
case 'n': /* user name */
switch (wp->who_status & STMASK) {
case ONLINE:
case CHANGED:
return wp->who_new;
case OFFLINE:
return wp->who_name;
default:
break;
}
break;
case 'a':
switch (wp->who_status & STMASK) {
case ONLINE:
return CGETS(26, 9, "logged on");
case OFFLINE:
return CGETS(26, 10, "logged off");
case CHANGED:
xsnprintf(wbuf, wbufsiz, CGETS(26, 11, "replaced %s on"),
wp->who_name);
return wbuf;
default:
break;
}
break;
#ifdef UTHOST
case 'm':
if (wp->who_host[0] == '\0')
return CGETS(26, 12, "local");
else {
/* the ':' stuff is for <host>:<display>.<screen> */
for (pb = wp->who_host, flg = Isdigit(*pb) ? '\0' : '.';
*pb != '\0' &&
(*pb != flg || ((pb = strchr(pb, ':')) != 0));
pb++) {
if (*pb == ':')
flg = '\0';
*wb++ = Isupper(*pb) ? Tolower(*pb) : *pb;
}
*wb = '\0';
return wbuf;
}
case 'M':
if (wp->who_host[0] == '\0')
return CGETS(26, 12, "local");
else {
for (pb = wp->who_host; *pb != '\0'; pb++)
*wb++ = Isupper(*pb) ? Tolower(*pb) : *pb;
*wb = '\0';
return wbuf;
}
#endif /* UTHOST */
case 'l':
return wp->who_tty;
default:
wbuf[0] = '%';
wbuf[1] = (char) c;
wbuf[2] = '\0';
return wbuf;
}
return NULL;
}
void
/*ARGSUSED*/
dolog(v, c)
Char **v;
struct command *c;
{
struct who *wp;
struct varent *vp;
USE(v);
USE(c);
vp = adrof(STRwatch); /* lint insists vp isn't used unless we */
if (vp == NULL) /* unless we assign it outside the if */
stderror(ERR_NOWATCH);
resetwatch();
wp = whohead.who_next;
while (wp->who_next != NULL) {
wp->who_name[0] = '\0';
wp = wp->who_next;
}
}
# ifdef UTHOST
size_t
utmphostsize()
{
return UTHOSTLEN;
}
char *
utmphost()
{
char *tty = short2str(varval(STRtty));
struct who *wp;
char *host = NULL;
watch_login(1);
for (wp = whohead.who_next; wp->who_next != NULL; wp = wp->who_next) {
if (strcmp(tty, wp->who_tty) == 0)
host = wp->who_host;
wp->who_name[0] = '\0';
}
resetwatch();
return host;
}
# endif /* UTHOST */
#ifdef WINNT_NATIVE
void add_to_who_list(name, mach_nm)
char *name;
char *mach_nm;
{
struct who *wp, *wpnew;
int comp = -1;
wp = whohead.who_next;
while (wp->who_next && (comp = strncmp(wp->who_tty,mach_nm,UTLINLEN)) < 0)
wp = wp->who_next;/* find that tty! */
if (wp->who_next && comp == 0) { /* found the tty... */
if (*name == '\0') {
wp->who_time = 0;
wp->who_status = OFFLINE;
}
else if (strncmp(name, wp->who_name, UTNAMLEN) == 0) {
/* someone is logged in */
wp->who_time = 0;
wp->who_status = 0; /* same guy */
}
else {
(void) strncpy(wp->who_new, name, UTNAMLEN);
wp->who_time = 0;
if (wp->who_name[0] == '\0')
wp->who_status = ONLINE;
else
wp->who_status = CHANGED;
}
}
else {
wpnew = (struct who *) xcalloc(1, sizeof *wpnew);
(void) strncpy(wpnew->who_tty, mach_nm, UTLINLEN);
wpnew->who_time = 0;
if (*name == '\0')
wpnew->who_status = OFFLINE;
else {
(void) strncpy(wpnew->who_new, name, UTNAMLEN);
wpnew->who_status = ONLINE;
}
#ifdef WHODEBUG
debugwholist(wpnew, wp);
#endif /* WHODEBUG */
wpnew->who_next = wp; /* link in a new 'who' */
wpnew->who_prev = wp->who_prev;
wpnew->who_prev->who_next = wpnew;
wp->who_prev = wpnew; /* linked in now */
}
}
#endif /* WINNT_NATIVE */
#endif /* HAVENOUTMP */
|