2 * Copyright (c) 1993, John Brezak
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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
33 * $FreeBSD: src/libexec/rpc.rusersd/rusers_proc.c,v 1.10 1999/08/28 00:09:56 peter Exp $
34 * $DragonFly: src/libexec/rpc.rusersd/rusers_proc.c,v 1.2 2003/06/17 04:27:07 dillon Exp $
42 #include <sys/param.h>
49 #include <X11/extensions/xidle.h>
52 #include <rpcsvc/rnusers.h>
55 #define IGNOREUSER "sleeper"
58 #define _PATH_UTMP UTMP_FILE
62 #define _PATH_UTMP "/etc/utmp"
66 #define _PATH_DEV "/dev"
70 #define UT_LINESIZE sizeof(((struct utmp *)0)->ut_line)
73 #define UT_NAMESIZE sizeof(((struct utmp *)0)->ut_name)
76 #define UT_HOSTSIZE sizeof(((struct utmp *)0)->ut_host)
79 typedef char ut_line_t[UT_LINESIZE+1];
80 typedef char ut_name_t[UT_NAMESIZE+1];
81 typedef char ut_host_t[UT_HOSTSIZE+1];
83 utmpidle utmp_idle[MAXUSERS];
84 rutmp old_utmp[MAXUSERS];
85 ut_line_t line[MAXUSERS];
86 ut_name_t name[MAXUSERS];
87 ut_host_t host[MAXUSERS];
89 extern int from_inetd;
96 static jmp_buf openAbort;
101 longjmp (openAbort, 1);
104 XqueryIdle(char *display)
106 int first_event, first_error;
109 (void) signal (SIGALRM, abortOpen);
110 (void) alarm ((unsigned) 10);
111 if (!setjmp (openAbort)) {
112 if (!(dpy= XOpenDisplay(display))) {
113 syslog(LOG_ERR, "Cannot open display %s", display);
116 if (XidleQueryExtension(dpy, &first_event, &first_error)) {
117 if (!XGetIdleTime(dpy, &IdleTime)) {
118 syslog(LOG_ERR, "%s: unable to get idle time", display);
123 syslog(LOG_ERR, "%s: Xidle extension not loaded", display);
129 syslog(LOG_ERR, "%s: server grabbed for over 10 seconds", display);
132 (void) signal (SIGALRM, SIG_DFL);
133 (void) alarm ((unsigned) 0);
136 return((IdleTime + 30) / 60);
141 getidle(char *tty, char *display)
144 char devname[PATH_MAX];
149 * If this is an X terminal or console, then try the
153 if (display && *display && (idle = XqueryIdle(display)) >= 0)
158 u_long kbd_idle, mouse_idle;
159 #if !defined(__FreeBSD__)
160 kbd_idle = getidle("kbd", NULL);
162 kbd_idle = getidle("vga", NULL);
164 mouse_idle = getidle("mouse", NULL);
165 idle = (kbd_idle < mouse_idle)?kbd_idle:mouse_idle;
168 sprintf(devname, "%s/%s", _PATH_DEV, tty);
169 if (stat(devname, &st) < 0) {
171 printf("%s: %s\n", devname, strerror(errno));
177 printf("%s: now=%d atime=%d\n", devname, now,
180 idle = now - st.st_atime;
181 idle = (idle + 30) / 60; /* secs->mins */
183 if (idle < 0) idle = 0;
191 static utmpidlearr ut;
195 bzero((char *)&ut, sizeof(ut));
196 ut.utmpidlearr_val = &utmp_idle[0];
198 ufp = fopen(_PATH_UTMP, "r");
200 syslog(LOG_ERR, "%m");
204 /* only entries with both name and line fields */
205 while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1 &&
207 if (*usr.ut_name && *usr.ut_line &&
208 strncmp(usr.ut_name, IGNOREUSER,
211 && usr.ut_type == USER_PROCESS
214 utmp_idle[nusers].ui_utmp.ut_time =
216 utmp_idle[nusers].ui_idle =
217 getidle(usr.ut_line, usr.ut_host);
218 utmp_idle[nusers].ui_utmp.ut_line = line[nusers];
219 strncpy(line[nusers], usr.ut_line, UT_LINESIZE);
220 utmp_idle[nusers].ui_utmp.ut_name = name[nusers];
221 strncpy(name[nusers], usr.ut_name, UT_NAMESIZE);
222 utmp_idle[nusers].ui_utmp.ut_host = host[nusers];
223 strncpy(host[nusers], usr.ut_host, UT_HOSTSIZE);
225 /* Make sure entries are NUL terminated */
226 line[nusers][UT_LINESIZE] =
227 name[nusers][UT_NAMESIZE] =
228 host[nusers][UT_HOSTSIZE] = '\0';
232 ut.utmpidlearr_len = nusers;
240 static int num_users = 0;
243 ufp = fopen(_PATH_UTMP, "r");
245 syslog(LOG_ERR, "%m");
249 /* only entries with both name and line fields */
250 while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
251 if (*usr.ut_name && *usr.ut_line &&
252 strncmp(usr.ut_name, IGNOREUSER,
255 && usr.ut_type == USER_PROCESS
272 bzero((char *)&ut, sizeof(ut));
274 utidle = do_names_2(all);
276 ut.utmparr_len = utidle->utmpidlearr_len;
277 ut.utmparr_val = &old_utmp[0];
278 for (i = 0; i < ut.utmparr_len; i++)
279 bcopy(&utmp_idle[i].ui_utmp, &old_utmp[i],
280 sizeof(old_utmp[0]));
288 rusersproc_names_2_svc(argp, rqstp)
290 struct svc_req *rqstp;
292 return(do_names_2(0));
296 rusersproc_allnames_2_svc(argp, rqstp)
298 struct svc_req *rqstp;
300 return(do_names_2(1));
304 rusersproc_names_1_svc(argp, rqstp)
306 struct svc_req *rqstp;
308 return(do_names_1(0));
312 rusersproc_allnames_1_svc(argp, rqstp)
314 struct svc_req *rqstp;
316 return(do_names_1(1));
320 rusers_service(rqstp, transp)
321 struct svc_req *rqstp;
328 bool_t (*xdr_argument)(), (*xdr_result)();
331 switch (rqstp->rq_proc) {
333 (void)svc_sendreply(transp, xdr_void, (char *)NULL);
337 xdr_argument = xdr_void;
338 xdr_result = xdr_int;
339 local = (char *(*)()) rusers_num;
342 case RUSERSPROC_NAMES:
343 xdr_argument = xdr_void;
344 xdr_result = xdr_utmpidlearr;
345 switch (rqstp->rq_vers) {
346 case RUSERSVERS_ORIG:
347 local = (char *(*)()) rusersproc_names_1_svc;
349 case RUSERSVERS_IDLE:
350 local = (char *(*)()) rusersproc_names_2_svc;
353 svcerr_progvers(transp, RUSERSVERS_ORIG, RUSERSVERS_IDLE);
359 case RUSERSPROC_ALLNAMES:
360 xdr_argument = xdr_void;
361 xdr_result = xdr_utmpidlearr;
362 switch (rqstp->rq_vers) {
363 case RUSERSVERS_ORIG:
364 local = (char *(*)()) rusersproc_allnames_1_svc;
366 case RUSERSVERS_IDLE:
367 local = (char *(*)()) rusersproc_allnames_2_svc;
370 svcerr_progvers(transp, RUSERSVERS_ORIG, RUSERSVERS_IDLE);
377 svcerr_noproc(transp);
380 bzero((char *)&argument, sizeof(argument));
381 if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
382 svcerr_decode(transp);
385 result = (*local)(&argument, rqstp);
386 if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
387 svcerr_systemerr(transp);
389 if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
390 syslog(LOG_ERR, "unable to free arguments");