]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/checkpoint.c
From: Daniel Lezcano <daniel.lezcano@free.fr>
[mirror_lxc.git] / src / lxc / checkpoint.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #undef _GNU_SOURCE
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/param.h>
35 #include <sys/file.h>
36 #include <netinet/in.h>
37 #include <net/if.h>
38
39 #include "error.h"
40 #include "lxc_plugin.h"
41 #include <lxc.h>
42
43 #define MAXPIDLEN 20
44
45 int lxc_checkpoint(const char *name, const char *statefile,
46 unsigned long flags)
47 {
48 char init[MAXPATHLEN];
49 char val[MAXPIDLEN];
50 int fd, lock, ret = -1;
51 size_t pid;
52
53 lock = lxc_get_lock(name);
54 if (lock >= 0) {
55 lxc_put_lock(lock);
56 return -LXC_ERROR_ESRCH;
57 }
58
59 if (lock < 0 && lock != -LXC_ERROR_EBUSY)
60 return lock;
61
62 snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);
63 fd = open(init, O_RDONLY);
64 if (fd < 0) {
65 lxc_log_syserror("failed to open init file for %s", name);
66 goto out_close;
67 }
68
69 if (read(fd, val, sizeof(val)) < 0) {
70 lxc_log_syserror("failed to read %s", init);
71 goto out_close;
72 }
73
74 pid = atoi(val);
75
76 if (lxc_plugin_checkpoint(pid, statefile, flags) < 0) {
77 lxc_log_syserror("failed to checkpoint %zd", pid);
78 goto out_close;
79 }
80
81 ret = 0;
82
83 out_close:
84 close(fd);
85 return ret;
86 }