#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
+#ifdef ENABLE_SYSTEMD
+#include <systemd/sd-daemon.h>
+#endif
#include <libubox/blob.h>
#include <libubox/uloop.h>
}
if (offset < sizeof(ub->hdr)) {
- iov[0].iov_base = ((char *) &ub->hdr) + offset;
- iov[0].iov_len = sizeof(ub->hdr) - offset;
+ struct ubus_msghdr hdr;
+
+ hdr.version = ub->hdr.version;
+ hdr.type = ub->hdr.type;
+ hdr.seq = cpu_to_be16(ub->hdr.seq);
+ hdr.peer = cpu_to_be32(ub->hdr.peer);
+
+ iov[0].iov_base = ((char *) &hdr) + offset;
+ iov[0].iov_len = sizeof(hdr) - offset;
iov[1].iov_base = (char *) ub->data;
iov[1].iov_len = ub->len;
if (!cl->pending_msg)
goto disconnect;
+ cl->hdrbuf.hdr.seq = be16_to_cpu(cl->hdrbuf.hdr.seq);
+ cl->hdrbuf.hdr.peer = be32_to_cpu(cl->hdrbuf.hdr.peer);
+
memcpy(&cl->pending_msg->hdr, &cl->hdrbuf.hdr, sizeof(cl->hdrbuf.hdr));
memcpy(cl->pending_msg->data, &cl->hdrbuf.data, sizeof(cl->hdrbuf.data));
}
{
fprintf(stderr, "Usage: %s [<options>]\n"
"Options: \n"
+ " -A <path>: Set the path to ACL files\n"
" -s <socket>: Set the unix domain socket to listen on\n"
"\n", progname);
return 1;
int main(int argc, char **argv)
{
const char *ubus_socket = UBUS_UNIX_SOCKET;
+ bool remove_socket = true;
int ret = 0;
int ch;
+#ifdef ENABLE_SYSTEMD
+ int n_fds;
+#endif
signal(SIGPIPE, SIG_IGN);
signal(SIGHUP, sighup_handler);
openlog("ubusd", LOG_PID, LOG_DAEMON);
uloop_init();
- while ((ch = getopt(argc, argv, "s:")) != -1) {
+ while ((ch = getopt(argc, argv, "A:s:")) != -1) {
switch (ch) {
case 's':
ubus_socket = optarg;
break;
+ case 'A':
+ ubusd_acl_dir = optarg;
+ break;
default:
return usage(argv[0]);
}
}
- unlink(ubus_socket);
- umask(0111);
- server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL);
- if (server_fd.fd < 0) {
- perror("usock");
- ret = -1;
+#ifdef ENABLE_SYSTEMD
+ n_fds = sd_listen_fds(1);
+ if (n_fds > 1) {
+ fprintf(stderr, "Too many file descriptors received.\n");
+ ret = -1;
goto out;
+ } else if (n_fds == 1) {
+ server_fd.fd = SD_LISTEN_FDS_START + 0;
+ fcntl(server_fd.fd, F_SETFD, fcntl(server_fd.fd, F_GETFD) | FD_CLOEXEC);
+ fcntl(server_fd.fd, F_SETFL, fcntl(server_fd.fd, F_GETFL) | O_NONBLOCK);
+
+ remove_socket = false;
+ } else
+#endif
+ {
+ unlink(ubus_socket);
+ umask(0111);
+ server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL);
+ if (server_fd.fd < 0) {
+ perror("usock");
+ ret = -1;
+ goto out;
+ }
}
uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
ubusd_acl_load();
uloop_run();
- unlink(ubus_socket);
+
+ if (remove_socket)
+ unlink(ubus_socket);
out:
uloop_done();