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