blob: 22d5831d3d34a569dfd6d32387f921e3d9b3ff6d [file] [log] [blame]
Sandeep Patil9b9e9772017-04-14 19:05:50 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <libgen.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <unistd.h>
22
23#define SYSTEM_DIRNAME "/system/bin/"
24
25// List of net utils wrapped by this program
26// The list MUST be in descending order of string length
27const char *netcmds[] = {
28 "ip6tables",
29 "iptables",
30 "ndc",
31 "tc",
32 "ip",
33 NULL,
34};
35
36// This is the only gateway for vendor programs to reach net utils.
37int main(int /* argc */, char **argv) {
38 char *progname = argv[0];
39 char *basename = NULL;
40
41 basename = strrchr(progname, '/');
42 basename = basename ? basename + 1 : progname;
43
44 for (int i = 0; netcmds[i]; ++i) {
45 size_t len = strlen(netcmds[i]);
46 if (!strncmp(basename, netcmds[i], len)) {
47 // truncate to match netcmds[i]
48 basename[len] = '\0';
49
50 // hardcode the path to /system so it cannot be overwritten
51 char *cmd;
52 if (asprintf(&cmd, "%s%s", SYSTEM_DIRNAME, basename) == -1) {
53 perror("asprintf");
54 exit(EXIT_FAILURE);
55 }
56 argv[0] = cmd;
57 execv(cmd, argv);
58 }
59 }
60
61 // must never reach here
62 fprintf(stderr, "(%s:%d) is not a supported net util\n", progname, errno);
63 exit(EXIT_FAILURE);
64}