File: fork.c

package info (click to toggle)
rocks 2.4-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 296 kB
  • ctags: 434
  • sloc: ansic: 5,668; makefile: 111
file content (82 lines) | stat: -rw-r--r-- 1,380 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
76
77
78
79
80
81
82
/* 
 *  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()
{
	rs_pid = getpid();
	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 = -1;
	sigset_t set;
	int i;

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

	for (i = 0; i < RS_MAXFD; i++) {
		rs_t rs;
		rs = rs_lookup(i);
		if (!rs)
			continue;

		/* share shareworthy rocks that are not already shared */
		if (rs->state != RS_NOTCONNECTED && rs->state != RS_EDP) {
			if (!rs_rock_is_shared(rs)) {
				if (0 > rs_shm_create(rs)) {
					rs_log("fork cannot share rock");
					goto out;
				}
			}
			rs_shm_lock(rs->shm);
			rs->shm->refcnt++;
			rs_shm_unlock(rs->shm);
		}
	}

	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();
}