aboutsummaryrefslogtreecommitdiff
path: root/src/lock.c
blob: b9ea0dc8ec1849ae7d1c0299b92493fed5b5cf0e (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* wydawca - automatic release submission daemon
   Copyright (C) 2007, 2009 Sergey Poznyakoff

   Wydawca 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 3 of the License, or (at your
   option) any later version.

   Wydawca 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 wydawca. If not, see <http://www.gnu.org/licenses/>. */

#include "wydawca.h"
#include "c-ctype.h"
#include "xgethostname.h"

int enable_locking = 1;
char *lockdir;
time_t lock_expire_time = 5*60;
unsigned lock_retry_attempts = 3;
time_t lock_retry_interval = 1;

#define LOCKFILE_MODE        0644

static int
stat_check (const char *file, int fd, int links)
{
  struct stat fn_stat;
  struct stat fd_stat;
  int err = 0;
  int localfd = -1;

  if (fd == -1)
    {
      localfd = open (file, O_RDONLY);
      
      if (localfd == -1)
	return errno;
      fd = localfd;
    }

  /* We should always be able to stat a valid fd, so this
     is an error condition. */
  if (lstat (file, &fn_stat) || fstat (fd, &fd_stat))
    err = errno;
  else
    {
      /* If the link and stat don't report the same info, or the
         file is a symlink, fail the locking. */
#define CHK(X) if(X) err = EINVAL

      CHK (!S_ISREG (fn_stat.st_mode));
      CHK (!S_ISREG (fd_stat.st_mode));
      CHK (fn_stat.st_nlink != links);
      CHK (fn_stat.st_dev != fd_stat.st_dev);
      CHK (fn_stat.st_ino != fd_stat.st_ino);
      CHK (fn_stat.st_mode != fd_stat.st_mode);
      CHK (fn_stat.st_nlink != fd_stat.st_nlink);
      CHK (fn_stat.st_uid != fd_stat.st_uid);
      CHK (fn_stat.st_gid != fd_stat.st_gid);
      CHK (fn_stat.st_rdev != fd_stat.st_rdev);

#undef CHK
    }
  if (localfd != -1)
    close (localfd);

  return err;
}

int
_lock_internal (const char *file, const char *fname)
{
  int err = 0;
  int fd;
  FILE *fp;
  
  fd = open (fname, O_WRONLY | O_CREAT | O_EXCL, LOCKFILE_MODE);
  if (fd == -1)
    {
      if (errno == EEXIST)
	return LOCK_RETRY;
      else
	{
	  logmsg (LOG_ERR, _("cannot create lock file %s: %s"),
		  fname, strerror (errno));
	  return LOCK_FAILURE;
	}
    }
  close (fd);
  
  /* Try to link to the lockfile. */
  if (link (fname, file) == -1)
    {
      unlink (fname);
      if (errno == EEXIST)
	return LOCK_RETRY;
      else
	{
	  logmsg (LOG_ERR, _("cannot create lock file %s: %s"),
		  file, strerror (errno));
	  return LOCK_FAILURE;
	}
    }

  if ((fd = open (file, O_RDWR)) == -1)
    {
      unlink (fname);
      logmsg (LOG_ERR, _("cannot open lock file %s: %s"),
	      fname, strerror (errno));
      return LOCK_FAILURE;
    }
  
  err = stat_check (fname, fd, 2);
  if (err)
    {
      unlink (fname);
      logmsg (LOG_ERR, _("lock file check failed: %s"), strerror (errno));
      return (err == EINVAL) ? LOCK_INVALID : LOCK_FAILURE;
    }

  unlink (fname);

  fp = fdopen (fd, "w");
  fprintf (fp, "%lu", (unsigned long) getpid ());
  fclose (fp);
  
  return 0;
}

static void
expire_stale_lock (const char *file)
{
  int stale = 0;
  char buf[80];
  int fd;
  int len;
  
  fd = open (file, O_RDONLY);
  if (fd == -1)
    return;

  len = read (fd, buf, sizeof (buf) - 1);
  if (len > 0)
    {
      pid_t pid;

      buf[len] = 0;
      pid = strtoul (buf, NULL, 10);
      if (pid > 0)
	{
	  /* Process is gone so we try to remove the lock. */
	  if (kill (pid, 0) == -1)
	    stale = 1;
	}
      else
	stale = 1;		/* Corrupted file, remove the lock. */
    }

  if (!stale && lock_expire_time > 0)
    {    
      struct stat stbuf;
      fstat (fd, &stbuf);
      /* The lock has expired. */
      if ((time (NULL) - stbuf.st_mtime) > lock_expire_time)
	stale = 1;
    }
  
  close (fd);
  if (stale)
    unlink (file);
}

int
wydawca_lock (const char *lockname)
{
  char *tempname = NULL;
  int rc;
  
  if (!enable_locking)
    return 0;
  expire_stale_lock (lockname);

  /* build the NFS hitching-post to the lock file */
  asprintf (&tempname, "%s.%lu.%lu.%s",
	    lockname,
	    (unsigned long) getpid (),
	    (unsigned long) time (NULL), xgethostname ());
  if (!tempname)
    return LOCK_FAILURE;
  if (lock_retry_attempts)
    {
      unsigned i;
      
      for (i = 0; i < lock_retry_attempts; i++)
	{
	  rc = _lock_internal (lockname, tempname);
	  if (rc == 0)
	    break;
	  if (rc == LOCK_RETRY)
	    sleep (lock_retry_interval);
	}
    }
  else
    rc = _lock_internal (lockname, tempname);

  free (tempname);
  return rc;
}

void
wydawca_unlock (const char *lockfile)
{
  if (enable_locking)
    unlink (lockfile);
}

static char *
fix_tagname (const char *tag)
{
  char *tagname = xstrdup (tag);
  char *p;
  
  for (p = tagname; *p; p++)
    if (!c_isalnum (*p) && *p != '_' && *p != '-')
      *p = '_';
  return tagname;
}  

char *
wydawca_lockname (const char *tag)
{
  char *lockname = NULL;
  char *tagname = fix_tagname (tag);
  asprintf (&lockname, "%s/LCK.%s", lockdir, tagname);
  if (!lockname)
    xalloc_die ();
  free (tagname);
  return lockname;
}

void
wydawca_lock_init ()
{
  if (enable_locking)
    {
      if (!lockdir)
	lockdir = xstrdup (LOCALSTATEDIR "/lock/" PACKAGE);
      if (create_hierarchy (lockdir, 0))
	exit (EX_OSFILE);
    }
}

Return to:

Send suggestions and report system problems to the System administrator.