Matus Marhefka | a201bcf | 2014-10-02 14:29:35 +0200 | [diff] [blame] | 1 | /* Copyright (c) 2014 Red Hat, Inc. |
| 2 | * |
| 3 | * This program is free software: you can redistribute it and/or modify |
| 4 | * it under the terms of version 2 the GNU General Public License as |
| 5 | * published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 | *********************************************************************** |
| 15 | * File: mountns04.c |
| 16 | * |
| 17 | * Tests an unbindable mount: unbindable mount is an unbindable |
| 18 | * private mount. |
| 19 | * Description: |
| 20 | * 1. Creates directories "A", "B" and files "A/A", "B/B" |
| 21 | * 2. Unshares mount namespace and makes it private (so mounts/umounts |
| 22 | * have no effect on a real system) |
| 23 | * 3. Bind mounts directory "A" to "A" |
| 24 | * 4. Makes directory directory "A" unbindable |
| 25 | * 5. Tries to bind mount unbindable "A" to "B": |
| 26 | * - if it fails, test passes |
| 27 | * - if it passes, test fails |
| 28 | ***********************************************************************/ |
| 29 | |
| 30 | #define _GNU_SOURCE |
| 31 | #include <sys/wait.h> |
| 32 | #include <sys/mount.h> |
| 33 | #include <stdio.h> |
| 34 | #include <errno.h> |
| 35 | #include "test.h" |
Matus Marhefka | a201bcf | 2014-10-02 14:29:35 +0200 | [diff] [blame] | 36 | #include "libclone.h" |
| 37 | #include "safe_macros.h" |
| 38 | #include "safe_file_ops.h" |
| 39 | #include "mountns_helper.h" |
| 40 | |
| 41 | |
| 42 | char *TCID = "mountns04"; |
| 43 | int TST_TOTAL = 1; |
| 44 | |
| 45 | |
| 46 | #if defined(MS_SHARED) && defined(MS_PRIVATE) \ |
| 47 | && defined(MS_REC) && defined(MS_UNBINDABLE) |
| 48 | |
| 49 | static void test(void) |
| 50 | { |
| 51 | /* unshares the mount ns */ |
| 52 | if (unshare(CLONE_NEWNS) == -1) |
| 53 | tst_brkm(TBROK | TERRNO, cleanup, "unshare failed"); |
| 54 | /* makes sure mounts/umounts have no effect on a real system */ |
| 55 | SAFE_MOUNT(cleanup, "none", "/", "none", MS_REC|MS_PRIVATE, NULL); |
| 56 | |
| 57 | /* bind mounts DIRA to itself */ |
| 58 | SAFE_MOUNT(cleanup, DIRA, DIRA, "none", MS_BIND, NULL); |
| 59 | /* makes mount DIRA unbindable */ |
| 60 | SAFE_MOUNT(cleanup, "none", DIRA, "none", MS_UNBINDABLE, NULL); |
| 61 | |
| 62 | /* tries to bind mount unbindable DIRA to DIRB which should fail */ |
| 63 | if (mount(DIRA, DIRB, "none", MS_BIND, NULL) == -1) { |
| 64 | tst_resm(TPASS, "unbindable mount passed"); |
| 65 | } else { |
| 66 | SAFE_UMOUNT(cleanup, DIRB); |
| 67 | tst_resm(TFAIL, "unbindable mount faled"); |
| 68 | } |
| 69 | |
| 70 | SAFE_UMOUNT(cleanup, DIRA); |
| 71 | } |
| 72 | |
| 73 | int main(int argc, char *argv[]) |
| 74 | { |
| 75 | const char *msg; |
| 76 | int lc; |
| 77 | |
| 78 | msg = parse_opts(argc, argv, NULL, NULL); |
| 79 | if (msg != NULL) |
| 80 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 81 | |
| 82 | setup(); |
| 83 | |
| 84 | for (lc = 0; TEST_LOOPING(lc); lc++) |
| 85 | test(); |
| 86 | |
| 87 | cleanup(); |
| 88 | tst_exit(); |
| 89 | } |
| 90 | |
| 91 | #else |
| 92 | int main(void) |
| 93 | { |
| 94 | tst_brkm(TCONF, NULL, "needed mountflags are not defined"); |
| 95 | } |
| 96 | #endif |