1 /* Copyright (C) 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
24 #include <sys/socket.h>
29 #include "utmp-private.h"
30 #include "programs/utmpd.h"
33 /* Descriptor for the socket. */
34 static int daemon_sock = -1;
37 /* Functions defined here. */
38 static int setutent_daemon (void);
39 static int getutent_r_daemon (struct utmp *buffer, struct utmp **result);
40 static int getutid_r_daemon (const struct utmp *line, struct utmp *buffer,
41 struct utmp **result);
42 static int getutline_r_daemon (const struct utmp *id, struct utmp *buffer,
43 struct utmp **result);
44 static struct utmp *pututline_daemon (const struct utmp *utmp);
45 static void endutent_daemon (void);
46 static int updwtmp_daemon (const char *file, const struct utmp *utmp);
48 /* Jump table for daemon functions. */
49 struct utfuncs __libc_utmp_daemon_functions =
60 static int do_setutent (int sock);
61 static int do_getutent (int sock, struct utmp *buffer);
62 static int do_getutid (int sock, const struct utmp *id,
64 static int do_pututline (int sock, const struct utmp *utmp);
65 static int do_getutline (int sock, const struct utmp *line,
67 static int do_pututline (int sock, const struct utmp *utmp);
68 static int do_endutent (int sock);
69 static int do_updwtmp (int sock, const char *file,
70 const struct utmp *utmp);
72 static int open_socket (const char *name);
73 static int send_request (int sock, const request_header *request,
78 setutent_daemon (void)
80 if (access (_PATH_UTMPD_RW, F_OK) == -1
81 && access (_PATH_UTMPD_RO, F_OK) == -1)
86 daemon_sock = open_socket (_PATH_UTMPD_RW);
89 /* Hhm, read-write access did not work. Try read-only. */
90 daemon_sock = open_socket (_PATH_UTMPD_RO);
96 /* Send request to the daemon. */
97 if (do_setutent (daemon_sock) < 0)
105 getutent_r_daemon (struct utmp *buffer, struct utmp **result)
107 assert (daemon_sock >= 0);
109 /* Send request to the daemon. */
110 if (do_getutent (daemon_sock, buffer) < 0)
122 getutid_r_daemon (const struct utmp *id, struct utmp *buffer,
123 struct utmp **result)
125 assert (daemon_sock >= 0);
127 /* Send request to the daemon. */
128 if (do_getutid (daemon_sock, id, buffer) < 0)
140 getutline_r_daemon (const struct utmp *line, struct utmp *buffer,
141 struct utmp **result)
143 assert (daemon_sock >= 0);
145 /* Send request to the daemon. */
146 if (do_getutline (daemon_sock, line, buffer) < 0)
158 pututline_daemon (const struct utmp *utmp)
160 assert (daemon_sock >= 0);
162 /* Send request to the daemon. */
163 if (do_pututline (daemon_sock, utmp) < 0)
166 return (struct utmp *)utmp;
171 endutent_daemon (void)
173 assert (daemon_sock >= 0);
175 /* Send request to the daemon. */
176 do_endutent (daemon_sock);
184 updwtmp_daemon (const char *file, const struct utmp *utmp)
188 /* Only try to open for both reading and writing. */
189 sock = open_socket (_PATH_UTMPD_RW);
193 /* Send request to the daemon. */
194 if (do_updwtmp (sock, file, utmp) < 0)
206 do_setutent (int sock)
208 setutent_request *request;
209 setutent_reply reply;
213 name_len = strlen (__libc_utmp_file_name) + 1;
214 size = sizeof (setutent_request) + name_len;
216 request = malloc (size);
220 request->header.version = UTMPD_VERSION;
221 request->header.size = size;
222 request->header.type = UTMPD_REQ_SETUTENT;
223 memcpy (request->file, __libc_utmp_file_name, name_len);
225 reply.header.version = UTMPD_VERSION;
226 reply.header.size = sizeof (setutent_reply);
227 reply.header.type = UTMPD_REQ_SETUTENT;
229 if (send_request (sock, &request->header, &reply.header) < 0)
235 if (reply.result < 0)
236 __set_errno (reply.errnum);
243 do_getutent (int sock, struct utmp *buffer)
245 getutent_request request;
246 getutent_reply reply;
248 request.header.version = UTMPD_VERSION;
249 request.header.size = sizeof (getutent_request);
250 request.header.type = UTMPD_REQ_GETUTENT;
252 reply.header.version = UTMPD_VERSION;
253 reply.header.size = sizeof (getutent_reply);
254 reply.header.type = UTMPD_REQ_GETUTENT;
256 if (send_request (sock, &request.header, &reply.header) < 0)
259 if (reply.result < 0)
260 __set_errno (reply.errnum);
262 memcpy (buffer, &reply.entry, sizeof (struct utmp));
268 do_getutid (int sock, const struct utmp *id, struct utmp *buffer)
270 getutid_request request;
273 request.header.version = UTMPD_VERSION;
274 request.header.size = sizeof (getutid_request);
275 request.header.type = UTMPD_REQ_GETUTID;
276 memcpy (&request.id, id, sizeof (struct utmp));
278 reply.header.version = UTMPD_VERSION;
279 reply.header.size = sizeof (getutid_reply);
280 reply.header.type = UTMPD_REQ_GETUTID;
282 if (send_request (sock, &request.header, &reply.header) < 0)
285 if (reply.result < 0)
286 __set_errno (reply.errnum);
288 memcpy (buffer, &reply.entry, sizeof (struct utmp));
294 do_getutline (int sock, const struct utmp *line, struct utmp *buffer)
296 getutline_request request;
297 getutline_reply reply;
299 request.header.version = UTMPD_VERSION;
300 request.header.size = sizeof (getutline_request);
301 request.header.type = UTMPD_REQ_GETUTLINE;
302 memcpy (&request.line, line, sizeof (struct utmp));
304 reply.header.version = UTMPD_VERSION;
305 reply.header.size = sizeof (getutline_reply);
306 reply.header.type = UTMPD_REQ_GETUTLINE;
308 if (send_request (sock, &request.header, &reply.header) < 0)
311 if (reply.result < 0)
312 __set_errno (reply.errnum);
314 memcpy (buffer, &reply.entry, sizeof (struct utmp));
320 do_pututline (int sock, const struct utmp *utmp)
322 pututline_request request;
323 pututline_reply reply;
325 request.header.version = UTMPD_VERSION;
326 request.header.size = sizeof (pututline_request);
327 request.header.type = UTMPD_REQ_PUTUTLINE;
328 memcpy (&request.utmp, utmp, sizeof (struct utmp));
330 reply.header.version = UTMPD_VERSION;
331 reply.header.size = sizeof (pututline_reply);
332 reply.header.type = UTMPD_REQ_PUTUTLINE;
334 if (send_request (sock, &request.header, &reply.header) < 0)
337 if (reply.result < 0)
338 __set_errno (reply.errnum);
344 do_endutent (int sock)
346 endutent_request request;
347 endutent_reply reply;
349 request.header.version = UTMPD_VERSION;
350 request.header.size = sizeof (endutent_request);
351 request.header.type = UTMPD_REQ_ENDUTENT;
353 reply.header.version = UTMPD_VERSION;
354 reply.header.size = sizeof (endutent_reply);
355 reply.header.type = UTMPD_REQ_ENDUTENT;
357 if (send_request (sock, &request.header, &reply.header) < 0)
360 if (reply.result < 0)
361 __set_errno (reply.errnum);
367 do_updwtmp (int sock, const char *file, const struct utmp *utmp)
369 updwtmp_request *request;
374 file_len = strlen (file) + 1;
375 size = sizeof (updwtmp_request) + file_len;
377 request = malloc (size);
381 request->header.version = UTMPD_VERSION;
382 request->header.size = size;
383 request->header.type = UTMPD_REQ_UPDWTMP;
384 memcpy (&request->utmp, utmp, sizeof (struct utmp));
385 memcpy (request->file, file, file_len);
387 reply.header.version = UTMPD_VERSION;
388 reply.header.size = sizeof (updwtmp_reply);
389 reply.header.type = UTMPD_REQ_UPDWTMP;
391 if (send_request (sock, &request->header, &reply.header) < 0)
397 if (reply.result < 0)
398 __set_errno (reply.errnum);
405 /* Create a socket connected to NAME. */
407 open_socket (const char *name)
409 struct sockaddr_un addr;
412 sock = __socket (PF_UNIX, SOCK_STREAM, 0);
416 addr.sun_family = AF_UNIX;
417 strcpy (addr.sun_path, name);
418 if (__connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
427 /* Send REQUEST to SOCK, and wait for reply. Returns 0 if successful,
428 storing the reply in REPLY, and -1 if not. */
430 send_request (int sock, const request_header *request,
436 nbytes = write (sock, request, request->size);
437 if (nbytes != (ssize_t) request->size)
440 nbytes = read (sock, &header, sizeof (reply_header));
441 if (nbytes != sizeof (reply_header))
444 if (reply->version != header.version
445 || reply->size != header.size
446 || reply->type != header.type)
449 nbytes = read (sock, reply + 1, reply->size - sizeof (reply_header));
450 if (nbytes != (ssize_t) (reply->size - sizeof (reply_header)))