xref: /aosp_15_r20/external/selinux/libselinux/utils/getpidcon.c (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <selinux/selinux.h>
7 
main(int argc,char ** argv)8 int main(int argc, char **argv)
9 {
10 	pid_t pid;
11 	char *buf;
12 	int rc;
13 
14 	if (argc != 2) {
15 		fprintf(stderr, "usage:  %s pid\n", argv[0]);
16 		exit(1);
17 	}
18 
19 	if (sscanf(argv[1], "%d", &pid) != 1) {
20 		fprintf(stderr, "%s:  invalid pid %s\n", argv[0], argv[1]);
21 		exit(2);
22 	}
23 
24 	rc = getpidcon(pid, &buf);
25 	if (rc < 0) {
26 		fprintf(stderr, "%s:  getpidcon() failed:  %s\n", argv[0], strerror(errno));
27 		exit(3);
28 	}
29 
30 	printf("%s\n", buf);
31 	freecon(buf);
32 	exit(EXIT_SUCCESS);
33 }
34