blob: 476f564a515f3bb9d9622514445e091d1063ac73 [file] [log] [blame]
Eric Paris5ef65fd2011-06-28 19:40:26 -04001#include <unistd.h>
2#include <sys/types.h>
3#include <fcntl.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <errno.h>
7#include <string.h>
8#include <ctype.h>
9#include <selinux/flask.h>
10#include <selinux/selinux.h>
11
Daniel P. Berrange5f8ce372012-01-23 15:41:19 +000012static void usage(const char *name, const char *detail, int rc)
Eric Paris5ef65fd2011-06-28 19:40:26 -040013{
14 fprintf(stderr, "usage: %s command [ fromcon ]\n", name);
15 if (detail)
16 fprintf(stderr, "%s: %s\n", name, detail);
17 exit(rc);
18}
19
20static security_context_t get_selinux_proc_context(const char *command, security_context_t execcon) {
21 security_context_t fcon = NULL, newcon = NULL;
22
23 int ret = getfilecon(command, &fcon);
24 if (ret < 0) goto err;
25 ret = security_compute_create(execcon, fcon, SECCLASS_PROCESS, &newcon);
26 if (ret < 0) goto err;
27
28err:
29 freecon(fcon);
30 return newcon;
31}
32
33int main(int argc, char **argv)
34{
35 int ret = -1;
36 security_context_t proccon = NULL, con = NULL;
37 if (argc < 2 || argc > 3)
38 usage(argv[0], "Invalid number of arguments", -1);
39
40 if (argc == 2) {
41 if (getcon(&con) < 0) {
42 perror(argv[0]);
43 return -1;
44 }
45 } else {
46 con = strdup(argv[2]);
47 }
48
49 proccon = get_selinux_proc_context(argv[1], con);
50 if (proccon) {
51 printf("%s\n", proccon);
52 ret = 0;
53 } else {
54 perror(argv[0]);
55 }
56
57 free(proccon);
58 free(con);
59 return ret;
60}