2013-10-10 01:47:09 -04:00
|
|
|
/* pklock.c -- implements pklock.h */
|
|
|
|
#include "estruct.h"
|
2013-05-20 01:16:08 -04:00
|
|
|
#include "pklock.h"
|
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/* PKLOCK.C
|
|
|
|
*
|
|
|
|
* locking routines as modified by Petri Kutvonen
|
|
|
|
*/
|
2005-09-30 18:26:09 -04:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
#if (FILOCK && BSD) || SVR4
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#ifdef SVR4
|
|
|
|
#include <string.h>
|
|
|
|
#else
|
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include <errno.h>
|
|
|
|
|
2019-06-19 07:49:20 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
2016-03-19 02:36:27 -04:00
|
|
|
/* Maximum file length name 255 */
|
|
|
|
#define MAXLOCK 256
|
2005-05-31 11:50:56 -04:00
|
|
|
#define MAXNAME 128
|
|
|
|
|
|
|
|
#if defined(SVR4) && ! defined(__linux__)
|
|
|
|
#include <sys/systeminfo.h>
|
|
|
|
|
|
|
|
int gethostname(char *name, int namelen)
|
|
|
|
{
|
2010-08-29 06:03:55 -04:00
|
|
|
return sysinfo(SI_HOSTNAME, name, namelen);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-05-06 22:50:09 -04:00
|
|
|
char *cuserid( char *retbuf) ; /* should have been declared in stdio.h */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
*
|
|
|
|
* if successful, returns NULL
|
|
|
|
* if file locked, returns username of person locking the file
|
|
|
|
* if other error, returns "LOCK ERROR: explanation"
|
|
|
|
*
|
|
|
|
*********************/
|
2013-09-20 22:24:45 -04:00
|
|
|
char *dolock( const char *fname)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2005-10-01 03:22:55 -04:00
|
|
|
int fd, n;
|
2016-03-19 02:36:27 -04:00
|
|
|
char lname[ MAXLOCK] ;
|
|
|
|
static char locker[ MAXNAME + 1] ;
|
2005-05-31 11:50:56 -04:00
|
|
|
int mask;
|
|
|
|
struct stat sbuf;
|
2005-09-30 18:26:09 -04:00
|
|
|
|
2019-06-19 07:49:20 -04:00
|
|
|
mystrscpy( lname, fname, sizeof lname - 6) ;
|
2016-03-19 02:36:27 -04:00
|
|
|
strcat( lname, ".lock~") ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
/* check that we are not being cheated, qname must point to */
|
|
|
|
/* a regular file - even this code leaves a small window of */
|
|
|
|
/* vulnerability but it is rather hard to exploit it */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
#if defined(S_IFLNK)
|
|
|
|
if (lstat(lname, &sbuf) == 0)
|
|
|
|
#else
|
|
|
|
if (stat(lname, &sbuf) == 0)
|
|
|
|
#endif
|
|
|
|
#if defined(S_ISREG)
|
2005-09-30 18:26:09 -04:00
|
|
|
if (!S_ISREG(sbuf.st_mode))
|
2005-05-31 11:50:56 -04:00
|
|
|
#else
|
2005-09-30 18:26:09 -04:00
|
|
|
if (!(((sbuf.st_mode) & 070000) == 0)) /* SysV R2 */
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
|
|
|
return "LOCK ERROR: not a regular file";
|
|
|
|
|
|
|
|
mask = umask(0);
|
|
|
|
fd = open(lname, O_RDWR | O_CREAT, 0666);
|
|
|
|
umask(mask);
|
2005-09-30 18:26:09 -04:00
|
|
|
if (fd < 0) {
|
2005-05-31 11:50:56 -04:00
|
|
|
if (errno == EACCES)
|
|
|
|
return NULL;
|
|
|
|
#ifdef EROFS
|
|
|
|
if (errno == EROFS)
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
return "LOCK ERROR: cannot access lock file";
|
|
|
|
}
|
2005-09-30 18:26:09 -04:00
|
|
|
if ((n = read(fd, locker, MAXNAME)) < 1) {
|
2005-05-31 11:50:56 -04:00
|
|
|
lseek(fd, 0, SEEK_SET);
|
2017-05-06 22:50:09 -04:00
|
|
|
/*
|
|
|
|
** Since Ubuntu 17.04, cuserid prototype seems missing. Replacing it by
|
|
|
|
** getlogin does the trick on 64 bits but fails on 32 bits.
|
|
|
|
** So let's work around with cuserid for a while.
|
|
|
|
** logname = getlogin() ;
|
|
|
|
** strcpy( locker, logname ? logname : cuserid( NULL)) ;
|
|
|
|
*/
|
2020-06-16 02:09:44 -04:00
|
|
|
#if BSD
|
|
|
|
strcpy( locker, getlogin()) ;
|
|
|
|
#else
|
2017-05-06 22:50:09 -04:00
|
|
|
strcpy( locker, cuserid( NULL)) ;
|
2020-06-16 02:09:44 -04:00
|
|
|
#endif
|
2017-05-06 22:50:09 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
strcat(locker + strlen(locker), "@");
|
|
|
|
gethostname(locker + strlen(locker), 64);
|
2015-01-02 01:20:07 -05:00
|
|
|
{
|
|
|
|
int ret, locker_size ;
|
|
|
|
|
|
|
|
locker_size = strlen( locker) ;
|
|
|
|
ret = write( fd, locker, locker_size) ;
|
|
|
|
if( ret != locker_size) {
|
|
|
|
/* some error handling here */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
locker[n > MAXNAME ? MAXNAME : n] = 0;
|
|
|
|
return locker;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
/*********************
|
|
|
|
*
|
|
|
|
* undolock -- unlock the file fname
|
|
|
|
*
|
|
|
|
* if successful, returns NULL
|
|
|
|
* if other error, returns "LOCK ERROR: explanation"
|
|
|
|
*
|
|
|
|
*********************/
|
2005-09-30 18:26:09 -04:00
|
|
|
|
2016-03-19 02:36:27 -04:00
|
|
|
char *undolock( const char *fname) {
|
|
|
|
char lname[ MAXLOCK] ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2019-06-19 07:49:20 -04:00
|
|
|
mystrscpy( lname, fname, sizeof lname - 6) ;
|
2016-03-19 02:36:27 -04:00
|
|
|
strcat( lname, ".lock~") ;
|
2005-09-30 18:26:09 -04:00
|
|
|
if (unlink(lname) != 0) {
|
2005-05-31 11:50:56 -04:00
|
|
|
if (errno == EACCES || errno == ENOENT)
|
|
|
|
return NULL;
|
|
|
|
#ifdef EROFS
|
|
|
|
if (errno == EROFS)
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
return "LOCK ERROR: cannot remove lock file";
|
|
|
|
}
|
|
|
|
return NULL;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
2015-11-24 21:42:33 -05:00
|
|
|
|
|
|
|
#else
|
|
|
|
typedef void _pedantic_empty_translation_unit ;
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2015-11-24 21:42:33 -05:00
|
|
|
|
|
|
|
/* end of pklock.c */
|