Update.
[glibc/history.git] / login / pututline_r.c
blob0b819000d13c4a76b96e33d12546016a28881cc8
1 /* Copyright (C) 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
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
17 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <alloca.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <utmp.h>
26 #include <sys/file.h>
27 #include <sys/stat.h>
30 /* XXX An alternative solution would be to call a SUID root program
31 which write the new value. */
33 int
34 __pututline_r (const struct utmp *id, struct utmp_data *utmp_data)
36 struct stat st;
37 int result = 0;
39 #if _HAVE_UT_TYPE - 0
40 /* Test whether ID has any of the legal types because we have to
41 prevent illegal entries. */
42 if (id->ut_type != RUN_LVL && id->ut_type != BOOT_TIME
43 && id->ut_type != OLD_TIME && id->ut_type != NEW_TIME
44 && id->ut_type != INIT_PROCESS && id->ut_type != LOGIN_PROCESS
45 && id->ut_type != USER_PROCESS && id->ut_type != DEAD_PROCESS)
46 /* No, using '<' and '>' for the test is not possible. */
48 errno = EINVAL;
49 return -1;
51 #endif
53 /* Open utmp file if not already done. */
54 if (utmp_data->ut_fd == -1)
56 setutent_r (utmp_data);
57 if (utmp_data->ut_fd == -1)
58 return -1;
61 #if _HAVE_UT_ID - 0
62 /* Check whether we need to reposition. Repositioning is necessary
63 either if the data in UTMP_DATA is not valid or if the ids don't
64 match: */
65 if (id->ut_id[0]
66 && (utmp_data->loc_utmp < (off_t) sizeof (struct utmp)
67 || strncmp(utmp_data->ubuf.ut_id, id->ut_id, sizeof (id->ut_id)) != 0))
69 /* We must not overwrite the data in UTMP_DATA since ID may be
70 aliasing it. */
71 struct utmp_data *data_tmp = alloca (sizeof (*data_tmp));
72 struct utmp *dummy;
74 *data_tmp = *utmp_data;
75 utmp_data = data_tmp;
77 if (getutid_r (id, &dummy, utmp_data) < 0)
79 if (errno != ESRCH)
80 /* Some error occured. If no entry was found, the position
81 pointer now is at the end of the file. */
82 return -1;
84 /* Set position pointer to position behind the record. */
85 utmp_data->loc_utmp += sizeof (struct utmp);
88 #endif
90 /* Try to lock the file. */
91 if (flock (utmp_data->ut_fd, LOCK_EX | LOCK_NB) < 0 && errno != ENOSYS)
93 /* Oh, oh. The file is already locked. Wait a bit and try again. */
94 sleep (1);
96 /* This time we ignore the error. */
97 (void) flock (utmp_data->ut_fd, LOCK_EX | LOCK_NB);
100 /* Find out how large the file is. */
101 result = fstat (utmp_data->ut_fd, &st);
103 if (result >= 0)
104 /* Position file correctly. */
105 if (utmp_data->loc_utmp < (off_t) sizeof (struct utmp))
106 /* Not located at any valid entry. Add at the end. */
108 result = lseek (utmp_data->ut_fd, 0L, SEEK_END);
109 if (result >= 0)
110 /* Where we'll be if the write succeeds. */
111 utmp_data->loc_utmp = st.st_size + sizeof (struct utmp);
113 else if (utmp_data->loc_utmp <= st.st_size)
114 result =
115 lseek (utmp_data->ut_fd, utmp_data->loc_utmp - sizeof (struct utmp),
116 SEEK_SET);
118 if (result >= 0)
119 /* Write the new data. */
120 if (write (utmp_data->ut_fd, id, sizeof (struct utmp))
121 != sizeof (struct utmp))
123 /* If we appended a new record this is only partially written.
124 Remove it. */
125 if (utmp_data->loc_utmp > st.st_size)
127 (void) ftruncate (utmp_data->ut_fd, st.st_size);
128 utmp_data->loc_utmp = st.st_size;
131 result = -1;
134 /* And unlock the file. */
135 (void) flock (utmp_data->ut_fd, LOCK_UN);
137 return result;
139 weak_alias (__pututline_r, pututline_r)