Revision bfdbb708 mnexec.c

View differences:

mnexec.c
140 140
            fflush(stdout);
141 141
            break;
142 142
        case 'a':
143
            /* Attach to pid's network namespace and mount namespace*/
143
            /* Attach to pid's network namespace and mount namespace */
144 144
            pid = atoi(optarg);
145
            sprintf(path, "/proc/%d/ns/net", pid );
145
            sprintf(path, "/proc/%d/ns/net", pid);
146 146
            nsid = open(path, O_RDONLY);
147 147
            if (nsid < 0) {
148 148
                perror(path);
......
152 152
                perror("setns");
153 153
                return 1;
154 154
            }
155
            sprintf(path, "/proc/%d/ns/mnt", pid );
155
            /* Plan A: call setns() to attach to mount namespace */
156
            sprintf(path, "/proc/%d/ns/mnt", pid);
156 157
            nsid = open(path, O_RDONLY);
157
            if (nsid < 0) {
158
                perror(path);
159
                return 1;
160
            }
161
            if (setns(nsid, 0) != 0) {
162
                perror("setns");
163
                return 1;
158
            if (nsid < 0 || setns(nsid, 0) != 0) {
159
                /* Plan B: chroot into pid's root file system */
160
                sprintf(path, "/proc/%d/root", pid);
161
                if (chroot(path) < 0) {
162
                    perror(path);
163
                    return 1;
164
                }
164 165
            }
165 166
            break;
166 167
        case 'g':

Also available in: Unified diff