blob: 35686254dc8899d7de48ff591b2bde108f24943e [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 Smalley500308a2014-02-18 11:15:52 -050012 fprintf(stderr, "usage: %s [-DFnrRv] 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 Smalley500308a2014-02-18 11:15:52 -050024 ch = getopt(argc, argv, "DFnrRv");
Stephen Smalley8290d102012-01-13 08:53:56 -050025 if (ch == EOF)
26 break;
27 switch (ch) {
Stephen Smalley500308a2014-02-18 11:15:52 -050028 case 'D':
29 flags |= SELINUX_ANDROID_RESTORECON_DATADATA;
30 break;
Stephen Smalley2761b712014-01-30 10:15:16 -050031 case 'F':
32 flags |= SELINUX_ANDROID_RESTORECON_FORCE;
33 break;
Stephen Smalley8290d102012-01-13 08:53:56 -050034 case 'n':
Stephen Smalley2761b712014-01-30 10:15:16 -050035 flags |= SELINUX_ANDROID_RESTORECON_NOCHANGE;
Stephen Smalley8290d102012-01-13 08:53:56 -050036 break;
37 case 'r':
38 case 'R':
Stephen Smalley2761b712014-01-30 10:15:16 -050039 flags |= SELINUX_ANDROID_RESTORECON_RECURSE;
Stephen Smalley8290d102012-01-13 08:53:56 -050040 break;
41 case 'v':
Stephen Smalley2761b712014-01-30 10:15:16 -050042 flags |= SELINUX_ANDROID_RESTORECON_VERBOSE;
Stephen Smalley8290d102012-01-13 08:53:56 -050043 break;
44 default:
45 usage();
46 }
47 } while (1);
48
49 argc -= optind;
50 argv += optind;
51 if (!argc)
52 usage();
53
Stephen Smalley2761b712014-01-30 10:15:16 -050054 for (i = 0; i < argc; i++) {
Stephen Smalley27a93652014-02-07 09:14:13 -050055 rc = selinux_android_restorecon(argv[i], flags);
Stephen Smalley2761b712014-01-30 10:15:16 -050056 if (rc < 0)
57 fprintf(stderr, "Could not restorecon %s: %s\n", argv[i],
58 strerror(errno));
Stephen Smalley8290d102012-01-13 08:53:56 -050059 }
60
61 return 0;
62}