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>
59 #define _PATH_DEV "/dev"
62 static utmpidle utmp_idle[MAXUSERS];
63 static utmp old_utmp[MAXUSERS];
64 static struct utmpx utmp_list[MAXUSERS];
69 static jmp_buf openAbort;
74 longjmp (openAbort, 1);
77 XqueryIdle(char *display)
79 int first_event, first_error;
82 (void) signal (SIGALRM, abortOpen);
83 (void) alarm ((unsigned) 10);
84 if (!setjmp (openAbort)) {
85 if (!(dpy= XOpenDisplay(display))) {
86 syslog(LOG_ERR, "Cannot open display %s", display);
89 if (XidleQueryExtension(dpy, &first_event, &first_error)) {
90 if (!XGetIdleTime(dpy, &IdleTime)) {
91 syslog(LOG_ERR, "%s: unable to get idle time", display);
95 syslog(LOG_ERR, "%s: Xidle extension not loaded", display);
100 syslog(LOG_ERR, "%s: server grabbed for over 10 seconds", display);
103 (void) signal (SIGALRM, SIG_DFL);
104 (void) alarm ((unsigned) 0);
107 return((IdleTime + 30) / 60);
112 getidle(const char *tty, const char *display __unused)
115 char ttyname[PATH_MAX];
120 * If this is an X terminal or console, then try the
124 if (display && *display && (idle = XqueryIdle(display)) >= 0)
129 u_long kbd_idle, mouse_idle;
130 #if !defined(__FreeBSD__)
131 kbd_idle = getidle("kbd", NULL);
133 kbd_idle = getidle("vga", NULL);
135 mouse_idle = getidle("mouse", NULL);
136 idle = (kbd_idle < mouse_idle)?kbd_idle:mouse_idle;
138 sprintf(ttyname, "%s/%s", _PATH_DEV, tty);
139 if (stat(ttyname, &st) < 0) {
141 printf("%s: %s\n", ttyname, strerror(errno));
147 printf("%s: now=%d atime=%d\n", ttyname, now,
150 idle = now - st.st_atime;
151 idle = (idle + 30) / 60; /* secs->mins */
160 static utmpidlearr ut;
164 memset(&ut, 0, sizeof(ut));
165 ut.utmpidlearr_val = &utmp_idle[0];
168 while ((usr = getutxent()) != NULL && nusers < MAXUSERS) {
169 if (usr->ut_type != USER_PROCESS)
172 memcpy(&utmp_list[nusers], usr, sizeof(*usr));
173 utmp_idle[nusers].ui_utmp.ut_time = usr->ut_tv.tv_sec;
174 utmp_idle[nusers].ui_idle =
175 getidle(usr->ut_line, usr->ut_host);
176 utmp_idle[nusers].ui_utmp.ut_line =
177 utmp_list[nusers].ut_line;
178 utmp_idle[nusers].ui_utmp.ut_name =
179 utmp_list[nusers].ut_user;
180 utmp_idle[nusers].ui_utmp.ut_host =
181 utmp_list[nusers].ut_host;
187 ut.utmpidlearr_len = nusers;
192 rusers_num(void *argp __unused, struct svc_req *rqstp __unused)
194 static int num_users = 0;
198 while ((usr = getutxent()) != NULL) {
199 if (usr->ut_type != USER_PROCESS)
215 bzero((char *)&ut, sizeof(ut));
217 utidle = do_names_2();
219 ut.utmparr_len = utidle->utmpidlearr_len;
220 ut.utmparr_val = &old_utmp[0];
221 for (i = 0; i < ut.utmparr_len; i++)
222 bcopy(&utmp_idle[i].ui_utmp, &old_utmp[i],
223 sizeof(old_utmp[0]));
231 rusersproc_names_2_svc(void *argp __unused, struct svc_req *rqstp __unused)
234 return (do_names_2());
238 rusersproc_allnames_2_svc(void *argp __unused, struct svc_req *rqstp __unused)
241 return (do_names_2());
245 rusersproc_names_1_svc(void *argp __unused, struct svc_req *rqstp __unused)
248 return (do_names_1());
252 rusersproc_allnames_1_svc(void *argp __unused, struct svc_req *rqstp __unused)
255 return (do_names_1());
258 typedef void *(*rusersproc_t)(void *, struct svc_req *);
261 rusers_service(struct svc_req *rqstp, SVCXPRT *transp)
267 xdrproc_t xdr_argument, xdr_result;
270 switch (rqstp->rq_proc) {
272 (void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
276 xdr_argument = (xdrproc_t)xdr_void;
277 xdr_result = (xdrproc_t)xdr_int;
278 local = (rusersproc_t)rusers_num;
281 case RUSERSPROC_NAMES:
282 xdr_argument = (xdrproc_t)xdr_void;
283 xdr_result = (xdrproc_t)xdr_utmpidlearr;
284 switch (rqstp->rq_vers) {
285 case RUSERSVERS_ORIG:
286 local = (rusersproc_t)rusersproc_names_1_svc;
288 case RUSERSVERS_IDLE:
289 local = (rusersproc_t)rusersproc_names_2_svc;
292 svcerr_progvers(transp, RUSERSVERS_ORIG, RUSERSVERS_IDLE);
298 case RUSERSPROC_ALLNAMES:
299 xdr_argument = (xdrproc_t)xdr_void;
300 xdr_result = (xdrproc_t)xdr_utmpidlearr;
301 switch (rqstp->rq_vers) {
302 case RUSERSVERS_ORIG:
303 local = (rusersproc_t)rusersproc_allnames_1_svc;
305 case RUSERSVERS_IDLE:
306 local = (rusersproc_t)rusersproc_allnames_2_svc;
309 svcerr_progvers(transp, RUSERSVERS_ORIG, RUSERSVERS_IDLE);
316 svcerr_noproc(transp);
319 bzero(&argument, sizeof(argument));
320 if (!svc_getargs(transp, (xdrproc_t)xdr_argument, &argument)) {
321 svcerr_decode(transp);
324 result = (*local)(&argument, rqstp);
325 if (result != NULL &&
326 !svc_sendreply(transp, (xdrproc_t)xdr_result, result)) {
327 svcerr_systemerr(transp);
329 if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, &argument)) {
330 syslog(LOG_ERR, "unable to free arguments");