/
sync.c
155 lines (130 loc) · 3.54 KB
/
sync.c
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
/*
* lxc: linux Container library
*
* (C) Copyright IBM Corp. 2007, 2008
*
* Authors:
* Daniel Lezcano <daniel.lezcano at free.fr>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include "sync.h"
#include "log.h"
#include "start.h"
#include "utils.h"
lxc_log_define(lxc_sync, lxc);
static int __sync_wait(int fd, int sequence)
{
int sync = -1;
ssize_t ret;
ret = lxc_read_nointr(fd, &sync, sizeof(sync));
if (ret < 0) {
ERROR("sync wait failure : %s", strerror(errno));
return -1;
}
if (!ret)
return 0;
if ((size_t)ret != sizeof(sync)) {
ERROR("unexpected sync size: %zu expected %zu", (size_t)ret, sizeof(sync));
return -1;
}
if (sync == LXC_SYNC_ERROR) {
ERROR("An error occurred in another process "
"(expected sequence number %d)", sequence);
return -1;
}
if (sync != sequence) {
ERROR("invalid sequence number %d. expected %d",
sync, sequence);
return -1;
}
return 0;
}
static int __sync_wake(int fd, int sequence)
{
int sync = sequence;
if (lxc_write_nointr(fd, &sync, sizeof(sync)) < 0) {
SYSERROR("Sync wake failure");
return -1;
}
return 0;
}
static int __sync_barrier(int fd, int sequence)
{
if (__sync_wake(fd, sequence))
return -1;
return __sync_wait(fd, sequence+1);
}
int lxc_sync_barrier_parent(struct lxc_handler *handler, int sequence)
{
return __sync_barrier(handler->sync_sock[0], sequence);
}
int lxc_sync_barrier_child(struct lxc_handler *handler, int sequence)
{
return __sync_barrier(handler->sync_sock[1], sequence);
}
int lxc_sync_wake_parent(struct lxc_handler *handler, int sequence)
{
return __sync_wake(handler->sync_sock[0], sequence);
}
int lxc_sync_wait_parent(struct lxc_handler *handler, int sequence)
{
return __sync_wait(handler->sync_sock[0], sequence);
}
int lxc_sync_wait_child(struct lxc_handler *handler, int sequence)
{
return __sync_wait(handler->sync_sock[1], sequence);
}
int lxc_sync_wake_child(struct lxc_handler *handler, int sequence)
{
return __sync_wake(handler->sync_sock[1], sequence);
}
int lxc_sync_init(struct lxc_handler *handler)
{
int ret;
ret = socketpair(AF_LOCAL, SOCK_STREAM, 0, handler->sync_sock);
if (ret) {
SYSERROR("failed to create synchronization socketpair");
return -1;
}
/* Be sure we don't inherit this after the exec */
fcntl(handler->sync_sock[0], F_SETFD, FD_CLOEXEC);
return 0;
}
void lxc_sync_fini_child(struct lxc_handler *handler)
{
if (handler->sync_sock[0] != -1) {
close(handler->sync_sock[0]);
handler->sync_sock[0] = -1;
}
}
void lxc_sync_fini_parent(struct lxc_handler *handler)
{
if (handler->sync_sock[1] != -1) {
close(handler->sync_sock[1]);
handler->sync_sock[1] = -1;
}
}
void lxc_sync_fini(struct lxc_handler *handler)
{
lxc_sync_fini_child(handler);
lxc_sync_fini_parent(handler);
}