blob: cb5799e1ac3da7a01fbb13c0b7dc39f940b3f180 [file] [log] [blame]
Stephen Smalley8290d102012-01-13 08:53:56 -05001#include <unistd.h>
2#include <stdio.h>
3#include <stdlib.h>
Elliott Hughesa744b052015-01-28 11:37:57 -08004#include <string.h>
Stephen Smalley8290d102012-01-13 08:53:56 -05005#include <errno.h>
Stephen Smalley8290d102012-01-13 08:53:56 -05006#include <selinux/selinux.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -04007#include <selinux/android.h>
Stephen Smalley8290d102012-01-13 08:53:56 -05008
Stephen Smalley8290d102012-01-13 08:53:56 -05009static const char *progname;
Stephen Smalley8290d102012-01-13 08:53:56 -050010
11static void usage(void)
12{
Stephen Smalley500308a2014-02-18 11:15:52 -050013 fprintf(stderr, "usage: %s [-DFnrRv] pathname...\n", progname);
Stephen Smalley8290d102012-01-13 08:53:56 -050014 exit(1);
15}
16
Stephen Smalley8290d102012-01-13 08:53:56 -050017int restorecon_main(int argc, char **argv)
18{
Stephen Smalley2761b712014-01-30 10:15:16 -050019 int ch, i, rc;
20 unsigned int flags = 0;
Stephen Smalley8290d102012-01-13 08:53:56 -050021
22 progname = argv[0];
23
24 do {
Stephen Smalley500308a2014-02-18 11:15:52 -050025 ch = getopt(argc, argv, "DFnrRv");
Stephen Smalley8290d102012-01-13 08:53:56 -050026 if (ch == EOF)
27 break;
28 switch (ch) {
Stephen Smalley500308a2014-02-18 11:15:52 -050029 case 'D':
30 flags |= SELINUX_ANDROID_RESTORECON_DATADATA;
31 break;
Stephen Smalley2761b712014-01-30 10:15:16 -050032 case 'F':
33 flags |= SELINUX_ANDROID_RESTORECON_FORCE;
34 break;
Stephen Smalley8290d102012-01-13 08:53:56 -050035 case 'n':
Stephen Smalley2761b712014-01-30 10:15:16 -050036 flags |= SELINUX_ANDROID_RESTORECON_NOCHANGE;
Stephen Smalley8290d102012-01-13 08:53:56 -050037 break;
38 case 'r':
39 case 'R':
Stephen Smalley2761b712014-01-30 10:15:16 -050040 flags |= SELINUX_ANDROID_RESTORECON_RECURSE;
Stephen Smalley8290d102012-01-13 08:53:56 -050041 break;
42 case 'v':
Stephen Smalley2761b712014-01-30 10:15:16 -050043 flags |= SELINUX_ANDROID_RESTORECON_VERBOSE;
Stephen Smalley8290d102012-01-13 08:53:56 -050044 break;
45 default:
46 usage();
47 }
48 } while (1);
49
50 argc -= optind;
51 argv += optind;
52 if (!argc)
53 usage();
54
Stephen Smalley2761b712014-01-30 10:15:16 -050055 for (i = 0; i < argc; i++) {
Stephen Smalley27a93652014-02-07 09:14:13 -050056 rc = selinux_android_restorecon(argv[i], flags);
Stephen Smalley2761b712014-01-30 10:15:16 -050057 if (rc < 0)
58 fprintf(stderr, "Could not restorecon %s: %s\n", argv[i],
59 strerror(errno));
Stephen Smalley8290d102012-01-13 08:53:56 -050060 }
61
62 return 0;
63}