]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/sync.c
sync: fail on unexpected message sizes
[mirror_lxc.git] / src / lxc / sync.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
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
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 "sync.h"
31 #include "log.h"
32 #include "start.h"
33
34 lxc_log_define(lxc_sync, lxc);
35
36 static int __sync_wait(int fd, int sequence)
37 {
38 int sync = -1;
39 int ret;
40
41 ret = read(fd, &sync, sizeof(sync));
42 if (ret < 0) {
43 ERROR("sync wait failure : %m");
44 return -1;
45 }
46
47 if (!ret)
48 return 0;
49
50 if (ret != sizeof(sync)) {
51 ERROR("unexpected sync size: %d expected %lu", ret, sizeof(sync));
52 return -1;
53 }
54
55 if (sync == LXC_SYNC_ERROR) {
56 ERROR("An error occurred in another process "
57 "(expected sequence number %d)", sequence);
58 return -1;
59 }
60
61 if (sync != sequence) {
62 ERROR("invalid sequence number %d. expected %d",
63 sync, sequence);
64 return -1;
65 }
66 return 0;
67 }
68
69 static int __sync_wake(int fd, int sequence)
70 {
71 int sync = sequence;
72
73 if (write(fd, &sync, sizeof(sync)) < 0) {
74 ERROR("sync wake failure : %m");
75 return -1;
76 }
77 return 0;
78 }
79
80 static int __sync_barrier(int fd, int sequence)
81 {
82 if (__sync_wake(fd, sequence))
83 return -1;
84 return __sync_wait(fd, sequence+1);
85 }
86
87 int lxc_sync_barrier_parent(struct lxc_handler *handler, int sequence)
88 {
89 return __sync_barrier(handler->sv[0], sequence);
90 }
91
92 int lxc_sync_barrier_child(struct lxc_handler *handler, int sequence)
93 {
94 return __sync_barrier(handler->sv[1], sequence);
95 }
96
97 int lxc_sync_wake_parent(struct lxc_handler *handler, int sequence)
98 {
99 return __sync_wake(handler->sv[0], sequence);
100 }
101
102 int lxc_sync_wait_child(struct lxc_handler *handler, int sequence)
103 {
104 return __sync_wait(handler->sv[1], sequence);
105 }
106
107 int lxc_sync_wake_child(struct lxc_handler *handler, int sequence)
108 {
109 return __sync_wake(handler->sv[1], sequence);
110 }
111
112 int lxc_sync_init(struct lxc_handler *handler)
113 {
114 int ret;
115
116 ret = socketpair(AF_LOCAL, SOCK_STREAM, 0, handler->sv);
117 if (ret) {
118 SYSERROR("failed to create synchronization socketpair");
119 return -1;
120 }
121
122 /* Be sure we don't inherit this after the exec */
123 fcntl(handler->sv[0], F_SETFD, FD_CLOEXEC);
124
125 return 0;
126 }
127
128 void lxc_sync_fini_child(struct lxc_handler *handler)
129 {
130 if (handler->sv[0] != -1) {
131 close(handler->sv[0]);
132 handler->sv[0] = -1;
133 }
134 }
135
136 void lxc_sync_fini_parent(struct lxc_handler *handler)
137 {
138 if (handler->sv[1] != -1) {
139 close(handler->sv[1]);
140 handler->sv[1] = -1;
141 }
142 }
143
144 void lxc_sync_fini(struct lxc_handler *handler)
145 {
146 lxc_sync_fini_child(handler);
147 lxc_sync_fini_parent(handler);
148 }