2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1993, John Brezak
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/param.h>
45 #include <X11/extensions/xidle.h>
47 #include <rpcsvc/rnusers.h>
52 #define _PATH_DEV "/dev"
55 static utmpidle utmp_idle[MAXUSERS];
56 static utmp old_utmp[MAXUSERS];
57 static struct utmpx utmp_list[MAXUSERS];
62 static jmp_buf openAbort;
67 longjmp (openAbort, 1);
70 XqueryIdle(char *display)
72 int first_event, first_error;
75 (void) signal (SIGALRM, abortOpen);
76 (void) alarm ((unsigned) 10);
77 if (!setjmp (openAbort)) {
78 if (!(dpy= XOpenDisplay(display))) {
79 syslog(LOG_ERR, "Cannot open display %s", display);
82 if (XidleQueryExtension(dpy, &first_event, &first_error)) {
83 if (!XGetIdleTime(dpy, &IdleTime)) {
84 syslog(LOG_ERR, "%s: unable to get idle time", display);
88 syslog(LOG_ERR, "%s: Xidle extension not loaded", display);
93 syslog(LOG_ERR, "%s: server grabbed for over 10 seconds", display);
96 (void) signal (SIGALRM, SIG_DFL);
97 (void) alarm ((unsigned) 0);
100 return((IdleTime + 30) / 60);
105 getidle(const char *tty, const char *display __unused)
108 char ttyname[PATH_MAX];
113 * If this is an X terminal or console, then try the
117 if (display && *display && (idle = XqueryIdle(display)) >= 0)
122 u_long kbd_idle, mouse_idle;
123 #if !defined(__FreeBSD__)
124 kbd_idle = getidle("kbd", NULL);
126 kbd_idle = getidle("vga", NULL);
128 mouse_idle = getidle("mouse", NULL);
129 idle = (kbd_idle < mouse_idle)?kbd_idle:mouse_idle;
131 sprintf(ttyname, "%s/%s", _PATH_DEV, tty);
132 if (stat(ttyname, &st) < 0) {
134 printf("%s: %s\n", ttyname, strerror(errno));
140 printf("%s: now=%d atime=%d\n", ttyname, now,
143 idle = now - st.st_atime;
144 idle = (idle + 30) / 60; /* secs->mins */
153 static utmpidlearr ut;
157 memset(&ut, 0, sizeof(ut));
158 ut.utmpidlearr_val = &utmp_idle[0];
161 while ((usr = getutxent()) != NULL && nusers < MAXUSERS) {
162 if (usr->ut_type != USER_PROCESS)
165 memcpy(&utmp_list[nusers], usr, sizeof(*usr));
166 utmp_idle[nusers].ui_utmp.ut_time = usr->ut_tv.tv_sec;
167 utmp_idle[nusers].ui_idle =
168 getidle(usr->ut_line, usr->ut_host);
169 utmp_idle[nusers].ui_utmp.ut_line =
170 utmp_list[nusers].ut_line;
171 utmp_idle[nusers].ui_utmp.ut_name =
172 utmp_list[nusers].ut_user;
173 utmp_idle[nusers].ui_utmp.ut_host =
174 utmp_list[nusers].ut_host;
180 ut.utmpidlearr_len = nusers;
185 rusers_num(void *argp __unused, struct svc_req *rqstp __unused)
187 static int num_users = 0;
191 while ((usr = getutxent()) != NULL) {
192 if (usr->ut_type != USER_PROCESS)
208 bzero((char *)&ut, sizeof(ut));
210 utidle = do_names_2();
212 ut.utmparr_len = utidle->utmpidlearr_len;
213 ut.utmparr_val = &old_utmp[0];
214 for (i = 0; i < ut.utmparr_len; i++)
215 bcopy(&utmp_idle[i].ui_utmp, &old_utmp[i],
216 sizeof(old_utmp[0]));
224 rusersproc_names_2_svc(void *argp __unused, struct svc_req *rqstp __unused)
227 return (do_names_2());
231 rusersproc_allnames_2_svc(void *argp __unused, struct svc_req *rqstp __unused)
234 return (do_names_2());
238 rusersproc_names_1_svc(void *argp __unused, struct svc_req *rqstp __unused)
241 return (do_names_1());
245 rusersproc_allnames_1_svc(void *argp __unused, struct svc_req *rqstp __unused)
248 return (do_names_1());
251 typedef void *(*rusersproc_t)(void *, struct svc_req *);
254 rusers_service(struct svc_req *rqstp, SVCXPRT *transp)
260 xdrproc_t xdr_argument, xdr_result;
263 switch (rqstp->rq_proc) {
265 (void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
269 xdr_argument = (xdrproc_t)xdr_void;
270 xdr_result = (xdrproc_t)xdr_int;
271 local = (rusersproc_t)rusers_num;
274 case RUSERSPROC_NAMES:
275 xdr_argument = (xdrproc_t)xdr_void;
276 xdr_result = (xdrproc_t)xdr_utmpidlearr;
277 switch (rqstp->rq_vers) {
278 case RUSERSVERS_ORIG:
279 local = (rusersproc_t)rusersproc_names_1_svc;
281 case RUSERSVERS_IDLE:
282 local = (rusersproc_t)rusersproc_names_2_svc;
285 svcerr_progvers(transp, RUSERSVERS_ORIG, RUSERSVERS_IDLE);
291 case RUSERSPROC_ALLNAMES:
292 xdr_argument = (xdrproc_t)xdr_void;
293 xdr_result = (xdrproc_t)xdr_utmpidlearr;
294 switch (rqstp->rq_vers) {
295 case RUSERSVERS_ORIG:
296 local = (rusersproc_t)rusersproc_allnames_1_svc;
298 case RUSERSVERS_IDLE:
299 local = (rusersproc_t)rusersproc_allnames_2_svc;
302 svcerr_progvers(transp, RUSERSVERS_ORIG, RUSERSVERS_IDLE);
309 svcerr_noproc(transp);
312 bzero(&argument, sizeof(argument));
313 if (!svc_getargs(transp, (xdrproc_t)xdr_argument, &argument)) {
314 svcerr_decode(transp);
317 result = (*local)(&argument, rqstp);
318 if (result != NULL &&
319 !svc_sendreply(transp, (xdrproc_t)xdr_result, result)) {
320 svcerr_systemerr(transp);
322 if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, &argument)) {
323 syslog(LOG_ERR, "unable to free arguments");