cmogstored.git  about / heads / tags
alternative mogstored implementation for MogileFS
blob 1975f280d00d7e6cb76b8b8c77e09ad8ef44440e 13495 bytes (raw)
$ git show HEAD:cmogstored.c	# shows this blob on the CLI

  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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
 
/*
 * Copyright (C) 2012-2020 all contributors <cmogstored-public@yhbt.net>
 * License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
 */
#include "cmogstored.h"
#include "cfg.h"
#include "nostd/setproctitle.h"
#define THIS "cmogstored"
#define DESC "alternative mogstored implementation for MogileFS"
static char summary[] = THIS " -- "DESC;
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
const char *argp_program_version = THIS" "PACKAGE_VERSION;
static struct mog_fd *master_selfwake;
static sig_atomic_t sigchld_hit;
static sig_atomic_t do_exit;
static sig_atomic_t do_upgrade;
static pid_t master_pid;
static pid_t upgrade_pid;
static bool iostat_running;

static struct mog_main mog_main;

#define CFG_KEY(f) -((int)offsetof(struct mog_cfg,f) + 1)
static struct argp_option options[] = {
	{ .name = "daemonize", .key = 'd',
	  .doc = "Daemonize" },
	{ .name = "config", .key = CFG_KEY(configfile),
	  .arg = "<file>",
          .doc = "Set config file (no default, unlike mogstored)" },
	{ .name = "httplisten", .key = CFG_KEY(httplisten),
	  .arg = "<ip:port>",
	  .doc = "IP/Port HTTP server listens on" },
	/*
	 * NOT adding httpgetlisten here to avoid (further) breaking
	 * compatibility with Perl mogstored.  Most of these command-line
	 * options suck anyways.
	 */
	{ .name = "mgmtlisten", .key = CFG_KEY(mgmtlisten),
	  .arg = "<ip:port>",
	  .doc = "IP/Port management/sidechannel listens on" },
	{ .name = "docroot", .key = CFG_KEY(docroot),
	  .arg = "<path>",
	  .doc = "Docroot above device mount points.  "
	         "Defaults to "MOG_DEFAULT_DOCROOT
	},
	{ .name = "maxconns", .key = CFG_KEY(maxconns),
	  .arg = "<number>",
	  .doc = "Number of simultaneous clients to serve. "
	         "Default " MOG_STR(MOG_DEFAULT_MAXCONNS) },
	{ .name = "pidfile", .key = CFG_KEY(pidfile),
	  .arg = "<path>",
	  .doc = "path to PID file" },
	{ .name = "server", .key = CFG_KEY(server), .flags = OPTION_HIDDEN,
	  .arg = "(perlbal|none)"
	},
	{
	  /* hidden for now, don't break compat with Perl mogstored */
	  .name = "multi", .key = -'M', .flags = OPTION_HIDDEN
	},
	{
	  /* hidden for now, don't break compat with Perl mogstored */
	  .name = "worker-processes", .key = -'W', .flags = OPTION_HIDDEN,
	  .arg = "<number>",
	},
	/* we don't load a default config file like Perl mogstored at all */
	{ .name = "skipconfig", .key = 's', .flags = OPTION_HIDDEN },
	{ NULL }
};

static void new_cfg_or_die(const char *config)
{
	struct mog_cfg *cfg = mog_cfg_new(config);

	if (!cfg) die("invalid (or duplicate) config=%s", config);
	if (mog_cfg_load(cfg) == 0) return;

	die("failed to load config=%s: %s",
	    config, errno ? strerror(errno) : "parser error");
}

static void check_strtoul(unsigned long *dst, const char *s, const char *key)
{
	char *end;

	errno = 0;
	*dst = strtoul(s, &end, 10);
	if (errno)
		die_errno("failed to parse --%s=%s", key, s);
	if (*end)
		die("failed to parse --%s=%s (invalid character: %c)",
		    key, s, *end);
}

static void addr_or_die(struct mog_addrinfo **dst, const char *key, char *s)
{
	*dst = mog_listen_parse(s);
	if (!*dst)
		die("failed to parse %s=%s", key, s);
}

static error_t parse_opt(int key, char *arg, struct argp_state *state)
{
	struct mog_cfg *cfg = state->input;
	int rv = 0;

	switch (key) {
	case 'd': cfg->daemonize = true; break;
	case 's': /* no-op, we don't load the default config file */; break;
	case CFG_KEY(docroot): cfg->docroot = xstrdup(arg); break;
	case CFG_KEY(pidfile): cfg->pidfile = xstrdup(arg); break;
	case CFG_KEY(configfile): new_cfg_or_die(arg); break;
	case CFG_KEY(maxconns):
		check_strtoul(&cfg->maxconns, arg, "maxconns");
		break;
	case CFG_KEY(httplisten):
		addr_or_die(&cfg->httplisten, "httplisten", arg);
		break;
	case CFG_KEY(mgmtlisten):
		addr_or_die(&cfg->mgmtlisten, "mgmtlisten", arg);
		break;
	case CFG_KEY(server):
		cfg->server = xstrdup(arg);
		mog_cfg_check_server(cfg);
		break;
	case -'M': mog_cfg_multi = true; break;
	case -'W':
		check_strtoul(&mog_main.worker_processes, arg,
				"worker-processes");
		if (mog_main.worker_processes > UINT_MAX)
			die("--worker-processes exceeded (max=%u)", UINT_MAX);
		break;
	case ARGP_KEY_ARG:
		argp_usage(state);
	case ARGP_KEY_END:
		break;
	default:
		rv = ARGP_ERR_UNKNOWN;
	}

	return rv;
}

static void dup2_null(int oldfd, int newfd, const char *errdest)
{
	int rc;

	do {
		rc = dup2(oldfd, newfd);
	} while (rc < 0 && (errno == EINTR || errno == EBUSY));

	if (rc < 0)
		die_errno("dup2(/dev/null,%s) failed", errdest);
}

static void daemonize(int null_fd)
{
	int ready_pipe[2];
	pid_t pid;
	ssize_t r;

	if (pipe(ready_pipe) < 0)
		die_errno("pipe() failed");
	pid = fork();
	if (pid < 0)
		die_errno("fork() failed");
	if (pid > 0) { /* parent */
		mog_close(ready_pipe[1]);
		do {
			r = read(ready_pipe[0], &pid, sizeof(pid_t));
		} while (r < 0 && errno == EINTR);

		PRESERVE_ERRNO( mog_close(ready_pipe[0]) );
		if (r == sizeof(pid_t))
			exit(EXIT_SUCCESS);
		if (r < 0)
			die_errno("ready_pipe read error");
		die("daemonization error, check syslog");
	}

	/* child */
	mog_close(ready_pipe[0]);
	if (setsid() < 0)
		die_errno("setsid() failed");

	pid = fork();
	if (pid < 0)
		die_errno("fork() failed");
	if (pid > 0) /* intermediate parent */
		exit(EXIT_SUCCESS);

	if (chdir("/") < 0)
		die_errno("chdir(/) failed");

	dup2_null(null_fd, STDOUT_FILENO, "stdout");
	dup2_null(null_fd, STDERR_FILENO, "stderr");
	do {
		r = write(ready_pipe[1], &pid, sizeof(pid_t));
	} while (r < 0 && errno == EINTR);
	if (r < 0)
		syslog(LOG_CRIT, "ready_pipe write error: %m");
	else
		assert(r == sizeof(pid_t) && "impossible short write");
	mog_close(ready_pipe[1]);
}

#ifndef LOG_PERROR
# define LOG_PERROR 0
#endif

/* TODO: make logging configurable (how?) */
static void log_init(bool is_daemon)
{
	int mask = 0;
	int option = LOG_PID;

	if (!is_daemon)
		option |= LOG_PERROR;

	openlog(THIS, option, LOG_DAEMON);
	mask |= LOG_MASK(LOG_EMERG);
	mask |= LOG_MASK(LOG_ALERT);
	mask |= LOG_MASK(LOG_CRIT);
	mask |= LOG_MASK(LOG_ERR);
	mask |= LOG_MASK(LOG_WARNING);
	mask |= LOG_MASK(LOG_NOTICE);
	mask |= LOG_MASK(LOG_INFO);
	/* mask |= LOG_MASK(LOG_DEBUG); */
	setlogmask(mask);
}

MOG_NOINLINE static void setup(int argc, char *argv[])
{
	int pid_fd = -1;
	static struct argp argp = { options, parse_opt, NULL, summary };
	int null_fd;

	mog_mnt_refresh();
	argp_parse(&argp, argc, argv, 0, NULL, &mog_cli);
	mog_cfg_validate_or_die(&mog_cli);
	log_init(mog_cli.daemonize);
	mog_inherit_init();
	mog_cfg_svc_start_or_die(&mog_cli);
	mog_inherit_cleanup();

	if (mog_cli.pidfile) pid_fd = mog_pidfile_prepare(mog_cli.pidfile);

	null_fd = open("/dev/null", O_RDWR);
	if (null_fd < 0)
		die_errno("open(/dev/null) failed");
	dup2_null(null_fd, STDIN_FILENO, "stdin");

	/* don't daemonize if we're inheriting FDs, we're already daemonized */
	if (mog_cli.daemonize && !getenv("CMOGSTORED_FD"))
		daemonize(null_fd);

	mog_close(null_fd);

	if (pid_fd >= 0 && mog_pidfile_commit(pid_fd) < 0)
		syslog(LOG_ERR,
		       "failed to write pidfile(%s): %m. continuing...",
		       mog_cli.pidfile);

	master_pid = getpid();

	/* set svc->nmogdev on all svc */
	mog_mkusage_all();
}

static void worker_wakeup_handler(int signum)
{
	switch (signum) {
	case SIGUSR2: do_upgrade = 1; break;
	case SIGCHLD: sigchld_hit = 1; break;
	case SIGQUIT:
	case SIGTERM:
	case SIGINT:
		do_exit = signum;
	}
	mog_notify(MOG_NOTIFY_SIGNAL);
}

static void wakeup_noop(int signum)
{
	/* just something to cause EINTR */
}

static void master_wakeup_handler(int signum)
{
	switch (signum) {
	case SIGCHLD: sigchld_hit = 1; break;
	case SIGUSR2: do_upgrade = 1; break;
	case SIGQUIT:
	case SIGTERM:
	case SIGINT:
		do_exit = signum;
	}
	mog_selfwake_trigger(master_selfwake);
}

static void siginit(void (*wakeup_handler)(int))
{
	struct sigaction sa;

	memset(&sa, 0, sizeof(struct sigaction));
	CHECK(int, 0, sigemptyset(&sa.sa_mask) );

	sa.sa_handler = SIG_IGN;
	CHECK(int, 0, sigaction(SIGPIPE, &sa, NULL));

	sa.sa_handler = wakeup_noop;
	CHECK(int, 0, sigaction(SIGURG, &sa, NULL));

	sa.sa_handler = wakeup_handler;

	/* TERM and INT are graceful shutdown for now, no immediate shutdown */
	CHECK(int, 0, sigaction(SIGTERM, &sa, NULL));
	CHECK(int, 0, sigaction(SIGINT, &sa, NULL));

	CHECK(int, 0, sigaction(SIGQUIT, &sa, NULL)); /* graceful shutdown */
	CHECK(int, 0, sigaction(SIGUSR1, &sa, NULL)); /* no-op, nginx compat */
	CHECK(int, 0, sigaction(SIGUSR2, &sa, NULL)); /* upgrade */

	/*
	 * SIGWINCH/SIGHUP are no-ops for now to allow reuse of nginx init
	 * scripts.  We should support them in the future.
	 * SIGWINCH will disable new connections and drop idlers
	 * SIGHUP will reenable new connections/idlers after SIGWINCH
	 */
	CHECK(int, 0, sigaction(SIGWINCH, &sa, NULL));
	CHECK(int, 0, sigaction(SIGHUP, &sa, NULL));

	sa.sa_flags = SA_NOCLDSTOP;
	CHECK(int, 0, sigaction(SIGCHLD, &sa, NULL));
}

static void process_died(pid_t pid, int status);

static void sigchld_handler(void)
{
	sigchld_hit = 0;

	for (;;) {
		int status;
		pid_t pid = waitpid(-1, &status, WNOHANG);

		if (pid > 0) {
			process_died(pid, status);
		} else if (pid == 0) {
			return;
		} else {
			switch (errno) {
			case EINTR: sigchld_hit = 1; return; /* retry later */
			case ECHILD: return;
			default: die_errno("waitpid");
			}
		}
	}
}

static void upgrade_handler(void)
{
	do_upgrade = 0;
	if (upgrade_pid > 0) {
		syslog(LOG_INFO, "upgrade already running on PID:%d",
		       upgrade_pid);
	} else {
		if (master_pid == getpid())
			upgrade_pid = mog_upgrade_spawn();
		/* else: worker processes (if configured) do not upgrade */
	}
}

static void main_worker_loop(const pid_t parent)
{
	while (parent == 0 || parent == getppid()) {
		mog_notify_wait(mog_main.have_mgmt);
		if (sigchld_hit)
			sigchld_handler();
		if (do_upgrade)
			upgrade_handler();
		if (do_exit)
			cmogstored_exit();
		if (mog_main.have_mgmt)
			mog_mnt_refresh();
		else if (mog_main.have_mgmt && !iostat_running && !do_exit)
			/*
			 * maybe iostat was not installed/available/usable at
			 * startup, but became usable later
			 */
			iostat_running = mog_iostat_respawn(0);
	}

	syslog(LOG_INFO, "parent=%d abandoned us, dying", parent);
	cmogstored_exit();
}

static void run_worker(const pid_t parent)
{
	mog_notify_init();
	siginit(worker_wakeup_handler);

	/* this can set mog_main->have_mgmt */
	mog_svc_each(mog_svc_start_each, &mog_main);

	if (mog_main.have_mgmt) {
		iostat_running = mog_iostat_respawn(0);
		if (!iostat_running)
			syslog(LOG_WARNING, "iostat(1) not available/running");
	}
	main_worker_loop(parent);
}

static void fork_worker(unsigned worker_id)
{
	pid_t pid;
	pid_t parent = getpid(); /* not using getppid() since it's racy */

	pid = fork();
	if (pid > 0) {
		mog_process_register(pid, worker_id);
	} else if (pid == 0) {
		mog_selfwake_put(master_selfwake);
		mog_process_reset();
		mog_svc_each(mog_svc_atfork_child, &parent);

		/* worker will call mog_intr_enable() later in notify loop */
		run_worker(parent);
		exit(EXIT_SUCCESS);
	} else {
		syslog(LOG_ERR, "fork() failed: %m, sleeping for 10s");
		mog_sleep(10);
	}
}

/* run when a worker dies */
static void worker_died(pid_t pid, int status, unsigned id)
{
	if (do_exit)
		return;

	syslog(LOG_INFO,
	       "worker[%u] PID:%d died with status=%d, respawning",
	       id, (int)pid, status);
	fork_worker(id);
}

static void iostat_died(pid_t pid, int status)
{
	if (do_exit)
		return;
	iostat_running = mog_iostat_respawn(status);
}

/* run when any process dies */
static void process_died(pid_t pid, int status)
{
	unsigned id = mog_process_reaped(pid);
	char *name;

	if (mog_process_is_worker(id)) {
		worker_died(pid, status, id);
		return;
	}

	switch (id) {
	case MOG_PROC_IOSTAT:
		iostat_died(pid, status);
		return;
	case MOG_PROC_UPGRADE:
		assert(pid == upgrade_pid && "upgrade_pid misplaced");
		syslog(LOG_INFO, "upgrade PID:%d exited with status=%d",
		       pid, status);
		mog_pidfile_upgrade_abort();
		upgrade_pid = -1;
		return;
	default:
		/* could be an inherited iostat if we're using worker+master */
		name = mog_process_name(id);
		if (!name)
			syslog(LOG_ERR, "OOM: %m");
		syslog(LOG_INFO,
		       "reaped %s pid=%d with status=%d, ignoring",
		       name ? name : "unknown", (int)pid, status);
		free(name);
	}
}

static void run_master(void)
{
	unsigned id;
	size_t running = mog_main.worker_processes;

	master_selfwake = mog_selfwake_new();
	siginit(master_wakeup_handler);

	for (id = 0; id < mog_main.worker_processes; id++)
		fork_worker(id);

	while (running > 0) {
		mog_selfwake_wait(master_selfwake);
		if (sigchld_hit)
			sigchld_handler();
		if (do_upgrade)
			upgrade_handler();
		if (do_exit)
			running = mog_kill_each_worker(SIGQUIT);
	}
	mog_selfwake_put(master_selfwake);
}

int main(int argc, char *argv[], char *envp[])
{
	mog_upgrade_prepare(argc, argv, envp);
	/* hack for older gcov + gcc, see nostd/setproctitle.h */
	spt_init(argc, argv, envp);
	set_program_name(argv[0]);

	mog_intr_disable();
	setup(argc, argv); /* this daemonizes */

	mog_process_init(mog_main.worker_processes);
	if (mog_main.worker_processes == 0)
		run_worker(0);
	else
		run_master();

	return 0;
}

/* called by the "shutdown" command via mgmt */
void cmogstored_quit(void)
{
	if (master_pid != getpid()) {
		if (kill(master_pid, SIGQUIT) != 0)
			syslog(LOG_ERR,
			       "SIGQUIT failed on master process (pid=%d): %m",
				master_pid);
	} else {
		CHECK(int, 0, kill(getpid(), SIGQUIT));
	}
}

git clone https://yhbt.net/cmogstored.git