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
;
233 request
.header
.version
= UTMPD_VERSION
;
234 request
.header
.size
= sizeof (setutent_request
);
235 request
.header
.type
= UTMPD_REQ_SETUTENT
;
236 strncpy (request
.file
, __libc_utmp_file_name
, sizeof request
.file
);
238 reply
.header
.version
= UTMPD_VERSION
;
239 reply
.header
.size
= sizeof (setutent_reply
);
240 reply
.header
.type
= UTMPD_REQ_SETUTENT
;
242 if (send_request (sock
, &request
.header
, &reply
.header
) < 0)
245 if (reply
.result
< 0)
246 __set_errno (reply
.errnum
);
252 do_getutent (int sock
, struct utmp
*buffer
)
254 getutent_request request
;
255 getutent_reply reply
;
257 request
.header
.version
= UTMPD_VERSION
;
258 request
.header
.size
= sizeof (getutent_request
);
259 request
.header
.type
= UTMPD_REQ_GETUTENT
;
261 reply
.header
.version
= UTMPD_VERSION
;
262 reply
.header
.size
= sizeof (getutent_reply
);
263 reply
.header
.type
= UTMPD_REQ_GETUTENT
;
265 if (send_request (sock
, &request
.header
, &reply
.header
) < 0)
268 if (reply
.result
< 0)
269 __set_errno (reply
.errnum
);
271 memcpy (buffer
, &reply
.entry
, sizeof (struct utmp
));
277 do_endutent (int sock
)
279 endutent_request request
;
280 endutent_reply reply
;
282 request
.header
.version
= UTMPD_VERSION
;
283 request
.header
.size
= sizeof (endutent_request
);
284 request
.header
.type
= UTMPD_REQ_ENDUTENT
;
286 reply
.header
.version
= UTMPD_VERSION
;
287 reply
.header
.size
= sizeof (endutent_reply
);
288 reply
.header
.type
= UTMPD_REQ_ENDUTENT
;
290 if (send_request (sock
, &request
.header
, &reply
.header
) < 0)
293 if (reply
.result
< 0)
294 __set_errno (reply
.errnum
);
300 do_getutline (int sock
, const struct utmp
*line
, struct utmp
*buffer
)
302 getutline_request request
;
303 getutline_reply reply
;
305 request
.header
.version
= UTMPD_VERSION
;
306 request
.header
.size
= sizeof (getutline_request
);
307 request
.header
.type
= UTMPD_REQ_GETUTLINE
;
308 memcpy (&request
.line
, line
, sizeof (struct utmp
));
310 reply
.header
.version
= UTMPD_VERSION
;
311 reply
.header
.size
= sizeof (getutline_reply
);
312 reply
.header
.type
= UTMPD_REQ_GETUTLINE
;
314 if (send_request (sock
, &request
.header
, &reply
.header
) < 0)
317 if (reply
.result
< 0)
318 __set_errno (reply
.errnum
);
320 memcpy (buffer
, &reply
.entry
, sizeof (struct utmp
));
326 do_getutid (int sock
, const struct utmp
*id
, struct utmp
*buffer
)
328 getutid_request request
;
331 request
.header
.version
= UTMPD_VERSION
;
332 request
.header
.size
= sizeof (getutid_request
);
333 request
.header
.type
= UTMPD_REQ_GETUTID
;
334 memcpy (&request
.id
, id
, sizeof (struct utmp
));
336 reply
.header
.version
= UTMPD_VERSION
;
337 reply
.header
.size
= sizeof (getutid_reply
);
338 reply
.header
.type
= UTMPD_REQ_GETUTID
;
340 if (send_request (sock
, &request
.header
, &reply
.header
) < 0)
343 if (reply
.result
< 0)
344 __set_errno (reply
.errnum
);
346 memcpy (buffer
, &reply
.entry
, sizeof (struct utmp
));
352 do_pututline (int sock
, const struct utmp
*utmp
)
354 pututline_request request
;
355 pututline_reply reply
;
357 request
.header
.version
= UTMPD_VERSION
;
358 request
.header
.size
= sizeof (pututline_request
);
359 request
.header
.type
= UTMPD_REQ_PUTUTLINE
;
360 memcpy (&request
.utmp
, utmp
, sizeof (struct utmp
));
362 reply
.header
.version
= UTMPD_VERSION
;
363 reply
.header
.size
= sizeof (pututline_reply
);
364 reply
.header
.type
= UTMPD_REQ_PUTUTLINE
;
366 if (send_request (sock
, &request
.header
, &reply
.header
) < 0)
369 if (reply
.result
< 0)
370 __set_errno (reply
.errnum
);
376 do_updwtmp (int sock
, const char *file
, const struct utmp
*utmp
)
378 updwtmp_request request
;
381 request
.header
.version
= UTMPD_VERSION
;
382 request
.header
.size
= sizeof (updwtmp_request
);
383 request
.header
.type
= UTMPD_REQ_UPDWTMP
;
384 strncpy (request
.file
, file
, sizeof request
.file
);
385 memcpy (&request
.utmp
, utmp
, sizeof (struct utmp
));
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)
394 if (reply
.result
< 0)
395 __set_errno (reply
.errnum
);
401 /* Create a socket connected to NAME. */
403 open_socket (const char *name
)
405 struct sockaddr_un addr
;
408 sock
= socket (PF_UNIX
, SOCK_STREAM
, 0);
412 addr
.sun_family
= AF_UNIX
;
413 strcpy (addr
.sun_path
, name
);
414 if (connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)) < 0)
423 /* Send REQUEST to SOCK, and wait for reply. Returns 0 if successful,
424 storing the reply in REPLY, and -1 if not. */
426 send_request (int sock
, const request_header
*request
,
432 nbytes
= write (sock
, request
, request
->size
);
433 if (nbytes
!= (ssize_t
) request
->size
)
436 nbytes
= read (sock
, &header
, sizeof (reply_header
));
437 if (nbytes
!= sizeof (reply_header
))
440 if (reply
->version
!= header
.version
441 || reply
->size
!= header
.size
442 || reply
->type
!= header
.type
)
445 nbytes
= read (sock
, reply
+ 1, reply
->size
- sizeof (reply_header
));
446 if (nbytes
!= (ssize_t
) (reply
->size
- sizeof (reply_header
)))