1998-04-25  Mark Kettenis  <kettenis@phys.uva.nl>

	* sysdeps/generic/utmp_file.c (TIMEOUT): New macro.
	(timeout_handler): New function.
	(LOCK_FILE, UNLOCK_FILE): New macros.
	Implement file locking with timeout.
	(getutent_r_file, internal_getut_r, getutline_r_file,
	pututline_file, updwtmp_file): Use LOCK_FILE and UNLOCK_FILE for
	locking.
This commit is contained in:
Ulrich Drepper 1998-05-18 15:26:40 +00:00
parent 298f256698
commit efea715879
2 changed files with 63 additions and 54 deletions

View File

@ -1,3 +1,13 @@
1998-04-25 Mark Kettenis <kettenis@phys.uva.nl>
* sysdeps/generic/utmp_file.c (TIMEOUT): New macro.
(timeout_handler): New function.
(LOCK_FILE, UNLOCK_FILE): New macros.
Implement file locking with timeout.
(getutent_r_file, internal_getut_r, getutline_r_file,
pututline_file, updwtmp_file): Use LOCK_FILE and UNLOCK_FILE for
locking.
1998-05-18 11:38 Ulrich Drepper <drepper@cygnus.com> 1998-05-18 11:38 Ulrich Drepper <drepper@cygnus.com>
* iconvdata/run-iconv-test.sh: Extend library-path by iconvdata * iconvdata/run-iconv-test.sh: Extend library-path by iconvdata

View File

@ -21,6 +21,7 @@
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@ -37,6 +38,48 @@ static off_t file_offset;
static struct utmp last_entry; static struct utmp last_entry;
/* Locking timeout. */
#ifndef TIMEOUT
# define TIMEOUT 1
#endif
/* Do-nothing handler for locking timeout. */
static void timeout_handler (int signum) {};
#define LOCK_FILE(fd, type) \
{ \
struct flock fl; \
struct sigaction action, old_action; \
unsigned int old_timeout; \
\
/* Cancel any existing alarm. */ \
old_timeout = alarm (0); \
\
/* Establish signal handler. */ \
action.sa_handler = timeout_handler; \
sigemptyset (&action.sa_mask); \
action.sa_flags = 0; \
sigaction (SIGALRM, &action, &old_action); \
\
alarm (TIMEOUT); \
\
/* Try to get the lock. */ \
memset (&fl, '\0', sizeof (struct flock)); \
fl.l_type = (type); \
fl.l_whence = SEEK_SET; \
fcntl ((fd), F_SETLKW, &fl)
#define UNLOCK_FILE(fd) \
/* Unlock the file. */ \
fl.l_type = F_UNLCK; \
fcntl ((fd), F_SETLKW, &fl); \
\
/* Reset the signal handler and alarm. */ \
sigaction (SIGALRM, &old_action, NULL); \
alarm (old_timeout); \
} while (0)
/* Functions defined here. */ /* Functions defined here. */
static int setutent_file (void); static int setutent_file (void);
static int getutent_r_file (struct utmp *buffer, struct utmp **result); static int getutent_r_file (struct utmp *buffer, struct utmp **result);
@ -100,7 +143,6 @@ static int
getutent_r_file (struct utmp *buffer, struct utmp **result) getutent_r_file (struct utmp *buffer, struct utmp **result)
{ {
ssize_t nbytes; ssize_t nbytes;
struct flock fl; /* Information struct for locking. */
assert (file_fd >= 0); assert (file_fd >= 0);
@ -111,26 +153,12 @@ getutent_r_file (struct utmp *buffer, struct utmp **result)
return -1; return -1;
} }
/* XXX The following is not perfect. Instead of locking the file itself LOCK_FILE (file_fd, F_RDLCK);
Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl> suggests to
use an extra locking file. */
/* XXX I think using an extra locking file does not solve the
problems. Instead we should set an alarm, which causes fcntl to
fail, as in ../nis/lckcache.c.
Mark Kettenis <kettenis@phys.uva.nl>. */
/* Try to get the lock. */
memset (&fl, '\0', sizeof (struct flock));
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fcntl (file_fd, F_SETLK, &fl);
/* Read the next entry. */ /* Read the next entry. */
nbytes = read (file_fd, &last_entry, sizeof (struct utmp)); nbytes = read (file_fd, &last_entry, sizeof (struct utmp));
/* And unlock the file. */ UNLOCK_FILE (file_fd);
fl.l_type = F_UNLCK;
fcntl (file_fd, F_SETLKW, &fl);
if (nbytes != sizeof (struct utmp)) if (nbytes != sizeof (struct utmp))
{ {
@ -180,13 +208,8 @@ static int
internal_getut_r (const struct utmp *id, struct utmp *buffer) internal_getut_r (const struct utmp *id, struct utmp *buffer)
{ {
int result = -1; int result = -1;
struct flock fl;
/* Try to get the lock. */ LOCK_FILE (file_fd, F_RDLCK);
memset (&fl, '\0', sizeof (struct flock));
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fcntl (file_fd, F_SETLKW, &fl);
#if _HAVE_UT_TYPE - 0 #if _HAVE_UT_TYPE - 0
if (id->ut_type == RUN_LVL || id->ut_type == BOOT_TIME if (id->ut_type == RUN_LVL || id->ut_type == BOOT_TIME
@ -237,9 +260,7 @@ internal_getut_r (const struct utmp *id, struct utmp *buffer)
result = 0; result = 0;
unlock_return: unlock_return:
/* And unlock the file. */ UNLOCK_FILE (file_fd);
fl.l_type = F_UNLCK;
fcntl (file_fd, F_SETLK, &fl);
return result; return result;
} }
@ -278,8 +299,6 @@ static int
getutline_r_file (const struct utmp *line, struct utmp *buffer, getutline_r_file (const struct utmp *line, struct utmp *buffer,
struct utmp **result) struct utmp **result)
{ {
struct flock fl;
assert (file_fd >= 0); assert (file_fd >= 0);
if (file_offset == -1l) if (file_offset == -1l)
@ -288,11 +307,7 @@ getutline_r_file (const struct utmp *line, struct utmp *buffer,
return -1; return -1;
} }
/* Try to get the lock. */ LOCK_FILE (file_fd, F_RDLCK);
memset (&fl, '\0', sizeof (struct flock));
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fcntl (file_fd, F_SETLKW, &fl);
while (1) while (1)
{ {
@ -322,9 +337,7 @@ getutline_r_file (const struct utmp *line, struct utmp *buffer,
*result = buffer; *result = buffer;
unlock_return: unlock_return:
/* And unlock the file. */ UNLOCK_FILE (file_fd);
fl.l_type = F_UNLCK;
fcntl (file_fd, F_SETLK, &fl);
return ((*result == NULL) ? -1 : 0); return ((*result == NULL) ? -1 : 0);
} }
@ -333,7 +346,6 @@ unlock_return:
static struct utmp * static struct utmp *
pututline_file (const struct utmp *data) pututline_file (const struct utmp *data)
{ {
struct flock fl; /* Information struct for locking. */
struct utmp buffer; struct utmp buffer;
struct utmp *pbuf; struct utmp *pbuf;
int found; int found;
@ -356,11 +368,7 @@ pututline_file (const struct utmp *data)
else else
found = internal_getut_r (data, &buffer); found = internal_getut_r (data, &buffer);
/* Try to lock the file. */ LOCK_FILE (file_fd, F_WRLCK);
memset (&fl, '\0', sizeof (struct flock));
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fcntl (file_fd, F_SETLK, &fl);
if (found < 0) if (found < 0)
{ {
@ -401,9 +409,7 @@ pututline_file (const struct utmp *data)
} }
unlock_return: unlock_return:
/* And unlock the file. */ UNLOCK_FILE (file_fd);
fl.l_type = F_UNLCK;
fcntl (file_fd, F_SETLK, &fl);
return pbuf; return pbuf;
} }
@ -423,7 +429,6 @@ static int
updwtmp_file (const char *file, const struct utmp *utmp) updwtmp_file (const char *file, const struct utmp *utmp)
{ {
int result = -1; int result = -1;
struct flock fl;
off_t offset; off_t offset;
int fd; int fd;
@ -432,11 +437,7 @@ updwtmp_file (const char *file, const struct utmp *utmp)
if (fd < 0) if (fd < 0)
return -1; return -1;
/* Try to get the lock. */ LOCK_FILE (fd, F_WRLCK);
memset (&fl, '\0', sizeof (struct flock));
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fcntl (fd, F_SETLK, &fl);
/* Remember original size of log file. */ /* Remember original size of log file. */
offset = lseek (fd, 0, SEEK_END); offset = lseek (fd, 0, SEEK_END);
@ -461,9 +462,7 @@ updwtmp_file (const char *file, const struct utmp *utmp)
result = 0; result = 0;
unlock_return: unlock_return:
/* And unlock the file. */ UNLOCK_FILE (fd);
fl.l_type = F_UNLCK;
fcntl (fd, F_SETLKW, &fl);
/* Close WTMP file. */ /* Close WTMP file. */
close (fd); close (fd);