]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/lxc_attach.c
fix lxc-attach returned error
[mirror_lxc.git] / src / lxc / lxc_attach.c
CommitLineData
81c75799
DL
1/*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2010
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
24#include <unistd.h>
25#include <errno.h>
26#include <sys/param.h>
27#include <pwd.h>
28#include "commands.h"
29#include "arguments.h"
30#include "namespace.h"
31#include "log.h"
32
33lxc_log_define(lxc_attach_ui, lxc);
34
35static const struct option my_longopts[] = {
36 LXC_COMMON_OPTIONS
37};
38
39static struct lxc_arguments my_args = {
40 .progname = "lxc-attach",
41 .help = "\
42--name=NAME\n\
43\n\
44Execute the specified command - enter the container NAME\n\
45\n\
46Options :\n\
47 -n, --name=NAME NAME for name of the container\n",
48 .options = my_longopts,
49 .parser = NULL,
50 .checker = NULL,
51};
52
53pid_t get_init_pid(const char *name)
54{
55 struct lxc_command command = {
56 .request = { .type = LXC_COMMAND_PID },
57 };
58
59 int ret, stopped = 0;
60
61 ret = lxc_command(name, &command, &stopped);
62 if (ret < 0 && stopped) {
63 INFO("'%s' is already stopped", name);
64 return 0;
65 }
66
67 if (ret < 0) {
68 ERROR("failed to send command");
69 return -1;
70 }
71
72 if (command.answer.ret) {
73 ERROR("failed to retrieve the init pid: %s",
0a3ec350 74 strerror(-command.answer.ret));
81c75799
DL
75 return -1;
76 }
77
78 return command.answer.pid;
79}
80
81int main(int argc, char *argv[], char *envp[])
82{
83 int ret;
84 pid_t pid;
85 struct passwd *passwd;
86 uid_t uid;
87
88 ret = lxc_arguments_parse(&my_args, argc, argv);
89 if (ret)
90 return ret;
91
92 ret = lxc_log_init(my_args.log_file, my_args.log_priority,
93 my_args.progname, my_args.quiet);
94 if (ret)
95 return ret;
96
97 pid = get_init_pid(my_args.name);
98 if (pid < 0) {
99 ERROR("failed to get the init pid");
100 return -1;
101 }
102
103 ret = lxc_attach(pid);
104 if (ret < 0) {
105 ERROR("failed to enter the namespace");
106 return -1;
107 }
108
109 if (my_args.argc) {
110 execve(my_args.argv[0], my_args.argv, envp);
111 SYSERROR("failed to exec '%s'", my_args.argv[0]);
112 return -1;
113 }
114
115 uid = getuid();
116
117 passwd = getpwuid(uid);
118 if (!passwd) {
119 SYSERROR("failed to get passwd entry for uid '%d'", uid);
120 return -1;
121 }
122
123 {
124 char *const args[] = {
125 passwd->pw_shell,
126 NULL,
127 };
128
129 execve(args[0], args, envp);
130 SYSERROR("failed to exec '%s'", args[0]);
131 return -1;
132 }
133
134 return 0;
135}