]>
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 | ||
24 | #include <sys/types.h> | |
25 | #include <sys/socket.h> | |
26 | #include <unistd.h> | |
27 | #include <errno.h> | |
28 | #include <fcntl.h> | |
29 | ||
30 | #include "log.h" | |
31 | #include "start.h" | |
32 | ||
33 | lxc_log_define(lxc_sync, lxc); | |
34 | ||
35 | static int __sync_wait(int fd, int sequence) | |
36 | { | |
37 | int sync = -1; | |
38 | int ret; | |
39 | ||
40 | ret = read(fd, &sync, sizeof(sync)); | |
41 | if (ret < 0) { | |
42 | ERROR("sync wait failure : %m"); | |
43 | return -1; | |
44 | } | |
45 | ||
46 | if (!ret) | |
47 | return 0; | |
48 | ||
49 | if (sync != sequence) { | |
50 | ERROR("invalid sequence number %d. expected %d", | |
51 | sync, sequence); | |
52 | return -1; | |
53 | } | |
54 | return 0; | |
55 | } | |
56 | ||
57 | static int __sync_wake(int fd, int sequence) | |
58 | { | |
59 | int sync = sequence; | |
60 | ||
61 | if (write(fd, &sync, sizeof(sync)) < 0) { | |
62 | ERROR("sync wake failure : %m"); | |
63 | return -1; | |
64 | } | |
65 | return 0; | |
66 | } | |
67 | ||
68 | static int __sync_barrier(int fd, int sequence) | |
69 | { | |
70 | if (__sync_wake(fd, sequence)) | |
71 | return -1; | |
72 | return __sync_wait(fd, sequence+1); | |
73 | } | |
74 | ||
75 | int lxc_sync_barrier_parent(struct lxc_handler *handler, int sequence) | |
76 | { | |
77 | return __sync_barrier(handler->sv[0], sequence); | |
78 | } | |
79 | ||
80 | int lxc_sync_barrier_child(struct lxc_handler *handler, int sequence) | |
81 | { | |
82 | return __sync_barrier(handler->sv[1], sequence); | |
83 | } | |
84 | ||
85 | int lxc_sync_wake_parent(struct lxc_handler *handler, int sequence) | |
86 | { | |
87 | return __sync_wake(handler->sv[0], sequence); | |
88 | } | |
89 | ||
90 | int lxc_sync_wait_child(struct lxc_handler *handler, int sequence) | |
91 | { | |
92 | return __sync_wait(handler->sv[1], sequence); | |
93 | } | |
94 | ||
95 | int lxc_sync_wake_child(struct lxc_handler *handler, int sequence) | |
96 | { | |
97 | return __sync_wake(handler->sv[1], sequence); | |
98 | } | |
99 | ||
100 | int lxc_sync_init(struct lxc_handler *handler) | |
101 | { | |
025ed0f3 SH |
102 | int ret; |
103 | ||
025ed0f3 | 104 | ret = socketpair(AF_LOCAL, SOCK_STREAM, 0, handler->sv); |
025ed0f3 | 105 | if (ret) { |
3c22086f CLG |
106 | SYSERROR("failed to create synchronization socketpair"); |
107 | return -1; | |
108 | } | |
109 | ||
110 | /* Be sure we don't inherit this after the exec */ | |
111 | fcntl(handler->sv[0], F_SETFD, FD_CLOEXEC); | |
112 | ||
113 | return 0; | |
114 | } | |
115 | ||
116 | void lxc_sync_fini_child(struct lxc_handler *handler) | |
117 | { | |
118 | if (handler->sv[0] != -1) { | |
119 | close(handler->sv[0]); | |
120 | handler->sv[0] = -1; | |
121 | } | |
122 | } | |
123 | ||
124 | void lxc_sync_fini_parent(struct lxc_handler *handler) | |
125 | { | |
126 | if (handler->sv[1] != -1) { | |
127 | close(handler->sv[1]); | |
128 | handler->sv[1] = -1; | |
129 | } | |
130 | } | |
131 | ||
132 | void lxc_sync_fini(struct lxc_handler *handler) | |
133 | { | |
134 | lxc_sync_fini_child(handler); | |
135 | lxc_sync_fini_parent(handler); | |
136 | } |