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>
52 #include <X11/extensions/xidle.h>
54 #include <rpcsvc/rnusers.h>
57 #define _PATH_DEV "/dev"
60 static utmpidle utmp_idle[MAXUSERS];
61 static utmp old_utmp[MAXUSERS];
62 static struct utmpx utmp_list[MAXUSERS];
64 extern int from_inetd;
66 void rusers_service(struct svc_req *, SVCXPRT *);
71 static jmp_buf openAbort;
76 longjmp (openAbort, 1);
79 XqueryIdle(char *display)
81 int first_event, first_error;
84 (void) signal (SIGALRM, abortOpen);
85 (void) alarm ((unsigned) 10);
86 if (!setjmp (openAbort)) {
87 if (!(dpy= XOpenDisplay(display))) {
88 syslog(LOG_ERR, "Cannot open display %s", display);
91 if (XidleQueryExtension(dpy, &first_event, &first_error)) {
92 if (!XGetIdleTime(dpy, &IdleTime)) {
93 syslog(LOG_ERR, "%s: unable to get idle time", display);
97 syslog(LOG_ERR, "%s: Xidle extension not loaded", display);
102 syslog(LOG_ERR, "%s: server grabbed for over 10 seconds", display);
105 (void) signal (SIGALRM, SIG_DFL);
106 (void) alarm ((unsigned) 0);
109 return((IdleTime + 30) / 60);
114 getidle(const char *tty, const char *display __unused)
117 char ttyname[PATH_MAX];
122 * If this is an X terminal or console, then try the
126 if (display && *display && (idle = XqueryIdle(display)) >= 0)
131 u_long kbd_idle, mouse_idle;
132 #if !defined(__FreeBSD__)
133 kbd_idle = getidle("kbd", NULL);
135 kbd_idle = getidle("vga", NULL);
137 mouse_idle = getidle("mouse", NULL);
138 idle = (kbd_idle < mouse_idle)?kbd_idle:mouse_idle;
140 sprintf(ttyname, "%s/%s", _PATH_DEV, tty);
141 if (stat(ttyname, &st) < 0) {
143 printf("%s: %s\n", ttyname, strerror(errno));
149 printf("%s: now=%d atime=%d\n", ttyname, now,
152 idle = now - st.st_atime;
153 idle = (idle + 30) / 60; /* secs->mins */
162 static utmpidlearr ut;
166 memset(&ut, 0, sizeof(ut));
167 ut.utmpidlearr_val = &utmp_idle[0];
170 while ((usr = getutxent()) != NULL && nusers < MAXUSERS) {
171 if (usr->ut_type != USER_PROCESS)
174 memcpy(&utmp_list[nusers], usr, sizeof(*usr));
175 utmp_idle[nusers].ui_utmp.ut_time = usr->ut_tv.tv_sec;
176 utmp_idle[nusers].ui_idle =
177 getidle(usr->ut_line, usr->ut_host);
178 utmp_idle[nusers].ui_utmp.ut_line =
179 utmp_list[nusers].ut_line;
180 utmp_idle[nusers].ui_utmp.ut_name =
181 utmp_list[nusers].ut_user;
182 utmp_idle[nusers].ui_utmp.ut_host =
183 utmp_list[nusers].ut_host;
189 ut.utmpidlearr_len = nusers;
194 rusers_num(void *argp __unused, struct svc_req *rqstp __unused)
196 static int num_users = 0;
200 while ((usr = getutxent()) != NULL) {
201 if (usr->ut_type != USER_PROCESS)
217 bzero((char *)&ut, sizeof(ut));
219 utidle = do_names_2();
221 ut.utmparr_len = utidle->utmpidlearr_len;
222 ut.utmparr_val = &old_utmp[0];
223 for (i = 0; i < ut.utmparr_len; i++)
224 bcopy(&utmp_idle[i].ui_utmp, &old_utmp[i],
225 sizeof(old_utmp[0]));
233 rusersproc_names_2_svc(void *argp __unused, struct svc_req *rqstp __unused)
236 return (do_names_2());
240 rusersproc_allnames_2_svc(void *argp __unused, struct svc_req *rqstp __unused)
243 return (do_names_2());
247 rusersproc_names_1_svc(void *argp __unused, struct svc_req *rqstp __unused)
250 return (do_names_1());
254 rusersproc_allnames_1_svc(void *argp __unused, struct svc_req *rqstp __unused)
257 return (do_names_1());
260 typedef void *(*rusersproc_t)(void *, struct svc_req *);
263 rusers_service(struct svc_req *rqstp, SVCXPRT *transp)
269 xdrproc_t xdr_argument, xdr_result;
272 switch (rqstp->rq_proc) {
274 (void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
278 xdr_argument = (xdrproc_t)xdr_void;
279 xdr_result = (xdrproc_t)xdr_int;
280 local = (rusersproc_t)rusers_num;
283 case RUSERSPROC_NAMES:
284 xdr_argument = (xdrproc_t)xdr_void;
285 xdr_result = (xdrproc_t)xdr_utmpidlearr;
286 switch (rqstp->rq_vers) {
287 case RUSERSVERS_ORIG:
288 local = (rusersproc_t)rusersproc_names_1_svc;
290 case RUSERSVERS_IDLE:
291 local = (rusersproc_t)rusersproc_names_2_svc;
294 svcerr_progvers(transp, RUSERSVERS_ORIG, RUSERSVERS_IDLE);
300 case RUSERSPROC_ALLNAMES:
301 xdr_argument = (xdrproc_t)xdr_void;
302 xdr_result = (xdrproc_t)xdr_utmpidlearr;
303 switch (rqstp->rq_vers) {
304 case RUSERSVERS_ORIG:
305 local = (rusersproc_t)rusersproc_allnames_1_svc;
307 case RUSERSVERS_IDLE:
308 local = (rusersproc_t)rusersproc_allnames_2_svc;
311 svcerr_progvers(transp, RUSERSVERS_ORIG, RUSERSVERS_IDLE);
318 svcerr_noproc(transp);
321 bzero(&argument, sizeof(argument));
322 if (!svc_getargs(transp, (xdrproc_t)xdr_argument, &argument)) {
323 svcerr_decode(transp);
326 result = (*local)(&argument, rqstp);
327 if (result != NULL &&
328 !svc_sendreply(transp, (xdrproc_t)xdr_result, result)) {
329 svcerr_systemerr(transp);
331 if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, &argument)) {
332 syslog(LOG_ERR, "unable to free arguments");