File: proto.c

package info (click to toggle)
nn 6.7.3-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,508 kB
  • ctags: 3,196
  • sloc: ansic: 32,032; sh: 1,491; awk: 138; makefile: 94
file content (264 lines) | stat: -rw-r--r-- 5,473 bytes parent folder | download | duplicates (7)
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
257
258
259
260
261
262
263
264
/*
 *	(c) Copyright 1990, Kim Fabricius Storm.  All rights reserved.
 *      Copyright (c) 1996-2005 Michael T Pins.  All rights reserved.
 *
 *	Master/slave communication and locking.
 */

#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <pwd.h>
#include <string.h>
#include "config.h"
#include "global.h"
#include "hostname.h"
#include "proto.h"

/* proto.c */

static void     write_lock(char *lock, char *operation);
static int      read_lock(char *lock);


#ifndef ACCOUNTING

#ifdef AUTHORIZE
#define ACCOUNTING
#endif

#endif

extern char    *master_directory, *db_directory;

#define HOSTBUF 80
char            proto_host[HOSTBUF];	/* host having the lock */

/*
 *	When setting a lock, we must check a little later that
 *	we really got the lock set, i.e. that another process
 *	didn't set it at the same time!
 */

#define LOCK_SAFETY	5	/* seconds */

/*
 *	proto_lock(program, mode)
 *
 *	Returns:
 *	-1	Not running.
 *	0	Running, no permission (PL_WAKEUP_SOFT)
 *	0	Lock set (PL_SET)
 *	1	Lock not set (PL_SET)  (another is running)
 *	1       Locked and running (PL_WAKEUP)
 *	pid	Locked and running (PL_CHECK)
 */

static void
write_lock(char *lock, char *operation)
{
    FILE           *m_pid;
    char            host[HOSTBUF];

    m_pid = open_file(lock, OPEN_CREATE);
    if (m_pid == NULL)
	sys_error("Cannot %s lock file: %s", operation, lock);
    nn_gethostname(host, HOSTBUF);
    fprintf(m_pid, "%d\n%s\n", process_id, host);
    fclose(m_pid);
}

static int
read_lock(char *lock)
{
    FILE           *m_pid;
    char            host[HOSTBUF];
    char            pid[10];

    pid[0] = NUL;
    proto_host[0] = NUL;

    m_pid = open_file(lock, OPEN_READ);
    if (m_pid == NULL)
	return -2;		/* no lock */
    fgets(pid, 10, m_pid);
    fgets(proto_host, HOSTBUF, m_pid);
    fclose(m_pid);

    if (pid[0] == NUL)
	return 0;		/* corrupted lock */

    if (proto_host[0] != NUL) {
	substchr(proto_host, NL, NUL);
	nn_gethostname(host, HOSTBUF);
	if (strncmp(proto_host, host, HOSTBUF) != 0)
	    return -1;		/* locked by another host */
	proto_host[0] = NUL;
    }
    return atoi(pid);
}

int
proto_lock(int prog, int command)
{
    int             try, pid;
    char           *lock = NULL;

    switch (prog) {
	case I_AM_MASTER:
	case I_AM_EXPIRE:
	    lock = relative(master_directory, "MPID");
	    break;
	case I_AM_SPEW:
	    lock = relative(master_directory, "WPID");
	    break;

#ifdef ACCOUNTING
	case I_AM_ACCT:
	    lock = relative(db_directory, "LCK..acct");
	    break;
#endif

	case I_AM_NN:
	    lock = relative(nn_directory, "LOCK");
	    break;
	default:
	    sys_error("Invalid LOCK prog");
    }

    if (command == PL_TRANSFER) {
	write_lock(lock, "transfer");
	return 1;
    }
    if (command == PL_CLEAR)
	goto rm_lock;

    try = 1;
again:

    switch (pid = read_lock(lock)) {
	case -2:
	    goto no_lock;
	case -1:		/* wrong host */
	    return 1;
	case 0:
	case 1:
	case 2:		/* corrupted lock file */
	    if (who_am_i == I_AM_NN)
		goto rm_lock;
	    if (--try < 0)
		goto rm_lock;
	    sleep(LOCK_SAFETY);	/* maybe it is being written */
	    goto again;
	default:
	    break;
    }

    if (kill(pid, command == PL_TERMINATE ? SIGHUP : SIGALRM) == 0) {
	switch (command) {
	    case PL_SET_QUICK:
		sleep(1);
		goto again;

	    case PL_SET_WAIT:
	    case PL_CLEAR_WAIT:
		sleep(30);
		goto again;

	    case PL_CHECK:
		return pid;

	    default:
		return 1;
	}
    }
    if (command == PL_CHECK)
	return (errno == EPERM) ? pid : -1;
    if (command == PL_WAKEUP_SOFT)
	return (errno == EPERM) ? 0 : -1;

    /* lock file contains a non-existing process, or a process with */
    /* wrong owner, ie. neither master or expire, so remove it */

rm_lock:
    unlink(lock);

no_lock:
    if (command != PL_SET && command != PL_SET_QUICK && command != PL_SET_WAIT)
	return -1;

    write_lock(lock, "create");

    /* a user will not start nn twice at the exact same time! */
    if (who_am_i == I_AM_NN || command == PL_SET_QUICK)
	return 0;

    sleep(LOCK_SAFETY);

    if (read_lock(lock) != process_id)
	return 1;		/* somebody stole the lock file */
    return 0;			/* lock is set */
}

FILE           *
open_gate_file(int mode)
{
    char           *gate, *err;
    FILE           *gf;

    gate = relative(master_directory, "GATE");
    gf = open_file(gate, mode);
    err = NULL;

    switch (mode) {
	case OPEN_READ:
	    if (gf != NULL) {
		if (unlink(gate) == 0)
		    break;
		err = "unlink";
		break;
	    }
	    if (errno != ENOENT)
		err = "read";
	    break;

	default:
	    if (gf != NULL)
		chmod(gate, 0644);	/* override restrictive umask */
	    /* caller must complain if cannot open! */
	    break;
    }

    if (err != NULL) {
	sys_warning("Cannot %s %s (err=%d).  Check modes!!", err, gate, errno);
	if (gf != NULL)
	    fclose(gf);
	return NULL;
    }
    return gf;
}

void
send_master(char command, group_header * gh, int opt, long arg)
{
    FILE           *gate;

    gate = open_gate_file(OPEN_APPEND);

    if (gate == NULL) {
	printf("Cannot send to master (check GATE file)\n");
	return;
    }
    fprintf(gate, "%c;%ld;%c;%ld;%s %s;\n",
	    command, gh == NULL ? -1L : gh->group_num, opt, arg,
	    user_name(), date_time((time_t) 0));

    fclose(gate);

    log_entry('A', "SEND %c %s %c %ld",
	      command, gh == NULL ? "(all)" : gh->group_name, opt, arg);

    if (who_am_i == I_AM_ADMIN)
	proto_lock(I_AM_MASTER, PL_WAKEUP_SOFT);
}