Wed May 29 12:53:10 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc.git] / login / pututline_r.c
blob07322e57404dadd0ed0b55b52884db1524b52c23
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>
29 /* XXX An alternative solution would be to call a SUID root program
30 which write the new value. */
32 int
33 pututline_r (const struct utmp *utmp_ptr, struct utmp_data *utmp_data)
35 struct stat st;
36 int result = 0;
38 #if _HAVE_UT_TYPE - 0
39 /* Test whether ID has any of the legal types because we have to
40 prevent illegal entries. */
41 if (id->ut_type != RUN_LVL && id->ut_type != BOOT_TIME
42 && id->ut_type != OLD_TIME && id->ut_type != NEW_TIME
43 && id->ut_type != INIT_PROCESS && id->ut_type != LOGIN_PROCESS
44 && id->ut_type != USER_PROCESS && id->ut_type != DEAD_PROCESS)
45 /* No, using '<' and '>' for the test is not possible. */
47 errno = EINVAL;
48 return -1;
50 #endif
52 /* Open utmp file if not already done. */
53 if (utmp_data->ut_fd == -1)
55 setutent_r (utmp_data);
56 if (utmp_data->ut_fd == -1)
57 return -1;
60 #if _HAVE_UT_TYPE - 0
61 /* Seek position to write. */
62 if (utmp_data->ubuf.ut_type != utmp_ptr->ut_type)
64 /* We must not overwrite the data in UTMP_DATA. */
65 struct utmp_data *data_tmp = alloca (sizeof (utmp_data));
66 struct utmp *dummy;
67 off_t new_pos;
69 *data_tmp = *utmp_data;
70 utmp_data = data_tmp;
72 if (getutid_r (utmp_ptr, &dummy, utmp_data) < 0)
74 if (errno != ESRCH)
75 /* Some error occured. If no entry was found, the position
76 pointer now is at the end of the file. */
77 return -1;
79 /* Set position pointer to position after adding of the record. */
80 utmp_data->loc_utmp += sizeof (struct utmp);
82 #endif
84 /* Find out how large the file is. */
85 if (fstat (utmp_data->ut_fd, &st) < 0)
86 return -1;
88 /* Position file correctly. */
89 if (utmp_data->loc_utmp <= st.st_size
90 && lseek (utmp_data->ut_fd, utmp_data->loc_utmp - sizeof (struct utmp),
91 SEEK_SET) < 0)
92 return -1;
94 /* Try to lock the file. */
95 if (flock (utmp_data->ut_fd, LOCK_EX | LOCK_NB) < 0 && errno != ENOSYS)
97 /* Oh, oh. The file is already locked. Wait a bit and try again. */
98 sleep (1);
100 /* This time we ignore the error. */
101 (void) flock (utmp_data->ut_fd, LOCK_EX | LOCK_NB);
104 /* Write the new data. */
105 if (write (utmp_data->ut_fd, utmp_ptr, sizeof (struct utmp))
106 != sizeof (struct utmp))
108 /* If we appended a new record this is only partially written.
109 Remove it. */
110 if (utmp_data->loc_utmp > st.st_size)
112 (void) ftruncate (utmp_data->ut_fd, st.st_size);
113 utmp_data->loc_utmp = st.st_size;
116 result = -1;
119 /* And unlock the file. */
120 (void) flock (utmp_data->ut_fd, LOCK_UN);
122 return result;