~serge-hallyn/+junk/nsexec

« back to all changes in this revision

Viewing changes to usernsexec.c

  • Committer: Serge Hallyn
  • Date: 2013-03-12 16:58:34 UTC
  • Revision ID: serge.hallyn@canonical.com-20130312165834-4wy5w8r16vcodvtx
compile fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
#include <sys/stat.h>
16
16
#include <sys/types.h>
17
17
#include <sys/wait.h>
 
18
#include <sched.h>
18
19
 
19
20
#include "clone.h"
 
21
int unshare(int flags);
20
22
 
21
23
static const char* procname;
22
24
 
70
72
                perror("setgid");
71
73
                return -1;
72
74
        }
73
 
        if (setuid(0), < 0) {
 
75
        if (setuid(0) < 0) {
74
76
                perror("setuid");
75
77
                return -1;
76
78
        }
77
 
        ret = unshare(CLONE_NEWNS);
78
 
        if (ret < 0) {
 
79
        if (unshare(CLONE_NEWNS) < 0) {
79
80
                perror("unshare CLONE_NEWNS");
80
81
                return -1;
81
82
        }
118
119
 
119
120
static int run_cmd(char **argv)
120
121
{
 
122
    int status;
121
123
        pid_t pid = fork();
 
124
 
122
125
        if (pid < 0)
123
126
                return pid;
124
127
        if (pid == 0) {
125
 
                execv(argv[0], argv);
 
128
                execvp(argv[0], argv);
126
129
                perror("exec failed");
127
130
                exit(1);
128
131
        }
129
 
        if ((ret = waitpid(pid, &status, __WALL)) < 0) {
130
 
                printf("waitpid() returns %d, errno %d\n", ret, errno);
131
 
                return ret;
 
132
        if (waitpid(pid, &status, __WALL) < 0) {
 
133
        perror("waitpid");
 
134
                return -1;
132
135
        }
133
136
 
134
137
        return WEXITSTATUS(status);
136
139
 
137
140
static int map_child_uids(int pid, struct id_map *map)
138
141
{
139
 
        char **uidargs = NULL, gidargs = NULL;
140
 
        int nuargs = 2, ngargs = 2;
 
142
        char **uidargs = NULL, **gidargs = NULL;
 
143
        int i, nuargs = 2, ngargs = 2;
141
144
        struct id_map *m;
142
145
 
143
146
        uidargs = malloc(3 * sizeof(*uidargs));
144
147
        gidargs = malloc(3 * sizeof(*gidargs));
145
 
        if (!uidargs || !gidargs)
 
148
        if (uidargs == NULL || gidargs == NULL)
146
149
                return -1;
147
150
        uidargs[0] = malloc(10);
148
151
        gidargs[0] = malloc(10);
190
193
        }
191
194
 
192
195
        // exec newuidmap
193
 
        if (run_cmd(uidargs) < 0) {
 
196
        if (run_cmd(uidargs) != 0) {
194
197
                fprintf(stderr, "Error mapping uids\n");
195
198
                return -2;
196
199
        }
197
200
        // exec newgidmap
198
 
        if (run_cmd(gidargs) < 0) {
 
201
        if (run_cmd(gidargs) != 0) {
199
202
                fprintf(stderr, "Error mapping gids\n");
200
203
                return -2;
201
204
        }
206
209
                free(gidargs[i]);
207
210
        free(uidargs);
208
211
        free(gidargs);
 
212
 
 
213
    return 0;
209
214
}
210
215
 
211
216
int main(int argc, char *argv[])