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
33 static const char rcsid[] =
42 #include <sys/param.h>
50 #include <X11/extensions/xidle.h>
52 #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];
67 static jmp_buf openAbort;
72 longjmp (openAbort, 1);
75 XqueryIdle(char *display)
77 int first_event, first_error;
80 (void) signal (SIGALRM, abortOpen);
81 (void) alarm ((unsigned) 10);
82 if (!setjmp (openAbort)) {
83 if (!(dpy= XOpenDisplay(display))) {
84 syslog(LOG_ERR, "Cannot open display %s", display);
87 if (XidleQueryExtension(dpy, &first_event, &first_error)) {
88 if (!XGetIdleTime(dpy, &IdleTime)) {
89 syslog(LOG_ERR, "%s: unable to get idle time", display);
93 syslog(LOG_ERR, "%s: Xidle extension not loaded", display);
98 syslog(LOG_ERR, "%s: server grabbed for over 10 seconds", display);
101 (void) signal (SIGALRM, SIG_DFL);
102 (void) alarm ((unsigned) 0);
105 return((IdleTime + 30) / 60);
110 getidle(const char *tty, const char *display __unused)
113 char ttyname[PATH_MAX];
118 * If this is an X terminal or console, then try the
122 if (display && *display && (idle = XqueryIdle(display)) >= 0)
127 u_long kbd_idle, mouse_idle;
128 #if !defined(__FreeBSD__)
129 kbd_idle = getidle("kbd", NULL);
131 kbd_idle = getidle("vga", NULL);
133 mouse_idle = getidle("mouse", NULL);
134 idle = (kbd_idle < mouse_idle)?kbd_idle:mouse_idle;
136 sprintf(ttyname, "%s/%s", _PATH_DEV, tty);
137 if (stat(ttyname, &st) < 0) {
139 printf("%s: %s\n", ttyname, strerror(errno));
145 printf("%s: now=%d atime=%d\n", ttyname, now,
148 idle = now - st.st_atime;
149 idle = (idle + 30) / 60; /* secs->mins */
158 static utmpidlearr ut;
162 memset(&ut, 0, sizeof(ut));
163 ut.utmpidlearr_val = &utmp_idle[0];
166 while ((usr = getutxent()) != NULL && nusers < MAXUSERS) {
167 if (usr->ut_type != USER_PROCESS)
170 memcpy(&utmp_list[nusers], usr, sizeof(*usr));
171 utmp_idle[nusers].ui_utmp.ut_time = usr->ut_tv.tv_sec;
172 utmp_idle[nusers].ui_idle =
173 getidle(usr->ut_line, usr->ut_host);
174 utmp_idle[nusers].ui_utmp.ut_line =
175 utmp_list[nusers].ut_line;
176 utmp_idle[nusers].ui_utmp.ut_name =
177 utmp_list[nusers].ut_user;
178 utmp_idle[nusers].ui_utmp.ut_host =
179 utmp_list[nusers].ut_host;
185 ut.utmpidlearr_len = nusers;
190 rusers_num(void *argp __unused, struct svc_req *rqstp __unused)
192 static int num_users = 0;
196 while ((usr = getutxent()) != NULL) {
197 if (usr->ut_type != USER_PROCESS)
213 bzero((char *)&ut, sizeof(ut));
215 utidle = do_names_2();
217 ut.utmparr_len = utidle->utmpidlearr_len;
218 ut.utmparr_val = &old_utmp[0];
219 for (i = 0; i < ut.utmparr_len; i++)
220 bcopy(&utmp_idle[i].ui_utmp, &old_utmp[i],
221 sizeof(old_utmp[0]));
229 rusersproc_names_2_svc(void *argp __unused, struct svc_req *rqstp __unused)
232 return (do_names_2());
236 rusersproc_allnames_2_svc(void *argp __unused, struct svc_req *rqstp __unused)
239 return (do_names_2());
243 rusersproc_names_1_svc(void *argp __unused, struct svc_req *rqstp __unused)
246 return (do_names_1());
250 rusersproc_allnames_1_svc(void *argp __unused, struct svc_req *rqstp __unused)
253 return (do_names_1());
256 typedef void *(*rusersproc_t)(void *, struct svc_req *);
259 rusers_service(struct svc_req *rqstp, SVCXPRT *transp)
265 xdrproc_t xdr_argument, xdr_result;
268 switch (rqstp->rq_proc) {
270 (void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
274 xdr_argument = (xdrproc_t)xdr_void;
275 xdr_result = (xdrproc_t)xdr_int;
276 local = (rusersproc_t)rusers_num;
279 case RUSERSPROC_NAMES:
280 xdr_argument = (xdrproc_t)xdr_void;
281 xdr_result = (xdrproc_t)xdr_utmpidlearr;
282 switch (rqstp->rq_vers) {
283 case RUSERSVERS_ORIG:
284 local = (rusersproc_t)rusersproc_names_1_svc;
286 case RUSERSVERS_IDLE:
287 local = (rusersproc_t)rusersproc_names_2_svc;
290 svcerr_progvers(transp, RUSERSVERS_ORIG, RUSERSVERS_IDLE);
296 case RUSERSPROC_ALLNAMES:
297 xdr_argument = (xdrproc_t)xdr_void;
298 xdr_result = (xdrproc_t)xdr_utmpidlearr;
299 switch (rqstp->rq_vers) {
300 case RUSERSVERS_ORIG:
301 local = (rusersproc_t)rusersproc_allnames_1_svc;
303 case RUSERSVERS_IDLE:
304 local = (rusersproc_t)rusersproc_allnames_2_svc;
307 svcerr_progvers(transp, RUSERSVERS_ORIG, RUSERSVERS_IDLE);
314 svcerr_noproc(transp);
317 bzero(&argument, sizeof(argument));
318 if (!svc_getargs(transp, (xdrproc_t)xdr_argument, &argument)) {
319 svcerr_decode(transp);
322 result = (*local)(&argument, rqstp);
323 if (result != NULL &&
324 !svc_sendreply(transp, (xdrproc_t)xdr_result, result)) {
325 svcerr_systemerr(transp);
327 if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, &argument)) {
328 syslog(LOG_ERR, "unable to free arguments");