+ if (applet->need_suid == _BB_SUID_ALWAYS) {
+ if (geteuid()) bb_error_msg_and_die("Applet requires root privileges!");
+ } else if (applet->need_suid == _BB_SUID_NEVER) {
+ xsetgid(rgid); /* drop all privileges */
+ xsetuid(ruid);
+ }
+}
+#else
+#define check_suid(x)
+#endif /* CONFIG_FEATURE_SUID */
+
+
+
+#if ENABLE_FEATURE_COMPRESS_USAGE
+
+#include "usage_compressed.h"
+#include "unarchive.h"
+
+static const char *unpack_usage_messages(void)
+{
+ int input[2], output[2], pid;
+ char *buf;
+
+ if(pipe(input) < 0 || pipe(output) < 0)
+ exit(1);
+
+ pid = fork();
+ switch (pid) {
+ case -1: /* error */
+ exit(1);
+ case 0: /* child */
+ close(input[1]);
+ close(output[0]);
+ uncompressStream(input[0], output[1]);
+ exit(0);
+ }
+ /* parent */
+
+ close(input[0]);
+ close(output[1]);
+ pid = fork();
+ switch (pid) {
+ case -1: /* error */
+ exit(1);
+ case 0: /* child */
+ full_write(input[1], packed_usage, sizeof(packed_usage));
+ exit(0);
+ }
+ /* parent */
+ close(input[1]);
+
+ buf = xmalloc(SIZEOF_usage_messages);
+ full_read(output[0], buf, SIZEOF_usage_messages);
+ return buf;
+}
+
+#else
+#define unpack_usage_messages() usage_messages
+#endif /* ENABLE_FEATURE_COMPRESS_USAGE */
+
+void bb_show_usage (void)
+{
+ if (ENABLE_SHOW_USAGE) {
+ const char *format_string;
+ const char *usage_string = unpack_usage_messages();
+ int i;
+
+ for (i = applet_using - applets; i > 0;)
+ if (!*usage_string++) --i;
+
+ format_string = "%s\n\nUsage: %s %s\n\n";
+ if (*usage_string == '\b')
+ format_string = "%s\n\nNo help available.\n\n";
+ fprintf (stderr, format_string, bb_msg_full_version,
+ applet_using->name, usage_string);
+ }
+
+ exit (bb_default_error_retval);
+}
+
+static int applet_name_compare(const void *name, const void *vapplet)
+{
+ const struct BB_applet *applet = vapplet;
+
+ return strcmp(name, applet->name);
+}
+
+extern const size_t NUM_APPLETS;
+
+struct BB_applet *find_applet_by_name(const char *name)
+{
+ return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet),
+ applet_name_compare);
+}
+
+void run_applet_by_name(const char *name, int argc, char **argv)
+{
+ if (ENABLE_FEATURE_SUID_CONFIG) parse_config_file();
+
+ if (!strncmp(name, "busybox", 7)) busybox_main(argc, argv);
+ /* Do a binary search to find the applet entry given the name. */
+ applet_using = find_applet_by_name(name);
+ if (applet_using) {
+ bb_applet_name = applet_using->name;
+ if(argc==2 && !strcmp(argv[1], "--help")) bb_show_usage();
+ if(ENABLE_FEATURE_SUID) check_suid(applet_using);
+ exit((*(applet_using->main))(argc, argv));
+ }
+}