1 /* Copyright (C) 1997 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
= INT_MIN
;
37 /* Functions defined here. */
38 static int setutent_daemon (int reset
);
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_endutent (int sock
);
63 static int do_getutline (int sock
, const struct utmp
*line
,
65 static int do_getutid (int sock
, const struct utmp
*id
,
67 static int do_pututline (int sock
, const struct utmp
*utmp
);
68 static int do_updwtmp (int sock
, const char *file
,
69 const struct utmp
*utmp
);
71 static int open_socket (const char *name
);
72 static int send_request (int sock
, const request_header
*request
,
77 setutent_daemon (int reset
)
79 if (daemon_sock
== INT_MIN
)
81 daemon_sock
= open_socket (_PATH_UTMPD_RW
);
84 /* Hhm, read-write access did not work. Try read-only. */
85 daemon_sock
= open_socket (_PATH_UTMPD_RO
);
90 /* Send request to the daemon. */
91 if (do_setutent (daemon_sock
) < 0)
96 /* Send request to the daemon. */
97 if (do_setutent (daemon_sock
) < 0)
106 endutent_daemon (void)
108 if (daemon_sock
>= 0)
110 /* Send request to the daemon. */
111 do_endutent (daemon_sock
);
115 daemon_sock
= INT_MIN
;
120 getutent_r_daemon (struct utmp
*buffer
, struct utmp
**result
)
122 /* Open connection if not already done. */
123 if (daemon_sock
== INT_MIN
)
133 /* Send request to the daemon. */
134 if (do_getutent (daemon_sock
, buffer
) < 0)
146 getutline_r_daemon (const struct utmp
*line
, struct utmp
*buffer
,
147 struct utmp
**result
)
155 /* Send request to the daemon. */
156 if (do_getutline (daemon_sock
, line
, buffer
) < 0)
168 getutid_r_daemon (const struct utmp
*id
, struct utmp
*buffer
,
169 struct utmp
**result
)
177 /* Send request to the daemon. */
178 if (do_getutid (daemon_sock
, id
, buffer
) < 0)
190 pututline_daemon (const struct utmp
*utmp
)
192 if (daemon_sock
== INT_MIN
)
193 /* The connection is closed. Open it again. */
197 /* Something went wrong. */
200 /* Send request to the daemon. */
201 if (do_pututline (daemon_sock
, utmp
) < 0)
204 return (struct utmp
*)utmp
;
209 updwtmp_daemon (const char *file
, const struct utmp
*utmp
)
213 /* Only try to open for both reading and writing. */
214 sock
= open_socket (_PATH_UTMPD_RW
);
218 /* Send request to the daemon. */
219 if (do_updwtmp (sock
, file
, utmp
) < 0)
228 do_setutent (int sock
)
230 setutent_request
*request
;
231 setutent_reply reply
;
234 size
= sizeof (setutent_request
) + strlen (__libc_utmp_file_name
) + 1;
236 request
= malloc (size
);
240 request
->header
.version
= UTMPD_VERSION
;
241 request
->header
.size
= size
;
242 request
->header
.type
= UTMPD_REQ_SETUTENT
;
243 strcpy (request
->file
, __libc_utmp_file_name
);
245 reply
.header
.version
= UTMPD_VERSION
;
246 reply
.header
.size
= sizeof (setutent_reply
);
247 reply
.header
.type
= UTMPD_REQ_SETUTENT
;
249 if (send_request (sock
, &request
->header
, &reply
.header
) < 0)
255 if (reply
.result
< 0)
256 __set_errno (reply
.errnum
);
263 do_getutent (int sock
, struct utmp
*buffer
)
265 getutent_request request
;
266 getutent_reply reply
;
268 request
.header
.version
= UTMPD_VERSION
;
269 request
.header
.size
= sizeof (getutent_request
);
270 request
.header
.type
= UTMPD_REQ_GETUTENT
;
272 reply
.header
.version
= UTMPD_VERSION
;
273 reply
.header
.size
= sizeof (getutent_reply
);
274 reply
.header
.type
= UTMPD_REQ_GETUTENT
;
276 if (send_request (sock
, &request
.header
, &reply
.header
) < 0)
279 if (reply
.result
< 0)
280 __set_errno (reply
.errnum
);
282 memcpy (buffer
, &reply
.entry
, sizeof (struct utmp
));
288 do_endutent (int sock
)
290 endutent_request request
;
291 endutent_reply reply
;
293 request
.header
.version
= UTMPD_VERSION
;
294 request
.header
.size
= sizeof (endutent_request
);
295 request
.header
.type
= UTMPD_REQ_ENDUTENT
;
297 reply
.header
.version
= UTMPD_VERSION
;
298 reply
.header
.size
= sizeof (endutent_reply
);
299 reply
.header
.type
= UTMPD_REQ_ENDUTENT
;
301 if (send_request (sock
, &request
.header
, &reply
.header
) < 0)
304 if (reply
.result
< 0)
305 __set_errno (reply
.errnum
);
311 do_getutline (int sock
, const struct utmp
*line
, struct utmp
*buffer
)
313 getutline_request request
;
314 getutline_reply reply
;
316 request
.header
.version
= UTMPD_VERSION
;
317 request
.header
.size
= sizeof (getutline_request
);
318 request
.header
.type
= UTMPD_REQ_GETUTLINE
;
319 memcpy (&request
.line
, line
, sizeof (struct utmp
));
321 reply
.header
.version
= UTMPD_VERSION
;
322 reply
.header
.size
= sizeof (getutline_reply
);
323 reply
.header
.type
= UTMPD_REQ_GETUTLINE
;
325 if (send_request (sock
, &request
.header
, &reply
.header
) < 0)
328 if (reply
.result
< 0)
329 __set_errno (reply
.errnum
);
331 memcpy (buffer
, &reply
.entry
, sizeof (struct utmp
));
337 do_getutid (int sock
, const struct utmp
*id
, struct utmp
*buffer
)
339 getutid_request request
;
342 request
.header
.version
= UTMPD_VERSION
;
343 request
.header
.size
= sizeof (getutid_request
);
344 request
.header
.type
= UTMPD_REQ_GETUTID
;
345 memcpy (&request
.id
, id
, sizeof (struct utmp
));
347 reply
.header
.version
= UTMPD_VERSION
;
348 reply
.header
.size
= sizeof (getutid_reply
);
349 reply
.header
.type
= UTMPD_REQ_GETUTID
;
351 if (send_request (sock
, &request
.header
, &reply
.header
) < 0)
354 if (reply
.result
< 0)
355 __set_errno (reply
.errnum
);
357 memcpy (buffer
, &reply
.entry
, sizeof (struct utmp
));
363 do_pututline (int sock
, const struct utmp
*utmp
)
365 pututline_request request
;
366 pututline_reply reply
;
368 request
.header
.version
= UTMPD_VERSION
;
369 request
.header
.size
= sizeof (pututline_request
);
370 request
.header
.type
= UTMPD_REQ_PUTUTLINE
;
371 memcpy (&request
.utmp
, utmp
, sizeof (struct utmp
));
373 reply
.header
.version
= UTMPD_VERSION
;
374 reply
.header
.size
= sizeof (pututline_reply
);
375 reply
.header
.type
= UTMPD_REQ_PUTUTLINE
;
377 if (send_request (sock
, &request
.header
, &reply
.header
) < 0)
380 if (reply
.result
< 0)
381 __set_errno (reply
.errnum
);
387 do_updwtmp (int sock
, const char *file
, const struct utmp
*utmp
)
389 updwtmp_request
*request
;
393 size
= sizeof (updwtmp_request
) + strlen (file
) + 1;
395 request
= malloc (size
);
399 request
->header
.version
= UTMPD_VERSION
;
400 request
->header
.size
= size
;
401 request
->header
.type
= UTMPD_REQ_UPDWTMP
;
402 memcpy (&request
->utmp
, utmp
, sizeof (struct utmp
));
403 strcpy (request
->file
, file
);
405 reply
.header
.version
= UTMPD_VERSION
;
406 reply
.header
.size
= sizeof (updwtmp_reply
);
407 reply
.header
.type
= UTMPD_REQ_UPDWTMP
;
409 if (send_request (sock
, &request
->header
, &reply
.header
) < 0)
415 if (reply
.result
< 0)
416 __set_errno (reply
.errnum
);
423 /* Create a socket connected to NAME. */
425 open_socket (const char *name
)
427 struct sockaddr_un addr
;
430 sock
= socket (PF_UNIX
, SOCK_STREAM
, 0);
434 addr
.sun_family
= AF_UNIX
;
435 strcpy (addr
.sun_path
, name
);
436 if (connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)) < 0)
445 /* Send REQUEST to SOCK, and wait for reply. Returns 0 if successful,
446 storing the reply in REPLY, and -1 if not. */
448 send_request (int sock
, const request_header
*request
,
454 nbytes
= write (sock
, request
, request
->size
);
455 if (nbytes
!= (ssize_t
) request
->size
)
458 nbytes
= read (sock
, &header
, sizeof (reply_header
));
459 if (nbytes
!= sizeof (reply_header
))
462 if (reply
->version
!= header
.version
463 || reply
->size
!= header
.size
464 || reply
->type
!= header
.type
)
467 nbytes
= read (sock
, reply
+ 1, reply
->size
- sizeof (reply_header
));
468 if (nbytes
!= (ssize_t
) (reply
->size
- sizeof (reply_header
)))