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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char rcsid[] =
40 #include <sys/param.h>
48 #include <X11/extensions/xidle.h>
50 #include <rpcsvc/rnusers.h>
55 #define _PATH_DEV "/dev"
58 static utmpidle utmp_idle[MAXUSERS];
59 static utmp old_utmp[MAXUSERS];
60 static struct utmpx utmp_list[MAXUSERS];
65 static jmp_buf openAbort;
70 longjmp (openAbort, 1);
73 XqueryIdle(char *display)
75 int first_event, first_error;
78 (void) signal (SIGALRM, abortOpen);
79 (void) alarm ((unsigned) 10);
80 if (!setjmp (openAbort)) {
81 if (!(dpy= XOpenDisplay(display))) {
82 syslog(LOG_ERR, "Cannot open display %s", display);
85 if (XidleQueryExtension(dpy, &first_event, &first_error)) {
86 if (!XGetIdleTime(dpy, &IdleTime)) {
87 syslog(LOG_ERR, "%s: unable to get idle time", display);
91 syslog(LOG_ERR, "%s: Xidle extension not loaded", display);
96 syslog(LOG_ERR, "%s: server grabbed for over 10 seconds", display);
99 (void) signal (SIGALRM, SIG_DFL);
100 (void) alarm ((unsigned) 0);
103 return((IdleTime + 30) / 60);
108 getidle(const char *tty, const char *display __unused)
111 char ttyname[PATH_MAX];
116 * If this is an X terminal or console, then try the
120 if (display && *display && (idle = XqueryIdle(display)) >= 0)
125 u_long kbd_idle, mouse_idle;
126 #if !defined(__FreeBSD__)
127 kbd_idle = getidle("kbd", NULL);
129 kbd_idle = getidle("vga", NULL);
131 mouse_idle = getidle("mouse", NULL);
132 idle = (kbd_idle < mouse_idle)?kbd_idle:mouse_idle;
134 sprintf(ttyname, "%s/%s", _PATH_DEV, tty);
135 if (stat(ttyname, &st) < 0) {
137 printf("%s: %s\n", ttyname, strerror(errno));
143 printf("%s: now=%d atime=%d\n", ttyname, now,
146 idle = now - st.st_atime;
147 idle = (idle + 30) / 60; /* secs->mins */
156 static utmpidlearr ut;
160 memset(&ut, 0, sizeof(ut));
161 ut.utmpidlearr_val = &utmp_idle[0];
164 while ((usr = getutxent()) != NULL && nusers < MAXUSERS) {
165 if (usr->ut_type != USER_PROCESS)
168 memcpy(&utmp_list[nusers], usr, sizeof(*usr));
169 utmp_idle[nusers].ui_utmp.ut_time = usr->ut_tv.tv_sec;
170 utmp_idle[nusers].ui_idle =
171 getidle(usr->ut_line, usr->ut_host);
172 utmp_idle[nusers].ui_utmp.ut_line =
173 utmp_list[nusers].ut_line;
174 utmp_idle[nusers].ui_utmp.ut_name =
175 utmp_list[nusers].ut_user;
176 utmp_idle[nusers].ui_utmp.ut_host =
177 utmp_list[nusers].ut_host;
183 ut.utmpidlearr_len = nusers;
188 rusers_num(void *argp __unused, struct svc_req *rqstp __unused)
190 static int num_users = 0;
194 while ((usr = getutxent()) != NULL) {
195 if (usr->ut_type != USER_PROCESS)
211 bzero((char *)&ut, sizeof(ut));
213 utidle = do_names_2();
215 ut.utmparr_len = utidle->utmpidlearr_len;
216 ut.utmparr_val = &old_utmp[0];
217 for (i = 0; i < ut.utmparr_len; i++)
218 bcopy(&utmp_idle[i].ui_utmp, &old_utmp[i],
219 sizeof(old_utmp[0]));
227 rusersproc_names_2_svc(void *argp __unused, struct svc_req *rqstp __unused)
230 return (do_names_2());
234 rusersproc_allnames_2_svc(void *argp __unused, struct svc_req *rqstp __unused)
237 return (do_names_2());
241 rusersproc_names_1_svc(void *argp __unused, struct svc_req *rqstp __unused)
244 return (do_names_1());
248 rusersproc_allnames_1_svc(void *argp __unused, struct svc_req *rqstp __unused)
251 return (do_names_1());
254 typedef void *(*rusersproc_t)(void *, struct svc_req *);
257 rusers_service(struct svc_req *rqstp, SVCXPRT *transp)
263 xdrproc_t xdr_argument, xdr_result;
266 switch (rqstp->rq_proc) {
268 (void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
272 xdr_argument = (xdrproc_t)xdr_void;
273 xdr_result = (xdrproc_t)xdr_int;
274 local = (rusersproc_t)rusers_num;
277 case RUSERSPROC_NAMES:
278 xdr_argument = (xdrproc_t)xdr_void;
279 xdr_result = (xdrproc_t)xdr_utmpidlearr;
280 switch (rqstp->rq_vers) {
281 case RUSERSVERS_ORIG:
282 local = (rusersproc_t)rusersproc_names_1_svc;
284 case RUSERSVERS_IDLE:
285 local = (rusersproc_t)rusersproc_names_2_svc;
288 svcerr_progvers(transp, RUSERSVERS_ORIG, RUSERSVERS_IDLE);
294 case RUSERSPROC_ALLNAMES:
295 xdr_argument = (xdrproc_t)xdr_void;
296 xdr_result = (xdrproc_t)xdr_utmpidlearr;
297 switch (rqstp->rq_vers) {
298 case RUSERSVERS_ORIG:
299 local = (rusersproc_t)rusersproc_allnames_1_svc;
301 case RUSERSVERS_IDLE:
302 local = (rusersproc_t)rusersproc_allnames_2_svc;
305 svcerr_progvers(transp, RUSERSVERS_ORIG, RUSERSVERS_IDLE);
312 svcerr_noproc(transp);
315 bzero(&argument, sizeof(argument));
316 if (!svc_getargs(transp, (xdrproc_t)xdr_argument, &argument)) {
317 svcerr_decode(transp);
320 result = (*local)(&argument, rqstp);
321 if (result != NULL &&
322 !svc_sendreply(transp, (xdrproc_t)xdr_result, result)) {
323 svcerr_systemerr(transp);
325 if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, &argument)) {
326 syslog(LOG_ERR, "unable to free arguments");