18290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley#include <unistd.h>
28290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley#include <stdio.h>
38290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley#include <stdlib.h>
48290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley#include <errno.h>
58290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley#include <selinux/selinux.h>
6ae6f3d7c05070f7e0e56fe0056c8923c6ee2f473Stephen Smalley#include <selinux/android.h>
78290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley
88290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalleystatic const char *progname;
98290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley
108290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalleystatic void usage(void)
118290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley{
12500308a7e67bfd4b918604a81b8125b8e7c2f37cStephen Smalley    fprintf(stderr, "usage:  %s [-DFnrRv] pathname...\n", progname);
138290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    exit(1);
148290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley}
158290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley
168290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalleyint restorecon_main(int argc, char **argv)
178290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley{
182761b71d3fafbf8f104d591dd4013f2a729e2400Stephen Smalley    int ch, i, rc;
192761b71d3fafbf8f104d591dd4013f2a729e2400Stephen Smalley    unsigned int flags = 0;
208290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley
218290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    progname = argv[0];
228290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley
238290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    do {
24500308a7e67bfd4b918604a81b8125b8e7c2f37cStephen Smalley        ch = getopt(argc, argv, "DFnrRv");
258290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley        if (ch == EOF)
268290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley            break;
278290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley        switch (ch) {
28500308a7e67bfd4b918604a81b8125b8e7c2f37cStephen Smalley        case 'D':
29500308a7e67bfd4b918604a81b8125b8e7c2f37cStephen Smalley            flags |= SELINUX_ANDROID_RESTORECON_DATADATA;
30500308a7e67bfd4b918604a81b8125b8e7c2f37cStephen Smalley            break;
312761b71d3fafbf8f104d591dd4013f2a729e2400Stephen Smalley        case 'F':
322761b71d3fafbf8f104d591dd4013f2a729e2400Stephen Smalley            flags |= SELINUX_ANDROID_RESTORECON_FORCE;
332761b71d3fafbf8f104d591dd4013f2a729e2400Stephen Smalley            break;
348290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley        case 'n':
352761b71d3fafbf8f104d591dd4013f2a729e2400Stephen Smalley            flags |= SELINUX_ANDROID_RESTORECON_NOCHANGE;
368290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley            break;
378290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley        case 'r':
388290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley        case 'R':
392761b71d3fafbf8f104d591dd4013f2a729e2400Stephen Smalley            flags |= SELINUX_ANDROID_RESTORECON_RECURSE;
408290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley            break;
418290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley        case 'v':
422761b71d3fafbf8f104d591dd4013f2a729e2400Stephen Smalley            flags |= SELINUX_ANDROID_RESTORECON_VERBOSE;
438290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley            break;
448290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley        default:
458290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley            usage();
468290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley        }
478290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    } while (1);
488290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley
498290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    argc -= optind;
508290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    argv += optind;
518290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    if (!argc)
528290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley        usage();
538290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley
542761b71d3fafbf8f104d591dd4013f2a729e2400Stephen Smalley    for (i = 0; i < argc; i++) {
5527a93650c0df02e4cd3c48bbec8acee8b817a012Stephen Smalley        rc = selinux_android_restorecon(argv[i], flags);
562761b71d3fafbf8f104d591dd4013f2a729e2400Stephen Smalley        if (rc < 0)
572761b71d3fafbf8f104d591dd4013f2a729e2400Stephen Smalley            fprintf(stderr, "Could not restorecon %s:  %s\n", argv[i],
582761b71d3fafbf8f104d591dd4013f2a729e2400Stephen Smalley                    strerror(errno));
598290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    }
608290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley
618290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley    return 0;
628290d1083ec7eee3f32265012f5d6be2774c4afcStephen Smalley}
63