]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/sync.c
log: change ERROR macro using sterror to SYSERROR
[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 #define _GNU_SOURCE
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <fcntl.h>
30
31 #include "sync.h"
32 #include "log.h"
33 #include "start.h"
34
35 lxc_log_define(lxc_sync, lxc);
36
37 static int __sync_wait(int fd, int sequence)
38 {
39 int sync = -1;
40 ssize_t ret;
41
42 ret = read(fd, &sync, sizeof(sync));
43 if (ret < 0) {
44 SYSERROR("Sync wait failure");
45 return -1;
46 }
47
48 if (!ret)
49 return 0;
50
51 if ((size_t)ret != sizeof(sync)) {
52 ERROR("Unexpected sync size: %zu expected %zu", (size_t)ret, sizeof(sync));
53 return -1;
54 }
55
56 if (sync == LXC_SYNC_ERROR) {
57 ERROR("An error occurred in another process "
58 "(expected sequence number %d)", sequence);
59 return -1;
60 }
61
62 if (sync != sequence) {
63 ERROR("Invalid sequence number %d. expected %d",
64 sync, sequence);
65 return -1;
66 }
67 return 0;
68 }
69
70 static int __sync_wake(int fd, int sequence)
71 {
72 int sync = sequence;
73
74 if (write(fd, &sync, sizeof(sync)) < 0) {
75 SYSERROR("Sync wake failure");
76 return -1;
77 }
78 return 0;
79 }
80
81 static int __sync_barrier(int fd, int sequence)
82 {
83 if (__sync_wake(fd, sequence))
84 return -1;
85 return __sync_wait(fd, sequence+1);
86 }
87
88 int lxc_sync_barrier_parent(struct lxc_handler *handler, int sequence)
89 {
90 return __sync_barrier(handler->sync_sock[0], sequence);
91 }
92
93 int lxc_sync_barrier_child(struct lxc_handler *handler, int sequence)
94 {
95 return __sync_barrier(handler->sync_sock[1], sequence);
96 }
97
98 int lxc_sync_wake_parent(struct lxc_handler *handler, int sequence)
99 {
100 return __sync_wake(handler->sync_sock[0], sequence);
101 }
102
103 int lxc_sync_wait_parent(struct lxc_handler *handler, int sequence)
104 {
105 return __sync_wait(handler->sync_sock[0], sequence);
106 }
107
108 int lxc_sync_wait_child(struct lxc_handler *handler, int sequence)
109 {
110 return __sync_wait(handler->sync_sock[1], sequence);
111 }
112
113 int lxc_sync_wake_child(struct lxc_handler *handler, int sequence)
114 {
115 return __sync_wake(handler->sync_sock[1], sequence);
116 }
117
118 int lxc_sync_init(struct lxc_handler *handler)
119 {
120 int ret;
121
122 ret = socketpair(AF_LOCAL, SOCK_STREAM, 0, handler->sync_sock);
123 if (ret) {
124 SYSERROR("failed to create synchronization socketpair");
125 return -1;
126 }
127
128 /* Be sure we don't inherit this after the exec */
129 fcntl(handler->sync_sock[0], F_SETFD, FD_CLOEXEC);
130
131 return 0;
132 }
133
134 void lxc_sync_fini_child(struct lxc_handler *handler)
135 {
136 if (handler->sync_sock[0] != -1) {
137 close(handler->sync_sock[0]);
138 handler->sync_sock[0] = -1;
139 }
140 }
141
142 void lxc_sync_fini_parent(struct lxc_handler *handler)
143 {
144 if (handler->sync_sock[1] != -1) {
145 close(handler->sync_sock[1]);
146 handler->sync_sock[1] = -1;
147 }
148 }
149
150 void lxc_sync_fini(struct lxc_handler *handler)
151 {
152 lxc_sync_fini_child(handler);
153 lxc_sync_fini_parent(handler);
154 }