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
35 static const char rcsid[] =
44 #include <sys/param.h>
48 #define _ULOG_POSIX_NAMES
53 #include <X11/extensions/xidle.h>
55 #include <rpcsvc/rnusers.h>
58 #define _PATH_DEV "/dev"
61 static utmpidle utmp_idle[MAXUSERS];
62 static utmp old_utmp[MAXUSERS];
63 static struct utmpx utmp_list[MAXUSERS];
65 extern int from_inetd;
67 void rusers_service(struct svc_req *, SVCXPRT *);
72 static jmp_buf openAbort;
77 longjmp (openAbort, 1);
80 XqueryIdle(char *display)
82 int first_event, first_error;
85 (void) signal (SIGALRM, abortOpen);
86 (void) alarm ((unsigned) 10);
87 if (!setjmp (openAbort)) {
88 if (!(dpy= XOpenDisplay(display))) {
89 syslog(LOG_ERR, "Cannot open display %s", display);
92 if (XidleQueryExtension(dpy, &first_event, &first_error)) {
93 if (!XGetIdleTime(dpy, &IdleTime)) {
94 syslog(LOG_ERR, "%s: unable to get idle time", display);
98 syslog(LOG_ERR, "%s: Xidle extension not loaded", display);
103 syslog(LOG_ERR, "%s: server grabbed for over 10 seconds", display);
106 (void) signal (SIGALRM, SIG_DFL);
107 (void) alarm ((unsigned) 0);
110 return((IdleTime + 30) / 60);
115 getidle(const char *tty, const char *display __unused)
118 char ttyname[PATH_MAX];
123 * If this is an X terminal or console, then try the
127 if (display && *display && (idle = XqueryIdle(display)) >= 0)
132 u_long kbd_idle, mouse_idle;
133 #if !defined(__FreeBSD__)
134 kbd_idle = getidle("kbd", NULL);
136 kbd_idle = getidle("vga", NULL);
138 mouse_idle = getidle("mouse", NULL);
139 idle = (kbd_idle < mouse_idle)?kbd_idle:mouse_idle;
141 sprintf(ttyname, "%s/%s", _PATH_DEV, tty);
142 if (stat(ttyname, &st) < 0) {
144 printf("%s: %s\n", ttyname, strerror(errno));
150 printf("%s: now=%d atime=%d\n", ttyname, now,
153 idle = now - st.st_atime;
154 idle = (idle + 30) / 60; /* secs->mins */
163 static utmpidlearr ut;
167 memset(&ut, 0, sizeof(ut));
168 ut.utmpidlearr_val = &utmp_idle[0];
171 while ((usr = getutxent()) != NULL && nusers < MAXUSERS) {
172 if (usr->ut_type != USER_PROCESS)
175 memcpy(&utmp_list[nusers], usr, sizeof(*usr));
176 utmp_idle[nusers].ui_utmp.ut_time = usr->ut_tv.tv_sec;
177 utmp_idle[nusers].ui_idle =
178 getidle(usr->ut_line, usr->ut_host);
179 utmp_idle[nusers].ui_utmp.ut_line =
180 utmp_list[nusers].ut_line;
181 utmp_idle[nusers].ui_utmp.ut_name =
182 utmp_list[nusers].ut_user;
183 utmp_idle[nusers].ui_utmp.ut_host =
184 utmp_list[nusers].ut_host;
190 ut.utmpidlearr_len = nusers;
195 rusers_num(void *argp __unused, struct svc_req *rqstp __unused)
197 static int num_users = 0;
201 while ((usr = getutxent()) != NULL) {
202 if (usr->ut_type != USER_PROCESS)
218 bzero((char *)&ut, sizeof(ut));
220 utidle = do_names_2();
222 ut.utmparr_len = utidle->utmpidlearr_len;
223 ut.utmparr_val = &old_utmp[0];
224 for (i = 0; i < ut.utmparr_len; i++)
225 bcopy(&utmp_idle[i].ui_utmp, &old_utmp[i],
226 sizeof(old_utmp[0]));
234 rusersproc_names_2_svc(void *argp __unused, struct svc_req *rqstp __unused)
237 return (do_names_2());
241 rusersproc_allnames_2_svc(void *argp __unused, struct svc_req *rqstp __unused)
244 return (do_names_2());
248 rusersproc_names_1_svc(void *argp __unused, struct svc_req *rqstp __unused)
251 return (do_names_1());
255 rusersproc_allnames_1_svc(void *argp __unused, struct svc_req *rqstp __unused)
258 return (do_names_1());
261 typedef void *(*rusersproc_t)(void *, struct svc_req *);
264 rusers_service(struct svc_req *rqstp, SVCXPRT *transp)
270 xdrproc_t xdr_argument, xdr_result;
273 switch (rqstp->rq_proc) {
275 (void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
279 xdr_argument = (xdrproc_t)xdr_void;
280 xdr_result = (xdrproc_t)xdr_int;
281 local = (rusersproc_t)rusers_num;
284 case RUSERSPROC_NAMES:
285 xdr_argument = (xdrproc_t)xdr_void;
286 xdr_result = (xdrproc_t)xdr_utmpidlearr;
287 switch (rqstp->rq_vers) {
288 case RUSERSVERS_ORIG:
289 local = (rusersproc_t)rusersproc_names_1_svc;
291 case RUSERSVERS_IDLE:
292 local = (rusersproc_t)rusersproc_names_2_svc;
295 svcerr_progvers(transp, RUSERSVERS_ORIG, RUSERSVERS_IDLE);
301 case RUSERSPROC_ALLNAMES:
302 xdr_argument = (xdrproc_t)xdr_void;
303 xdr_result = (xdrproc_t)xdr_utmpidlearr;
304 switch (rqstp->rq_vers) {
305 case RUSERSVERS_ORIG:
306 local = (rusersproc_t)rusersproc_allnames_1_svc;
308 case RUSERSVERS_IDLE:
309 local = (rusersproc_t)rusersproc_allnames_2_svc;
312 svcerr_progvers(transp, RUSERSVERS_ORIG, RUSERSVERS_IDLE);
319 svcerr_noproc(transp);
322 bzero(&argument, sizeof(argument));
323 if (!svc_getargs(transp, (xdrproc_t)xdr_argument, &argument)) {
324 svcerr_decode(transp);
327 result = (*local)(&argument, rqstp);
328 if (result != NULL &&
329 !svc_sendreply(transp, (xdrproc_t)xdr_result, result)) {
330 svcerr_systemerr(transp);
332 if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, &argument)) {
333 syslog(LOG_ERR, "unable to free arguments");