]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxc_attach.c
conditional use of new capabilities
[mirror_lxc.git] / src / lxc / lxc_attach.c
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 #define _GNU_SOURCE
25 #include <unistd.h>
26 #include <errno.h>
27 #include <pwd.h>
28 #include <stdlib.h>
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/wait.h>
32
33 #include "commands.h"
34 #include "arguments.h"
35 #include "namespace.h"
36 #include "log.h"
37
38 lxc_log_define(lxc_attach_ui, lxc);
39
40 static const struct option my_longopts[] = {
41 LXC_COMMON_OPTIONS
42 };
43
44 static struct lxc_arguments my_args = {
45 .progname = "lxc-attach",
46 .help = "\
47 --name=NAME\n\
48 \n\
49 Execute the specified command - enter the container NAME\n\
50 \n\
51 Options :\n\
52 -n, --name=NAME NAME for name of the container\n",
53 .options = my_longopts,
54 .parser = NULL,
55 .checker = NULL,
56 };
57
58 int main(int argc, char *argv[], char *envp[])
59 {
60 int ret;
61 pid_t pid;
62 struct passwd *passwd;
63 uid_t uid;
64 char *curdir;
65
66 ret = lxc_arguments_parse(&my_args, argc, argv);
67 if (ret)
68 return ret;
69
70 ret = lxc_log_init(my_args.log_file, my_args.log_priority,
71 my_args.progname, my_args.quiet);
72 if (ret)
73 return ret;
74
75 pid = get_init_pid(my_args.name);
76 if (pid < 0) {
77 ERROR("failed to get the init pid");
78 return -1;
79 }
80
81 curdir = get_current_dir_name();
82
83 ret = lxc_attach(pid);
84 if (ret < 0) {
85 ERROR("failed to enter the namespace");
86 return -1;
87 }
88
89 if (curdir && chdir(curdir))
90 WARN("could not change directory to '%s'", curdir);
91
92 free(curdir);
93
94 pid = fork();
95
96 if (pid < 0) {
97 SYSERROR("failed to fork");
98 return -1;
99 }
100
101 if (pid) {
102 int status;
103
104 again:
105 if (waitpid(pid, &status, 0) < 0) {
106 if (errno == EINTR)
107 goto again;
108 SYSERROR("failed to wait '%d'", pid);
109 return -1;
110 }
111
112 if (WIFEXITED(status))
113 return WEXITSTATUS(status);
114
115 return -1;
116 }
117
118 if (!pid) {
119
120 if (my_args.argc) {
121 execve(my_args.argv[0], my_args.argv, envp);
122 SYSERROR("failed to exec '%s'", my_args.argv[0]);
123 return -1;
124 }
125
126 uid = getuid();
127
128 passwd = getpwuid(uid);
129 if (!passwd) {
130 SYSERROR("failed to get passwd " \
131 "entry for uid '%d'", uid);
132 return -1;
133 }
134
135 {
136 char *const args[] = {
137 passwd->pw_shell,
138 NULL,
139 };
140
141 execve(args[0], args, envp);
142 SYSERROR("failed to exec '%s'", args[0]);
143 return -1;
144 }
145
146 }
147
148 return 0;
149 }