]>
Commit | Line | Data |
---|---|---|
3c22086f CLG |
1 | /* |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
9afe19d6 | 7 | * Daniel Lezcano <daniel.lezcano at free.fr> |
3c22086f CLG |
8 | * |
9 | * This library is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; either | |
12 | * version 2.1 of the License, or (at your option) any later version. | |
13 | * | |
14 | * This library is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public | |
20 | * License along with this library; if not, write to the Free Software | |
250b1eec | 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
3c22086f CLG |
22 | */ |
23 | ||
d38dd64a CB |
24 | #ifndef _GNU_SOURCE |
25 | #define _GNU_SOURCE 1 | |
26 | #endif | |
3c22086f CLG |
27 | #include <errno.h> |
28 | #include <fcntl.h> | |
d38dd64a CB |
29 | #include <sys/socket.h> |
30 | #include <sys/types.h> | |
31 | #include <unistd.h> | |
3c22086f | 32 | |
d38dd64a | 33 | #include "config.h" |
3c22086f CLG |
34 | #include "log.h" |
35 | #include "start.h" | |
d38dd64a | 36 | #include "sync.h" |
ba38ff8e | 37 | #include "utils.h" |
3c22086f | 38 | |
ac2cecc4 | 39 | lxc_log_define(sync, lxc); |
3c22086f CLG |
40 | |
41 | static int __sync_wait(int fd, int sequence) | |
42 | { | |
43 | int sync = -1; | |
d38c8d16 | 44 | ssize_t ret; |
3c22086f | 45 | |
ba38ff8e | 46 | ret = lxc_read_nointr(fd, &sync, sizeof(sync)); |
3c22086f | 47 | if (ret < 0) { |
6d1400b5 | 48 | SYSERROR("Sync wait failure"); |
3c22086f CLG |
49 | return -1; |
50 | } | |
51 | ||
52 | if (!ret) | |
53 | return 0; | |
54 | ||
d38c8d16 | 55 | if ((size_t)ret != sizeof(sync)) { |
6d1400b5 | 56 | ERROR("Unexpected sync size: %zu expected %zu", (size_t)ret, sizeof(sync)); |
fcd59cf0 TA |
57 | return -1; |
58 | } | |
59 | ||
ea720ff1 AM |
60 | if (sync == LXC_SYNC_ERROR) { |
61 | ERROR("An error occurred in another process " | |
62 | "(expected sequence number %d)", sequence); | |
63 | return -1; | |
64 | } | |
65 | ||
3c22086f | 66 | if (sync != sequence) { |
abbec8b4 | 67 | ERROR("Invalid sequence number %d. Expected sequence number %d", |
3c22086f CLG |
68 | sync, sequence); |
69 | return -1; | |
70 | } | |
71 | return 0; | |
72 | } | |
73 | ||
74 | static int __sync_wake(int fd, int sequence) | |
75 | { | |
76 | int sync = sequence; | |
77 | ||
03876f90 | 78 | if (lxc_write_nointr(fd, &sync, sizeof(sync)) < 0) { |
6d1400b5 | 79 | SYSERROR("Sync wake failure"); |
3c22086f CLG |
80 | return -1; |
81 | } | |
82 | return 0; | |
83 | } | |
84 | ||
85 | static int __sync_barrier(int fd, int sequence) | |
86 | { | |
87 | if (__sync_wake(fd, sequence)) | |
88 | return -1; | |
89 | return __sync_wait(fd, sequence+1); | |
90 | } | |
91 | ||
92 | int lxc_sync_barrier_parent(struct lxc_handler *handler, int sequence) | |
93 | { | |
35a02107 | 94 | return __sync_barrier(handler->sync_sock[0], sequence); |
3c22086f CLG |
95 | } |
96 | ||
97 | int lxc_sync_barrier_child(struct lxc_handler *handler, int sequence) | |
98 | { | |
35a02107 | 99 | return __sync_barrier(handler->sync_sock[1], sequence); |
3c22086f CLG |
100 | } |
101 | ||
102 | int lxc_sync_wake_parent(struct lxc_handler *handler, int sequence) | |
103 | { | |
35a02107 | 104 | return __sync_wake(handler->sync_sock[0], sequence); |
3c22086f CLG |
105 | } |
106 | ||
5b1e83cb SH |
107 | int lxc_sync_wait_parent(struct lxc_handler *handler, int sequence) |
108 | { | |
35a02107 | 109 | return __sync_wait(handler->sync_sock[0], sequence); |
5b1e83cb SH |
110 | } |
111 | ||
3c22086f CLG |
112 | int lxc_sync_wait_child(struct lxc_handler *handler, int sequence) |
113 | { | |
35a02107 | 114 | return __sync_wait(handler->sync_sock[1], sequence); |
3c22086f CLG |
115 | } |
116 | ||
117 | int lxc_sync_wake_child(struct lxc_handler *handler, int sequence) | |
118 | { | |
35a02107 | 119 | return __sync_wake(handler->sync_sock[1], sequence); |
3c22086f CLG |
120 | } |
121 | ||
122 | int lxc_sync_init(struct lxc_handler *handler) | |
123 | { | |
025ed0f3 SH |
124 | int ret; |
125 | ||
35a02107 | 126 | ret = socketpair(AF_LOCAL, SOCK_STREAM, 0, handler->sync_sock); |
025ed0f3 | 127 | if (ret) { |
3c22086f CLG |
128 | SYSERROR("failed to create synchronization socketpair"); |
129 | return -1; | |
130 | } | |
131 | ||
132 | /* Be sure we don't inherit this after the exec */ | |
35a02107 | 133 | fcntl(handler->sync_sock[0], F_SETFD, FD_CLOEXEC); |
3c22086f CLG |
134 | |
135 | return 0; | |
136 | } | |
137 | ||
138 | void lxc_sync_fini_child(struct lxc_handler *handler) | |
139 | { | |
35a02107 CB |
140 | if (handler->sync_sock[0] != -1) { |
141 | close(handler->sync_sock[0]); | |
142 | handler->sync_sock[0] = -1; | |
3c22086f CLG |
143 | } |
144 | } | |
145 | ||
146 | void lxc_sync_fini_parent(struct lxc_handler *handler) | |
147 | { | |
35a02107 CB |
148 | if (handler->sync_sock[1] != -1) { |
149 | close(handler->sync_sock[1]); | |
150 | handler->sync_sock[1] = -1; | |
3c22086f CLG |
151 | } |
152 | } | |
153 | ||
154 | void lxc_sync_fini(struct lxc_handler *handler) | |
155 | { | |
156 | lxc_sync_fini_child(handler); | |
157 | lxc_sync_fini_parent(handler); | |
158 | } |