File: fork.c

package info (click to toggle)
rocks 2.1-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 284 kB
  • ctags: 412
  • sloc: ansic: 5,218; makefile: 111
file content (75 lines) | stat: -rw-r--r-- 1,236 bytes parent folder | download
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
/* 
 *  rocks/fork.c
 *
 *  Fork handler
 *
 *  Copyright (C) 2001 Victor Zandy
 *  See COPYING for distribution terms.
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include "rs.h"
#include "log.h"

static int
post_fork_child()
{
	if (!rs_opt_hb)
		return 0; /* nothing to do if there is no hb */
	if (0 > rs_init_heartbeat()) {
		rs_log("fork child cannot initialize heartbeat");
		return -1;
	}
	return 0;
}

static int
post_fork_parent(pid_t child)
{
	rs_log("fork -> [%d]", child);
	return 0;
}

/* FIXME: What should happen to suspended rocks across fork? */
pid_t
rs_fork()
{
	pid_t rv;
	sigset_t set;
	int i;

	/* avoid races with shm garbage collector in hb alarm handler */
	rs_stop_heartbeat(&set);

	/* share rocks that are not already shared */
	for (i = 0; i < RS_MAXFD; i++) {
		rs_t rs;
		rs = rs_lookup(i);
		if (rs && rs->state != RS_NOTCONNECTED
		    && !rs_rock_is_shared(rs)) {
			rv = rs_shm_create(rs);
			if (0 > rv) {
				rs_log("fork cannot create shared rock");
				goto out;
			}
		}
	}

	rv = fork();
	if (!rv)
		post_fork_child();
	else
		post_fork_parent(rv);
out:
	rs_resume_heartbeat(&set);
	return rv;
}

pid_t
rs_vfork()
{
	return rs_fork();
}