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 * 4. 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
29 * $FreeBSD: src/libexec/rpc.rusersd/rusers_proc.c,v 1.10 1999/08/28 00:09:56 peter Exp $
38 #include <sys/param.h>
45 #include <X11/extensions/xidle.h>
48 #include <rpcsvc/rnusers.h>
51 #define IGNOREUSER "sleeper"
54 #define _PATH_UTMP UTMP_FILE
58 #define _PATH_UTMP "/etc/utmp"
62 #define _PATH_DEV "/dev"
66 #define UT_LINESIZE sizeof(((struct utmp *)0)->ut_line)
69 #define UT_NAMESIZE sizeof(((struct utmp *)0)->ut_name)
72 #define UT_HOSTSIZE sizeof(((struct utmp *)0)->ut_host)
75 typedef char ut_line_t[UT_LINESIZE+1];
76 typedef char ut_name_t[UT_NAMESIZE+1];
77 typedef char ut_host_t[UT_HOSTSIZE+1];
79 utmpidle utmp_idle[MAXUSERS];
80 rutmp old_utmp[MAXUSERS];
81 ut_line_t line[MAXUSERS];
82 ut_name_t name[MAXUSERS];
83 ut_host_t host[MAXUSERS];
85 extern int from_inetd;
92 static jmp_buf openAbort;
97 longjmp (openAbort, 1);
100 XqueryIdle(char *display)
102 int first_event, first_error;
105 (void) signal (SIGALRM, abortOpen);
106 (void) alarm ((unsigned) 10);
107 if (!setjmp (openAbort)) {
108 if (!(dpy= XOpenDisplay(display))) {
109 syslog(LOG_ERR, "Cannot open display %s", display);
112 if (XidleQueryExtension(dpy, &first_event, &first_error)) {
113 if (!XGetIdleTime(dpy, &IdleTime)) {
114 syslog(LOG_ERR, "%s: unable to get idle time", display);
119 syslog(LOG_ERR, "%s: Xidle extension not loaded", display);
125 syslog(LOG_ERR, "%s: server grabbed for over 10 seconds", display);
128 (void) signal (SIGALRM, SIG_DFL);
129 (void) alarm ((unsigned) 0);
132 return((IdleTime + 30) / 60);
137 getidle(char *tty, char *display)
140 char devname[PATH_MAX];
145 * If this is an X terminal or console, then try the
149 if (display && *display && (idle = XqueryIdle(display)) >= 0)
154 u_long kbd_idle, mouse_idle;
155 #if !defined(__DragonFly__) && !defined(__FreeBSD__)
156 kbd_idle = getidle("kbd", NULL);
158 kbd_idle = getidle("vga", NULL);
160 mouse_idle = getidle("mouse", NULL);
161 idle = (kbd_idle < mouse_idle)?kbd_idle:mouse_idle;
164 sprintf(devname, "%s/%s", _PATH_DEV, tty);
165 if (stat(devname, &st) < 0) {
167 printf("%s: %s\n", devname, strerror(errno));
173 printf("%s: now=%d atime=%d\n", devname, now,
176 idle = now - st.st_atime;
177 idle = (idle + 30) / 60; /* secs->mins */
179 if (idle < 0) idle = 0;
187 static utmpidlearr ut;
191 bzero((char *)&ut, sizeof(ut));
192 ut.utmpidlearr_val = &utmp_idle[0];
194 ufp = fopen(_PATH_UTMP, "r");
196 syslog(LOG_ERR, "%m");
200 /* only entries with both name and line fields */
201 while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1 &&
203 if (*usr.ut_name && *usr.ut_line &&
204 strncmp(usr.ut_name, IGNOREUSER,
207 && usr.ut_type == USER_PROCESS
210 utmp_idle[nusers].ui_utmp.ut_time =
212 utmp_idle[nusers].ui_idle =
213 getidle(usr.ut_line, usr.ut_host);
214 utmp_idle[nusers].ui_utmp.ut_line = line[nusers];
215 strncpy(line[nusers], usr.ut_line, UT_LINESIZE);
216 utmp_idle[nusers].ui_utmp.ut_name = name[nusers];
217 strncpy(name[nusers], usr.ut_name, UT_NAMESIZE);
218 utmp_idle[nusers].ui_utmp.ut_host = host[nusers];
219 strncpy(host[nusers], usr.ut_host, UT_HOSTSIZE);
221 /* Make sure entries are NUL terminated */
222 line[nusers][UT_LINESIZE] =
223 name[nusers][UT_NAMESIZE] =
224 host[nusers][UT_HOSTSIZE] = '\0';
228 ut.utmpidlearr_len = nusers;
236 static int num_users = 0;
239 ufp = fopen(_PATH_UTMP, "r");
241 syslog(LOG_ERR, "%m");
245 /* only entries with both name and line fields */
246 while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
247 if (*usr.ut_name && *usr.ut_line &&
248 strncmp(usr.ut_name, IGNOREUSER,
251 && usr.ut_type == USER_PROCESS
268 bzero((char *)&ut, sizeof(ut));
270 utidle = do_names_2(all);
272 ut.utmparr_len = utidle->utmpidlearr_len;
273 ut.utmparr_val = &old_utmp[0];
274 for (i = 0; i < ut.utmparr_len; i++)
275 bcopy(&utmp_idle[i].ui_utmp, &old_utmp[i],
276 sizeof(old_utmp[0]));
284 rusersproc_names_2_svc(void *argp, struct svc_req *rqstp)
286 return(do_names_2(0));
290 rusersproc_allnames_2_svc(void *argp, struct svc_req *rqstp)
292 return(do_names_2(1));
296 rusersproc_names_1_svc(void *argp, struct svc_req *rqstp)
298 return(do_names_1(0));
302 rusersproc_allnames_1_svc(void *argp, struct svc_req *rqstp)
304 return(do_names_1(1));
308 rusers_service(struct svc_req *rqstp, SVCXPRT *transp)
314 bool_t (*xdr_argument)(), (*xdr_result)();
317 switch (rqstp->rq_proc) {
319 (void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
323 xdr_argument = xdr_void;
324 xdr_result = xdr_int;
325 local = (char *(*)()) rusers_num;
328 case RUSERSPROC_NAMES:
329 xdr_argument = xdr_void;
330 xdr_result = xdr_utmpidlearr;
331 switch (rqstp->rq_vers) {
332 case RUSERSVERS_ORIG:
333 local = (char *(*)()) rusersproc_names_1_svc;
335 case RUSERSVERS_IDLE:
336 local = (char *(*)()) rusersproc_names_2_svc;
339 svcerr_progvers(transp, RUSERSVERS_ORIG, RUSERSVERS_IDLE);
345 case RUSERSPROC_ALLNAMES:
346 xdr_argument = xdr_void;
347 xdr_result = xdr_utmpidlearr;
348 switch (rqstp->rq_vers) {
349 case RUSERSVERS_ORIG:
350 local = (char *(*)()) rusersproc_allnames_1_svc;
352 case RUSERSVERS_IDLE:
353 local = (char *(*)()) rusersproc_allnames_2_svc;
356 svcerr_progvers(transp, RUSERSVERS_ORIG, RUSERSVERS_IDLE);
363 svcerr_noproc(transp);
366 bzero((char *)&argument, sizeof(argument));
367 if (!svc_getargs(transp, (xdrproc_t)xdr_argument, (caddr_t)&argument)) {
368 svcerr_decode(transp);
371 result = (*local)(&argument, rqstp);
372 if (result != NULL &&
373 !svc_sendreply(transp, (xdrproc_t)xdr_result, result)) {
374 svcerr_systemerr(transp);
376 if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, (caddr_t)&argument)) {
377 syslog(LOG_ERR, "unable to free arguments");