]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxclock.c
3 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2012 Canonical Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #define OFLAG (O_CREAT | O_RDWR)
27 #define SEMVALUE_LOCKED 0
28 #define LXCLOCK_PREFIX "/lxcapi."
31 static char *lxclock_name(const char *container
)
34 int len
= strlen(container
) + strlen(LXCLOCK_PREFIX
) + 1;
35 char *dest
= malloc(len
);
38 ret
= snprintf(dest
, len
, "%s%s", LXCLOCK_PREFIX
, container
);
39 if (ret
< 0 || ret
>= len
) {
46 static void lxcfree_name(char *name
)
52 static sem_t
*lxc_new_unnamed_sem(void)
57 s
= malloc(sizeof(*s
));
60 ret
= sem_init(s
, 0, 1);
68 sem_t
*lxc_newlock(const char *name
)
74 return lxc_new_unnamed_sem();
76 lname
= lxclock_name(name
);
79 lock
= sem_open(lname
, OFLAG
, SEMMODE
, SEMVALUE
);
81 if (lock
== SEM_FAILED
)
86 int lxclock(sem_t
*sem
, int timeout
)
94 if (clock_gettime(CLOCK_REALTIME
, &ts
) == -1)
97 ret
= sem_timedwait(sem
, &ts
);
103 int lxcunlock(sem_t
*sem
)
107 return sem_post(sem
);