blob: 11532da694b8aa4857b10b0129f45d18b044e9a2 [file] [log] [blame]
Stephen Smalley8290d102012-01-13 08:53:56 -05001#include <unistd.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <errno.h>
Stephen Smalley8290d102012-01-13 08:53:56 -05005#include <selinux/selinux.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -04006#include <selinux/android.h>
Stephen Smalley8290d102012-01-13 08:53:56 -05007
Stephen Smalley8290d102012-01-13 08:53:56 -05008static const char *progname;
Stephen Smalley8290d102012-01-13 08:53:56 -05009
10static void usage(void)
11{
Stephen Smalley2761b712014-01-30 10:15:16 -050012 fprintf(stderr, "usage: %s [-FnrRv] pathname...\n", progname);
Stephen Smalley8290d102012-01-13 08:53:56 -050013 exit(1);
14}
15
Stephen Smalley8290d102012-01-13 08:53:56 -050016int restorecon_main(int argc, char **argv)
17{
Stephen Smalley2761b712014-01-30 10:15:16 -050018 int ch, i, rc;
19 unsigned int flags = 0;
Stephen Smalley8290d102012-01-13 08:53:56 -050020
21 progname = argv[0];
22
23 do {
Stephen Smalley2761b712014-01-30 10:15:16 -050024 ch = getopt(argc, argv, "FnrRv");
Stephen Smalley8290d102012-01-13 08:53:56 -050025 if (ch == EOF)
26 break;
27 switch (ch) {
Stephen Smalley2761b712014-01-30 10:15:16 -050028 case 'F':
29 flags |= SELINUX_ANDROID_RESTORECON_FORCE;
30 break;
Stephen Smalley8290d102012-01-13 08:53:56 -050031 case 'n':
Stephen Smalley2761b712014-01-30 10:15:16 -050032 flags |= SELINUX_ANDROID_RESTORECON_NOCHANGE;
Stephen Smalley8290d102012-01-13 08:53:56 -050033 break;
34 case 'r':
35 case 'R':
Stephen Smalley2761b712014-01-30 10:15:16 -050036 flags |= SELINUX_ANDROID_RESTORECON_RECURSE;
Stephen Smalley8290d102012-01-13 08:53:56 -050037 break;
38 case 'v':
Stephen Smalley2761b712014-01-30 10:15:16 -050039 flags |= SELINUX_ANDROID_RESTORECON_VERBOSE;
Stephen Smalley8290d102012-01-13 08:53:56 -050040 break;
41 default:
42 usage();
43 }
44 } while (1);
45
46 argc -= optind;
47 argv += optind;
48 if (!argc)
49 usage();
50
Stephen Smalley2761b712014-01-30 10:15:16 -050051 for (i = 0; i < argc; i++) {
Stephen Smalley27a93652014-02-07 09:14:13 -050052 rc = selinux_android_restorecon(argv[i], flags);
Stephen Smalley2761b712014-01-30 10:15:16 -050053 if (rc < 0)
54 fprintf(stderr, "Could not restorecon %s: %s\n", argv[i],
55 strerror(errno));
Stephen Smalley8290d102012-01-13 08:53:56 -050056 }
57
58 return 0;
59}