12e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes/* getprop.c - Get an Android system property
22e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes *
32e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes * Copyright 2015 The Android Open Source Project
42e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes
5b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom CherryUSE_GETPROP(NEWTOY(getprop, ">2Z", TOYFLAG_USR|TOYFLAG_SBIN))
62e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes
72e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughesconfig GETPROP
82e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes  bool "getprop"
92e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes  default y
105b493dc48db03c7e27c8ce002fe216bcd778fe92Rob Landley  depends on TOYBOX_ON_ANDROID
112e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes  help
122e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes    usage: getprop [NAME [DEFAULT]]
132e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes
142e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes    Gets an Android system property, or lists them all.
152e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes*/
162e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes
172e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes#define FOR_getprop
182e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes#include "toys.h"
192e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes
202e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes#if defined(__ANDROID__)
212e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes
222e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes#include <cutils/properties.h>
232e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes
24b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry#include <selinux/android.h>
25b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry#include <selinux/label.h>
26b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry#include <selinux/selinux.h>
27b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry
282e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott HughesGLOBALS(
292e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes  size_t size;
305b493dc48db03c7e27c8ce002fe216bcd778fe92Rob Landley  char **nv; // name/value pairs: even=name, odd=value
31b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry  struct selabel_handle *handle;
322e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes)
332e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes
34b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherrystatic char *get_property_context(char *property)
35b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry{
36b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry  char *context = NULL;
37b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry
38b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry  if (selabel_lookup(TT.handle, &context, property, 1)) {
39b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry    perror_exit("unable to lookup label for \"%s\"", property);
40b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry  }
41b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry  return context;
42b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry}
43b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry
445b493dc48db03c7e27c8ce002fe216bcd778fe92Rob Landleystatic void add_property(char *name, char *value, void *unused)
452e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes{
465b493dc48db03c7e27c8ce002fe216bcd778fe92Rob Landley  if (!(TT.size&31)) TT.nv = xrealloc(TT.nv, (TT.size+32)*2*sizeof(char *));
472e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes
485b493dc48db03c7e27c8ce002fe216bcd778fe92Rob Landley  TT.nv[2*TT.size] = xstrdup(name);
49b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry  if (toys.optflags & FLAG_Z) {
50b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry    TT.nv[1+2*TT.size++] = get_property_context(name);
51b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry  } else {
52b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry    TT.nv[1+2*TT.size++] = xstrdup(value);
53b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry  }
54b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry}
55b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry
56b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry// Needed to supress extraneous "Loaded property_contexts from" message
57b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherryint selinux_log_callback(int type, const char *fmt, ...) {
58b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry  va_list ap;
59b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry
60b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry  if (type == SELINUX_INFO) return 0;
61b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry  va_start(ap, fmt);
62b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry  verror_msg(fmt, 0, ap);
63b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry  va_end(ap);
64b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry  return 0;
652e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes}
662e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes
672e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughesvoid getprop_main(void)
682e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes{
69b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry  if (toys.optflags & FLAG_Z) {
70b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry    union selinux_callback cb;
71b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry
72b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry    cb.func_log = selinux_log_callback;
73b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry    selinux_set_callback(SELINUX_CB_LOG, cb);
74b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry    TT.handle = selinux_android_prop_context_handle();
75b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry    if (!TT.handle) error_exit("unable to get selinux property context handle");
76b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry  }
77b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry
782e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes  if (*toys.optargs) {
79b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry    if (toys.optflags & FLAG_Z) {
80b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry      char *context = get_property_context(*toys.optargs);
81b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry
82b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry      puts(context);
83b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry      if (CFG_TOYBOX_FREE) free(context);
84b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry    } else {
85b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry      property_get(*toys.optargs, toybuf, toys.optargs[1] ? toys.optargs[1] : "");
86b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry      puts(toybuf);
87b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry    }
882e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes  } else {
892e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes    size_t i;
902e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes
912a7727dec265fa96e8522b0a557b72320d2889a7Elliott Hughes    if (property_list((void *)add_property, 0)) error_exit("property_list");
922a7727dec265fa96e8522b0a557b72320d2889a7Elliott Hughes    qsort(TT.nv, TT.size, 2*sizeof(char *), qstrcmp);
935b493dc48db03c7e27c8ce002fe216bcd778fe92Rob Landley    for (i = 0; i<TT.size; i++) printf("[%s]: [%s]\n", TT.nv[i*2],TT.nv[1+i*2]);
94b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry    if (CFG_TOYBOX_FREE) {
95b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry      for (i = 0; i<TT.size; i++) {
96b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry        free(TT.nv[i*2]);
97b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry        free(TT.nv[1+i*2]);
98b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry      }
99b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry      free(TT.nv);
100b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry    }
1012e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes  }
102b66a29ab58c686c98ab9683c5c544f40a1ea35f5Tom Cherry  if (CFG_TOYBOX_FREE && (toys.optflags & FLAG_Z)) selabel_close(TT.handle);
1032e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes}
1042e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes
1052e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes#else
1062e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes
1072e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughesvoid getprop_main(void)
1082e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes{
1092e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes}
1102e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes
1112e4c96533c1b7ffb5f303c9589a6b4a1de6be560Elliott Hughes#endif
112