summaryrefslogtreecommitdiffstats
path: root/util/dotlock.c
blob: 917b90dd55a0b1a30d506284f0d4b4431540bb8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* dotlock.c - dotfile locking
 *	Copyright (C) 1998 Free Software Foundation, Inc.
 *
 * This file is part of GNUPG.
 *
 * GNUPG is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GNUPG is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include "types.h"
#include "util.h"
#include "memory.h"




#if 0
/****************
 * Create a lockfile with the given name. A TIMEOUT of 0
 * returns immediately, -1 waits forever (hopefully not), other
 * values are timeouts in milliseconds.
 * Returns: a char pointer used as handle for release lock
 *	    or NULL in case of an error.
 *
 * Notes: This function creates a lock file in the same directory
 *	  as file_to_lock with the name "file_to_lock.lock"
 *	  A temporary file ".#lk.<pid>.<hostname> is used.
 *	  This function does nothing for Windoze.
 */
const char *
make_dotlock( const char *file_to_lock, long timeout )
{
    int rc=-1, fd=-1, pid;
    char pidstr[16];
    char *tname = NULL;
    char *p;

    log_debug("dotlock_make: lock='%s'\n", lockfile );
    sprintf( pidstr, "%10d\n", getpid() );
    /* add the hostname to the second line (FQDN or IP addr?) */

    /* create a temporary file */
    tname = CreateTmpFile2( p, ".#lk" );
    free(p);
    if( !tname )
	log_fatal( "could not create temporary lock file '%s'\n");
    log_debug( "dotlock_make: tmpname='%s'\n", tname );
    chmod( tname, 0644 ); /* just in case an umask is set */
    if( !(fd = open( tname, O_WRONLY )) )
	log_fatal( "could not open temporary lock file '%s'\n", tname);
    if( write(fd, pidstr, 11 ) != 11 )
	log_fatal( "error writing to temporary lock file '%s'\n", tname);
    if( close(fd) ) {
	log_fatal( "error closing '%s'\n", tname);

  retry:
    if( !link(tname, lockfile) )
	rc = 0; /* okay */
    else if( errno != EEXIST )
	log_error( "lock not made: link() failed: %s\n", strerror(errno) );
    else { /* lock file already there */
	if( (pid = read_lockfile(lockfile)) == -1 ) {
	    if( errno == ENOENT ) {
		log_debug( "lockfile disappeared\n");
		goto retry;
	    }
	    log_debug("cannot read lockfile\n");
	}
	else if( pid == getpid() ) {
	    log_info( "Oops: lock already hold by us\n");
	    rc = 0;  /* okay */
	}
	else if( kill(pid, 0) && errno == ESRCH ) {
	    log_info( "removing stale lockfile (created by %d)", (int)pid );
	    remove( lockfile );
	    goto retry;
	}
	log_debug( "lock not made: lock file exists\n" );
    }

    if( tname ) {
	remove(tname);
	free(tname);
    }
    if( !rc )
	log_debug( "lock made\n");
    return rc;
}

/****************
 * Create a lockfile for a existing file
 * Returns: a char pointer used as handle for release lock
 *	    or NULL in case of an error.
 *
 * Notes: This function creates a lock file in the same directory
 *	  as file_to_lock with the name "lock.<inode-no>"
 *
 * int
 * make_inodelock( const char *file_to_lock )
 *
 */




/****************
 * release a lock
 * Returns: 0 := success
 */
int
release_dotlock( const char *lockfile )
{
    int pid = read_lockfile( lockfile );
    if( pid == -1 ) {
	log_error( "release_dotlock: lockfile error");
	return -1;
    }
    if( pid != getpid() ) {
	log_error( "release_dotlock: not our lock (pid=%d)", pid);
	return -1;
    }
    if( remove( lockfile ) ) {
	log_error( "release_dotlock: error removing lockfile '%s'",
							    lockfile);
	return -1;
    }
    log_debug( "release_dotlock: released lockfile '%s'", lockfile);
    return 0;
}


/****************
 * Read the lock file and return the pid, returns -1 on error.
 */
static int
read_lockfile( const char *name )
{
    int fd, pid;
    char pidstr[16];

    if( (fd = open(name, O_RDONLY)) == -1 ) {
	int e = errno;
	log_debug("error opening lockfile '%s'", name );
	errno = e;
	return -1;
    }
    if( read(fd, pidstr, 10 ) != 10 ) {
	log_debug("error reading lockfile '%s'", name );
	close(fd);
	errno = 0;
	return -1;
    }
    close(fd);
    pid = atoi(pidstr);
    if( !pid || pid == -1 ) {
	log_error("invalid pid %d in lockfile '%s'", pid, name );
	errno = 0;
	return -1;
    }
    return pid;
}
#endif