~serge-hallyn/ubuntu/natty/lxc/lxc-fix-3bugs

« back to all changes in this revision

Viewing changes to src/lxc/stop.c

  • Committer: Bazaar Package Importer
  • Author(s): Guido Trotter, Stéphane Graber, Guido Trotter
  • Date: 2010-01-10 10:40:21 UTC
  • mfrom: (1.1.2 upstream) (3.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100110104021-z8rj5zw5mlvra08l
Tags: 0.6.4-1
[ Stéphane Graber ]
* Upgrade standards-version to 3.8.3
* Drop the copy of etc/* from rules as "etc" is no longer in the tarball

[ Guido Trotter ]
* New Upstream Version
* Update libcap2-dev dependency to libcap-dev
* Install upstream-built man pages via debian/lxc.manpages
* Drop unneeded docbook-utils build dependency

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
#include <sys/signal.h>
30
30
#include <sys/types.h>
31
31
#include <sys/stat.h>
 
32
#include <sys/socket.h>
32
33
#include <fcntl.h>
33
34
 
34
 
#include <lxc/lxc.h>
35
35
#include <lxc/log.h>
 
36
#include <lxc/start.h>
 
37
 
 
38
#include "commands.h"
36
39
 
37
40
lxc_log_define(lxc_stop, lxc);
38
41
 
39
 
#define MAXPIDLEN 20
40
 
 
41
42
int lxc_stop(const char *name)
42
43
{
43
 
        char init[MAXPATHLEN];
44
 
        char val[MAXPIDLEN];
45
 
        int fd, ret = -1;
46
 
        size_t pid;
47
 
 
48
 
        if (lxc_check_lock(name) < 0)
49
 
                return ret;
50
 
 
51
 
        snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);
52
 
        fd = open(init, O_RDONLY);
53
 
        if (fd < 0) {
54
 
                SYSERROR("failed to open init file for %s", name);
55
 
                goto out_close;
56
 
        }
57
 
 
58
 
        if (read(fd, val, sizeof(val)) < 0) {
59
 
                SYSERROR("failed to read %s", init);
60
 
                goto out_close;
61
 
        }
62
 
 
63
 
        pid = atoi(val);
64
 
 
65
 
        if (kill(pid, SIGKILL)) {
66
 
                SYSERROR("failed to kill %zd", pid);
67
 
                goto out_close;
68
 
        }
69
 
 
70
 
        ret = 0;
71
 
 
72
 
out_close:
73
 
        close(fd);
74
 
        return ret;
75
 
}
 
44
        struct lxc_command command = {
 
45
                .request = { .type = LXC_COMMAND_STOP },
 
46
        };
 
47
 
 
48
        int ret, stopped = 0;
 
49
 
 
50
        ret = lxc_command(name, &command,&stopped);
 
51
        if (ret < 0 && stopped) {
 
52
                INFO("'%s' is already stopped", name);
 
53
                return 0;
 
54
        }
 
55
 
 
56
        if (ret < 0) {
 
57
                ERROR("failed to send command");
 
58
                return -1;
 
59
        }
 
60
 
 
61
        /* we do not expect any answer, because we wait for the connection to be
 
62
         * closed
 
63
         */
 
64
        if (ret > 0) {
 
65
                ERROR("failed to stop '%s': %s",
 
66
                        name, strerror(-command.answer.ret));
 
67
                return -1;
 
68
        }
 
69
 
 
70
        INFO("'%s' has stopped", name);
 
71
 
 
72
        return 0;
 
73
}
 
74
 
 
75
/*----------------------------------------------------------------------------
 
76
 * functions used by lxc-start mainloop
 
77
 * to handle above command request.
 
78
 *--------------------------------------------------------------------------*/
 
79
extern int lxc_stop_callback(int fd, struct lxc_request *request,
 
80
                        struct lxc_handler *handler)
 
81
{
 
82
        struct lxc_answer answer;
 
83
        int ret;
 
84
 
 
85
        answer.ret = kill(handler->pid, SIGKILL);
 
86
        if (!answer.ret)
 
87
                return 0;
 
88
 
 
89
        ret = send(fd, &answer, sizeof(answer), 0);
 
90
        if (ret < 0) {
 
91
                WARN("failed to send answer to the peer");
 
92
                goto out;
 
93
        }
 
94
 
 
95
        if (ret != sizeof(answer)) {
 
96
                ERROR("partial answer sent");
 
97
                goto out;
 
98
        }
 
99
 
 
100
out:
 
101
        return -1;
 
102
}
 
103