1 /* getprop.c - Get an Android system property
2  *
3  * Copyright 2015 The Android Open Source Project
4 
5 USE_GETPROP(NEWTOY(getprop, ">2Z", TOYFLAG_USR|TOYFLAG_SBIN))
6 
7 config GETPROP
8   bool "getprop"
9   default y
10   depends on TOYBOX_ON_ANDROID && TOYBOX_SELINUX
11   help
12     usage: getprop [NAME [DEFAULT]]
13 
14     Gets an Android system property, or lists them all.
15 */
16 
17 #define FOR_getprop
18 #include "toys.h"
19 
20 #include <sys/system_properties.h>
21 
22 #include <selinux/android.h>
23 #include <selinux/label.h>
24 #include <selinux/selinux.h>
25 
GLOBALS(size_t size;char ** nv;struct selabel_handle * handle;)26 GLOBALS(
27   size_t size;
28   char **nv; // name/value pairs: even=name, odd=value
29   struct selabel_handle *handle;
30 )
31 
32 static char *get_property_context(const char *property)
33 {
34   char *context = NULL;
35 
36   if (selabel_lookup(TT.handle, &context, property, 1)) {
37     perror_exit("unable to lookup label for \"%s\"", property);
38   }
39   return context;
40 }
41 
read_callback(void * unused,const char * name,const char * value)42 static void read_callback(void *unused, const char *name, const char *value)
43 {
44   if (!(TT.size&31)) TT.nv = xrealloc(TT.nv, (TT.size+32)*2*sizeof(char *));
45 
46   TT.nv[2*TT.size] = xstrdup((char *)name);
47   if (toys.optflags & FLAG_Z) {
48     TT.nv[1+2*TT.size++] = get_property_context(name);
49   } else {
50     TT.nv[1+2*TT.size++] = xstrdup((char *)value);
51   }
52 }
53 
add_property(const prop_info * pi,void * unused)54 static void add_property(const prop_info *pi, void *unused)
55 {
56   __system_property_read_callback(pi, read_callback, NULL);
57 }
58 
59 // Needed to supress extraneous "Loaded property_contexts from" message
selinux_log_callback_local(int type,const char * fmt,...)60 static int selinux_log_callback_local(int type, const char *fmt, ...)
61 {
62   va_list ap;
63 
64   if (type == SELINUX_INFO) return 0;
65   va_start(ap, fmt);
66   verror_msg((char *)fmt, 0, ap);
67   va_end(ap);
68   return 0;
69 }
70 
getprop_main(void)71 void getprop_main(void)
72 {
73   if (toys.optflags & FLAG_Z) {
74     union selinux_callback cb;
75 
76     cb.func_log = selinux_log_callback_local;
77     selinux_set_callback(SELINUX_CB_LOG, cb);
78     TT.handle = selinux_android_prop_context_handle();
79     if (!TT.handle) error_exit("unable to get selinux property context handle");
80   }
81 
82   if (*toys.optargs) {
83     if (toys.optflags & FLAG_Z) {
84       char *context = get_property_context(*toys.optargs);
85 
86       puts(context);
87       if (CFG_TOYBOX_FREE) free(context);
88     } else {
89       if (__system_property_get(*toys.optargs, toybuf) <= 0)
90         strcpy(toybuf, toys.optargs[1] ? toys.optargs[1] : "");
91       puts(toybuf);
92     }
93   } else {
94     size_t i;
95 
96     if (__system_property_foreach(add_property, NULL))
97       error_exit("property_list");
98     qsort(TT.nv, TT.size, 2*sizeof(char *), qstrcmp);
99     for (i = 0; i<TT.size; i++) printf("[%s]: [%s]\n", TT.nv[i*2],TT.nv[1+i*2]);
100     if (CFG_TOYBOX_FREE) {
101       for (i = 0; i<TT.size; i++) {
102         free(TT.nv[i*2]);
103         free(TT.nv[1+i*2]);
104       }
105       free(TT.nv);
106     }
107   }
108   if (CFG_TOYBOX_FREE && (toys.optflags & FLAG_Z)) selabel_close(TT.handle);
109 }
110