File: lockdb.c

package info (click to toggle)
lyskom-server 2.1.2-16.1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 10,564 kB
  • sloc: ansic: 51,006; exp: 12,368; sh: 10,501; python: 2,927; makefile: 1,025; yacc: 466; awk: 340; lisp: 271; lex: 218; sed: 50
file content (157 lines) | stat: -rw-r--r-- 3,935 bytes parent folder | download | duplicates (3)
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
/*
 * Lock and unlock the database.
 * Copyright (C) 1999, 2003  Lysator Academic Computer Association.
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM 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 1, or (at your option) 
 * any later version.
 * 
 * LysKOM 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 LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please report bugs at http://bugzilla.lysator.liu.se/. 
 */

#include <config.h>

#include <sys/param.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#ifdef HAVE_STRING_H
#  include <string.h>
#else
#  ifdef HAVE_STRINGS_H
#    include <strings.h>
#  endif
#endif
#ifndef HAVE_STRCHR
#  define strchr index
#endif
#include <stdlib.h>
#include <signal.h>
#include <netdb.h>
#include "timewrap.h"

#include "lockdb.h"
#include "log.h"
#include "kom-types.h"
#include "param.h"
#include "lyskomd.h"

int lock_db(void)
{
    char *new_lock;
    char *current_lock;
    char *end;
    int retry;
    size_t sz;
    size_t ix;
    pid_t pid;

    size_t lock_sz = 16;
    do {
	new_lock = malloc(lock_sz + 3 + 3 * sizeof(pid_t));
	if (!new_lock) restart_kom("malloc failed (%d)\n", errno);
	int ret = gethostname(new_lock, lock_sz);
	if (ret == -1) {
	    if (errno == ENAMETOOLONG) {
		free(new_lock);
		lock_sz *= 2;
	    } else {
		restart_kom("gethostname failed (%d)\n", errno);
	    }
	} else break;
    } while (1);

    end = strchr(new_lock, '\0');
    sprintf(end, ":%ld", (long)getpid());

    for (retry = 0; retry < 2; ++retry)
    {
	if (symlink(new_lock, param.lockfile_name) == 0)
	{
	    kom_log("Created lock %s\n", param.lockfile_name);
	    return 0;
	}
	
	if (errno != EEXIST)
	    restart_kom("Failed to create lock symlink %s "
			"pointing to %s: %d\n", param.lockfile_name,
			new_lock, errno);

	if (retry)
	{
	    kom_log("Lock file recreated by some other party\n");
	    return -1;
	}

	lock_sz = 16;
	do {
	    current_lock = malloc(lock_sz);
	    sz = readlink(param.lockfile_name, current_lock, lock_sz);
	    if (sz >= lock_sz) {
		free(current_lock);
		lock_sz *= 2;
	    } else {
		break;
	    }
	} while (1);
	current_lock[sz] = '\0';

	for (ix = 0; ix < lock_sz && current_lock[ix] && new_lock[ix]; ++ix)
	{
	    if (new_lock[ix] != current_lock[ix])
	    {
		kom_log("Database already locked by %s\n", current_lock);
		free(new_lock); free(current_lock);
		return -1;
	    }

	    if (new_lock[ix] == ':')
		break;
	}

	pid = strtol(&current_lock[ix+1], &end, 10);
	if (end == &current_lock[ix+1])
	    restart_kom("Found a broken lock symlink %s: %s\n",
			param.lockfile_name, current_lock);

	if (kill(pid, 0) == 0 || errno != ESRCH)
	{
	    kom_log("Database already locked by %s\n", current_lock);
	    free(new_lock); free(current_lock);
	    return -1;
	}

	/* The process is dead.  Remove the stale lock file.  If it
	   was just removed by a dying process -- fine.  */
	if (remove(param.lockfile_name) < 0 && errno != ENOENT)
	    restart_kom("Failed to remove stale lock symlink %s\n",
			param.lockfile_name);
	else
	    kom_log("Removed stale lock file left by %s.\n", current_lock);

	free(new_lock); free(current_lock);
    }
    restart_kom("Unreachable code reached in lock_db.\n");
}

void
unlock_db(void)
{
    if (remove(param.lockfile_name) < 0)
	restart_kom("Failed to remove lock file %s\n", param.lockfile_name);
}