• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ubus/tests/test-ubusd.h

  1 #ifndef __TEST_UBUSD_H
  2 #define __TEST_UBUSD_H
  3 
  4 #include <fcntl.h>
  5 #include <limits.h>
  6 #include <sched.h>
  7 #include <signal.h>
  8 #include <stdio.h>
  9 #include <stdlib.h>
 10 #include <string.h>
 11 #include <sys/prctl.h>
 12 #include <sys/stat.h>
 13 #include <sys/wait.h>
 14 #include <unistd.h>
 15 
 16 #include "libubus.h"
 17 
 18 #define TEST_SKIPPED    77
 19 
 20 static const char *ubusd_bin;
 21 static char sock_path[PATH_MAX];
 22 static char sock_dir[] = "/tmp/ubus-test-XXXXXX";
 23 static volatile pid_t ubusd_pid;
 24 static int failures;
 25 
 26 #define CHECK(cond) do {                                                \
 27         if (!(cond)) {                                                  \
 28                 fprintf(stderr, "%s:%d: check failed: %s\n",            \
 29                         __FILE__, __LINE__, #cond);                     \
 30                 failures++;                                             \
 31         }                                                               \
 32 } while (0)
 33 
 34 static inline void ubusd_start(void)
 35 {
 36         struct ubus_context *probe;
 37         int i;
 38 
 39         unlink(sock_path);
 40         ubusd_pid = fork();
 41         if (ubusd_pid < 0)
 42                 exit(2);
 43 
 44         if (!ubusd_pid) {
 45                 /* a test that crashes must not leave ubusd behind */
 46                 prctl(PR_SET_PDEATHSIG, SIGKILL);
 47                 execl(ubusd_bin, ubusd_bin, "-s", sock_path, NULL);
 48                 _exit(127);
 49         }
 50 
 51         for (i = 0; i < 100; i++) {
 52                 probe = ubus_connect(sock_path);
 53                 if (probe) {
 54                         ubus_free(probe);
 55                         return;
 56                 }
 57                 usleep(50 * 1000);
 58         }
 59 
 60         fprintf(stderr, "ubusd did not start\n");
 61         exit(2);
 62 }
 63 
 64 static inline void ubusd_reap(void)
 65 {
 66         waitpid(ubusd_pid, NULL, 0);
 67         ubusd_pid = 0;
 68         unlink(sock_path);
 69 }
 70 
 71 static inline void ubusd_kill(void)
 72 {
 73         kill(ubusd_pid, SIGKILL);
 74         ubusd_reap();
 75 }
 76 
 77 static inline void ubusd_kill_alarm(int sig)
 78 {
 79         kill(ubusd_pid, SIGKILL);
 80 }
 81 
 82 static inline bool object_visible(const char *name)
 83 {
 84         struct ubus_context *ctx = ubus_connect(sock_path);
 85         uint32_t id = 0;
 86         int ret;
 87 
 88         if (!ctx)
 89                 return false;
 90 
 91         /* ubusd answers OK with no id for an object that has no type */
 92         ret = ubus_lookup_id(ctx, name, &id);
 93         ubus_free(ctx);
 94 
 95         return !ret && id;
 96 }
 97 
 98 static inline bool proc_file_write(const char *path, const char *data)
 99 {
100         ssize_t len = strlen(data);
101         bool ret;
102         int fd;
103 
104         fd = open(path, O_WRONLY);
105         if (fd < 0)
106                 return false;
107 
108         ret = write(fd, data, len) == len;
109         close(fd);
110 
111         return ret;
112 }
113 
114 /*
115  * ubusd applies its ACLs to every client that is not root, and it loads only
116  * ACL files that root owns.
117  */
118 static inline bool userns_enter(void)
119 {
120         uid_t uid = getuid();
121         gid_t gid = getgid();
122         char map[32];
123 
124         if (!uid)
125                 return true;
126 
127         if (unshare(CLONE_NEWUSER))
128                 return false;
129 
130         snprintf(map, sizeof(map), "0 %u 1", (unsigned int) uid);
131         if (!proc_file_write("/proc/self/uid_map", map) ||
132             !proc_file_write("/proc/self/setgroups", "deny"))
133                 return false;
134 
135         snprintf(map, sizeof(map), "0 %u 1", (unsigned int) gid);
136 
137         return proc_file_write("/proc/self/gid_map", map);
138 }
139 
140 static inline int test_ubusd_init(int argc, char **argv)
141 {
142         if (argc != 2) {
143                 fprintf(stderr, "usage: %s <ubusd>\n", argv[0]);
144                 return 2;
145         }
146 
147         if (!userns_enter()) {
148                 fprintf(stderr, "not root and no user namespace, skipped\n");
149                 return TEST_SKIPPED;
150         }
151 
152         ubusd_bin = argv[1];
153         signal(SIGPIPE, SIG_IGN);
154 
155         if (!mkdtemp(sock_dir))
156                 return 2;
157 
158         snprintf(sock_path, sizeof(sock_path), "%s/ubus.sock", sock_dir);
159 
160         return 0;
161 }
162 
163 static inline int test_ubusd_done(void)
164 {
165         rmdir(sock_dir);
166 
167         if (failures)
168                 fprintf(stderr, "%d check(s) failed\n", failures);
169 
170         return failures ? 1 : 0;
171 }
172 
173 #endif
174 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt