platform_system_core/toolbox/restorecon.c
Stephen Smalley 2761b71d3f Unify toolbox restorecon and libselinux restorecon implementations.
Extend the libselinux restorecon implementation to allow reuse
by the toolbox restorecon command.  This simply requires adding
support for the nochange (-n) and verbose (-v) options to the
libselinux functions and rewriting the toolbox restorecon command
to use the libselinux functions.   Also add a force (-F) option to
support forcing a restorecon_recursive even if the restorecon_last
attribute matches the current file_contexts hash so that we can
continue to force a restorecon via the toolbox command for testing
or when we know something else has changed (e.g. for when we support
relabeling /data/data and package information has changed).

Change-Id: I92bb3259790a7195ba56a5e9555c3b6c76ceb862
Signed-off-by: Stephen Smalley <sds@tycho.nsa.gov>
2014-02-06 09:42:04 -05:00

59 lines
1.3 KiB
C

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <selinux/selinux.h>
#include <selinux/android.h>
static const char *progname;
static void usage(void)
{
fprintf(stderr, "usage: %s [-FnrRv] pathname...\n", progname);
exit(1);
}
int restorecon_main(int argc, char **argv)
{
int ch, i, rc;
unsigned int flags = 0;
progname = argv[0];
do {
ch = getopt(argc, argv, "FnrRv");
if (ch == EOF)
break;
switch (ch) {
case 'F':
flags |= SELINUX_ANDROID_RESTORECON_FORCE;
break;
case 'n':
flags |= SELINUX_ANDROID_RESTORECON_NOCHANGE;
break;
case 'r':
case 'R':
flags |= SELINUX_ANDROID_RESTORECON_RECURSE;
break;
case 'v':
flags |= SELINUX_ANDROID_RESTORECON_VERBOSE;
break;
default:
usage();
}
} while (1);
argc -= optind;
argv += optind;
if (!argc)
usage();
for (i = 0; i < argc; i++) {
rc = selinux_android_restorecon_flags(argv[i], flags);
if (rc < 0)
fprintf(stderr, "Could not restorecon %s: %s\n", argv[i],
strerror(errno));
}
return 0;
}