summaryrefslogtreecommitdiffstats
path: root/toolbox
diff options
context:
space:
mode:
authorSujith Ramakrishnan <sujithrk@google.com>2014-01-14 16:57:22 -0800
committerColin Cross <ccross@android.com>2014-06-11 11:10:28 -0700
commit032d4e298b0eb8e55e9963c7f3699acc30527c85 (patch)
treeb13c137ff44d2afa932ff3c104e2edf4e62ef599 /toolbox
parent140b13546ac30a836e39ad4f22da042b3fcb57a4 (diff)
downloadsystem_core-032d4e298b0eb8e55e9963c7f3699acc30527c85.zip
system_core-032d4e298b0eb8e55e9963c7f3699acc30527c85.tar.gz
system_core-032d4e298b0eb8e55e9963c7f3699acc30527c85.tar.bz2
Add mknod utility to system/core/toolbox
Change-Id: I46586cba83ecb8095721be8ffad9e386d4698e4a
Diffstat (limited to 'toolbox')
-rw-r--r--toolbox/Android.mk1
-rw-r--r--toolbox/mknod.c95
2 files changed, 96 insertions, 0 deletions
diff --git a/toolbox/Android.mk b/toolbox/Android.mk
index 6bac5f5..c53f17d 100644
--- a/toolbox/Android.mk
+++ b/toolbox/Android.mk
@@ -33,6 +33,7 @@ TOOLS := \
lsof \
md5 \
mkdir \
+ mknod \
mkswap \
mount \
mv \
diff --git a/toolbox/mknod.c b/toolbox/mknod.c
new file mode 100644
index 0000000..0fedece
--- /dev/null
+++ b/toolbox/mknod.c
@@ -0,0 +1,95 @@
+/*
+ * Copyright (c) 2014, The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google, Inc. nor the names of its contributors
+ * may be used to endorse or promote products derived from this
+ * software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/stat.h>
+
+static int print_usage() {
+ fprintf(stderr, "mknod <path> [b|c|u|p] <major> <minor>\n");
+ return EXIT_FAILURE;
+}
+
+int mknod_main(int argc, char **argv) {
+ char *path = NULL;
+ int major = 0;
+ int minor = 0;
+ int args = 0;
+ mode_t mode = 0660;
+
+ /* Check correct argument count is 3 or 5 */
+ if (argc != 3 && argc != 5) {
+ fprintf(stderr, "Incorrect argument count\n");
+ return print_usage();
+ }
+
+ path = argv[1];
+
+ const char node_type = *argv[2];
+ switch (node_type) {
+ case 'b':
+ mode |= S_IFBLK;
+ args = 5;
+ break;
+ case 'c':
+ case 'u':
+ mode |= S_IFCHR;
+ args = 5;
+ break;
+ case 'p':
+ mode |= S_IFIFO;
+ args = 3;
+ break;
+ default:
+ fprintf(stderr, "Invalid node type '%c'\n", node_type);
+ return print_usage();
+ }
+
+ if (argc != args) {
+ if (args == 5) {
+ fprintf(stderr, "Node type '%c' requires <major> and <minor>\n", node_type);
+ } else {
+ fprintf(stderr, "Node type '%c' does not require <major> and <minor>\n", node_type);
+ }
+ return print_usage();
+ }
+
+ if (args == 5) {
+ major = atoi(argv[3]);
+ minor = atoi(argv[4]);
+ }
+
+ if (mknod(path, mode, makedev(major, minor))) {
+ perror("Unable to create node");
+ return EXIT_FAILURE;
+ }
+ return 0;
+}