Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
Elliott Hughes | a744b05 | 2015-01-28 11:37:57 -0800 | [diff] [blame] | 4 | #include <string.h> |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 5 | #include <errno.h> |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 6 | #include <selinux/selinux.h> |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 7 | #include <selinux/android.h> |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 8 | |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 9 | static const char *progname; |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 10 | |
| 11 | static void usage(void) |
| 12 | { |
Stephen Smalley | 500308a | 2014-02-18 11:15:52 -0500 | [diff] [blame] | 13 | fprintf(stderr, "usage: %s [-DFnrRv] pathname...\n", progname); |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 14 | exit(1); |
| 15 | } |
| 16 | |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 17 | int restorecon_main(int argc, char **argv) |
| 18 | { |
Stephen Smalley | 2761b71 | 2014-01-30 10:15:16 -0500 | [diff] [blame] | 19 | int ch, i, rc; |
| 20 | unsigned int flags = 0; |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 21 | |
| 22 | progname = argv[0]; |
| 23 | |
| 24 | do { |
Stephen Smalley | 500308a | 2014-02-18 11:15:52 -0500 | [diff] [blame] | 25 | ch = getopt(argc, argv, "DFnrRv"); |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 26 | if (ch == EOF) |
| 27 | break; |
| 28 | switch (ch) { |
Stephen Smalley | 500308a | 2014-02-18 11:15:52 -0500 | [diff] [blame] | 29 | case 'D': |
| 30 | flags |= SELINUX_ANDROID_RESTORECON_DATADATA; |
| 31 | break; |
Stephen Smalley | 2761b71 | 2014-01-30 10:15:16 -0500 | [diff] [blame] | 32 | case 'F': |
| 33 | flags |= SELINUX_ANDROID_RESTORECON_FORCE; |
| 34 | break; |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 35 | case 'n': |
Stephen Smalley | 2761b71 | 2014-01-30 10:15:16 -0500 | [diff] [blame] | 36 | flags |= SELINUX_ANDROID_RESTORECON_NOCHANGE; |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 37 | break; |
| 38 | case 'r': |
| 39 | case 'R': |
Stephen Smalley | 2761b71 | 2014-01-30 10:15:16 -0500 | [diff] [blame] | 40 | flags |= SELINUX_ANDROID_RESTORECON_RECURSE; |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 41 | break; |
| 42 | case 'v': |
Stephen Smalley | 2761b71 | 2014-01-30 10:15:16 -0500 | [diff] [blame] | 43 | flags |= SELINUX_ANDROID_RESTORECON_VERBOSE; |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 44 | break; |
| 45 | default: |
| 46 | usage(); |
| 47 | } |
| 48 | } while (1); |
| 49 | |
| 50 | argc -= optind; |
| 51 | argv += optind; |
| 52 | if (!argc) |
| 53 | usage(); |
| 54 | |
Stephen Smalley | 2761b71 | 2014-01-30 10:15:16 -0500 | [diff] [blame] | 55 | for (i = 0; i < argc; i++) { |
Stephen Smalley | 27a9365 | 2014-02-07 09:14:13 -0500 | [diff] [blame] | 56 | rc = selinux_android_restorecon(argv[i], flags); |
Stephen Smalley | 2761b71 | 2014-01-30 10:15:16 -0500 | [diff] [blame] | 57 | if (rc < 0) |
| 58 | fprintf(stderr, "Could not restorecon %s: %s\n", argv[i], |
| 59 | strerror(errno)); |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | } |