Ubox
OpenWrt core utilities
logd.c File Reference
#include <sys/types.h>
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
#include <syslog.h>
#include <linux/types.h>
#include <libubox/uloop.h>
#include <libubox/blobmsg.h>
#include <libubox/list.h>
#include <libubox/ustream.h>
#include <libubox/utils.h>
#include <libubus.h>

Go to the source code of this file.

Data Structures

struct  client
 

Enumerations

enum  { READ_LINES , READ_STREAM , READ_ONESHOT , __READ_MAX }
 

Functions

static LIST_HEAD (clients)
 
static void client_close (struct ustream *s)
 
static void client_notify_state (struct ustream *s)
 
static void client_notify_write (struct ustream *s, int bytes)
 
static void log_fill_msg (struct blob_buf *b, struct log_head *l)
 
static int read_log (struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
 
static int write_log (struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
 
void ubus_notify_log (struct log_head *l)
 
static void ubus_connect_handler (struct ubus_context *ctx)
 
int main (int argc, char **argv)
 

Variables

int debug = 0
 
static struct blob_buf b
 
static struct ubus_auto_conn conn
 
static struct udebug_ubus udebug
 
static const struct blobmsg_policy read_policy [__READ_MAX]
 
static const struct blobmsg_policy write_policy
 
static const struct ubus_method log_methods []
 
static struct ubus_object_type log_object_type
 
static struct ubus_object log_object
 

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
READ_LINES 
READ_STREAM 
READ_ONESHOT 
__READ_MAX 

Definition at line 38 of file logd.c.

38  {
39  READ_LINES,
43 };
@ __READ_MAX
Definition: logd.c:42
@ READ_LINES
Definition: logd.c:39
@ READ_ONESHOT
Definition: logd.c:41
@ READ_STREAM
Definition: logd.c:40

Function Documentation

◆ client_close()

static void client_close ( struct ustream *  s)
static

Definition at line 62 of file logd.c.

63 {
64  struct client *cl = container_of(s, struct client, s.stream);
65 
66  list_del(&cl->list);
67  ustream_free(s);
68  close(cl->fd);
69  free(cl);
70 }
Definition: logd.c:54
int fd
Definition: logd.c:58
struct list_head list
Definition: logd.c:55
struct ustream_fd s
Definition: logd.c:57
Here is the caller graph for this function:

◆ client_notify_state()

static void client_notify_state ( struct ustream *  s)
static

Definition at line 72 of file logd.c.

73 {
74  client_close(s);
75 }
static void client_close(struct ustream *s)
Definition: logd.c:62
Here is the call graph for this function:
Here is the caller graph for this function:

◆ client_notify_write()

static void client_notify_write ( struct ustream *  s,
int  bytes 
)
static

Definition at line 77 of file logd.c.

78 {
79  if (ustream_pending_data(s, true))
80  return;
81 
82  client_close(s);
83 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ LIST_HEAD()

static LIST_HEAD ( clients  )
static
Here is the caller graph for this function:

◆ log_fill_msg()

static void log_fill_msg ( struct blob_buf *  b,
struct log_head l 
)
static

Definition at line 86 of file logd.c.

87 {
88  blobmsg_add_string(b, "msg", l->data);
89  blobmsg_add_u32(b, "id", l->id);
90  blobmsg_add_u32(b, "priority", l->priority);
91  blobmsg_add_u32(b, "source", l->source);
92  blobmsg_add_u64(b, "time", (((__u64) l->ts.tv_sec) * 1000) + (l->ts.tv_nsec / 1000000));
93 }
static struct blob_buf b
Definition: logd.c:33
char data[]
Definition: syslog.h:34
int source
Definition: syslog.h:32
unsigned int id
Definition: syslog.h:30
struct timespec ts
Definition: syslog.h:33
int priority
Definition: syslog.h:31
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 245 of file logd.c.

246 {
247  int ch, log_size = 16;
248  struct passwd *p = NULL;
249 
250  signal(SIGPIPE, SIG_IGN);
251  while ((ch = getopt(argc, argv, "S:")) != -1) {
252  switch (ch) {
253  case 'S':
254  log_size = atoi(optarg);
255  if (log_size < 1)
256  log_size = 16;
257  break;
258  }
259  }
260  log_size *= 1024;
261 
262  uloop_init();
265  ubus_auto_connect(&conn);
266  udebug_ubus_init(&udebug, &conn.ctx, "log", log_udebug_config);
267  p = getpwnam("logd");
268  if (p) {
269  if (setgid(p->pw_gid) < 0) {
270  fprintf(stderr, "setgid() failed: %s\n", strerror(errno));
271  exit(1);
272  }
273 
274  if (setuid(p->pw_uid) < 0) {
275  fprintf(stderr, "setuid() failed: %s\n", strerror(errno));
276  exit(1);
277  }
278  }
279  uloop_run();
280  udebug_ubus_free(&udebug);
281  log_shutdown();
282  uloop_done();
283  ubus_auto_shutdown(&conn);
284 
285  return 0;
286 }
static void ubus_connect_handler(struct ubus_context *ctx)
Definition: logd.c:232
static struct udebug_ubus udebug
Definition: logd.c:35
static struct ubus_auto_conn conn
Definition: logd.c:34
static int log_size
Definition: logread.c:66
void log_init(int _log_size)
Definition: syslog.c:369
void log_udebug_config(struct udebug_ubus *ctx, struct blob_attr *data, bool enabled)
Definition: syslog.c:362
void log_shutdown(void)

◆ read_log()

static int read_log ( struct ubus_context *  ctx,
struct ubus_object *  obj,
struct ubus_request_data *  req,
const char *  method,
struct blob_attr *  msg 
)
static

Definition at line 96 of file logd.c.

99 {
100  struct client *cl;
101  struct blob_attr *tb[__READ_MAX] = {};
102  struct log_head *l;
103  int count = 0;
104  int fds[2];
105  int ret;
106  bool stream = true;
107  bool oneshot = false;
108  void *c, *e;
109 
110  if (!stream)
111  count = 100;
112 
113  if (msg) {
114  blobmsg_parse(read_policy, __READ_MAX, tb, blob_data(msg), blob_len(msg));
115  if (tb[READ_LINES])
116  count = blobmsg_get_u32(tb[READ_LINES]);
117  if (tb[READ_STREAM])
118  stream = blobmsg_get_bool(tb[READ_STREAM]);
119  if (tb[READ_ONESHOT])
120  oneshot = blobmsg_get_bool(tb[READ_ONESHOT]);
121  }
122 
123  l = log_list(count, NULL);
124  if (stream) {
125  if (pipe(fds) == -1) {
126  fprintf(stderr, "logd: failed to create pipe: %m\n");
127  return -1;
128  }
129 
130  ubus_request_set_fd(ctx, req, fds[0]);
131  cl = calloc(1, sizeof(*cl));
132  cl->s.stream.notify_state = client_notify_state;
133  cl->fd = fds[1];
134  ustream_fd_init(&cl->s, cl->fd);
135  list_add(&cl->list, &clients);
136  while ((!tb[READ_LINES] || count) && l) {
137  blob_buf_init(&b, 0);
138  log_fill_msg(&b, l);
139  l = log_list(count, l);
140  ret = ustream_write(&cl->s.stream, (void *) b.head, blob_len(b.head) + sizeof(struct blob_attr), false);
141  if (ret < 0)
142  break;
143  }
144 
145  if (oneshot) {
146  cl->s.stream.notify_write = client_notify_write;
147  client_notify_write(&cl->s.stream, 0);
148  }
149  } else {
150  blob_buf_init(&b, 0);
151  c = blobmsg_open_array(&b, "log");
152  while ((!tb[READ_LINES] || count) && l) {
153  e = blobmsg_open_table(&b, NULL);
154  log_fill_msg(&b, l);
155  blobmsg_close_table(&b, e);
156  l = log_list(count, l);
157  }
158  blobmsg_close_array(&b, c);
159  ubus_send_reply(ctx, req, b.head);
160  }
161  blob_buf_free(&b);
162  return 0;
163 }
static void client_notify_state(struct ustream *s)
Definition: logd.c:72
static void client_notify_write(struct ustream *s, int bytes)
Definition: logd.c:77
static const struct blobmsg_policy read_policy[__READ_MAX]
Definition: logd.c:45
static void log_fill_msg(struct blob_buf *b, struct log_head *l)
Definition: logd.c:86
static struct ubus_context * ctx
Definition: lsbloader.c:43
struct log_head * log_list(int count, struct log_head *h)
Definition: syslog.c:305
Here is the call graph for this function:

◆ ubus_connect_handler()

static void ubus_connect_handler ( struct ubus_context *  ctx)
static

Definition at line 232 of file logd.c.

233 {
234  int ret;
235 
236  ret = ubus_add_object(ctx, &log_object);
237  if (ret) {
238  fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
239  exit(1);
240  }
241  fprintf(stderr, "log: connected to ubus\n");
242 }
static struct ubus_object log_object
Definition: logd.c:200

◆ ubus_notify_log()

void ubus_notify_log ( struct log_head l)

Definition at line 208 of file logd.c.

209 {
210  struct client *c;
211 
212  if (list_empty(&clients) && !log_object.has_subscribers)
213  return;
214 
215  blob_buf_init(&b, 0);
216  blobmsg_add_string(&b, "msg", l->data);
217  blobmsg_add_u32(&b, "id", l->id);
218  blobmsg_add_u32(&b, "priority", l->priority);
219  blobmsg_add_u32(&b, "source", l->source);
220  blobmsg_add_u64(&b, "time", (((__u64) l->ts.tv_sec) * 1000) + (l->ts.tv_nsec / 1000000));
221 
222  if (log_object.has_subscribers)
223  ubus_notify(&conn.ctx, &log_object, "message", b.head, -1);
224 
225  list_for_each_entry(c, &clients, list)
226  ustream_write(&c->s.stream, (void *) b.head, blob_len(b.head) + sizeof(struct blob_attr), false);
227 
228  blob_buf_free(&b);
229 }
Here is the caller graph for this function:

◆ write_log()

static int write_log ( struct ubus_context *  ctx,
struct ubus_object *  obj,
struct ubus_request_data *  req,
const char *  method,
struct blob_attr *  msg 
)
static

Definition at line 166 of file logd.c.

169 {
170  struct blob_attr *tb;
171  char *event;
172 
173  if (msg) {
174  int len;
175 
176  blobmsg_parse(&write_policy, 1, &tb, blob_data(msg), blob_len(msg));
177  if (tb) {
178  event = blobmsg_get_string(tb);
179  len = strlen(event) + 1;
180  if (len > LOG_LINE_SIZE) {
181  len = LOG_LINE_SIZE;
182  event[len - 1] = 0;
183  }
184 
185  log_add(event, len, SOURCE_SYSLOG);
186  }
187  }
188 
189  return 0;
190 }
static const struct blobmsg_policy write_policy
Definition: logd.c:51
void log_add(char *buf, int size, int source)
Definition: syslog.c:147
#define LOG_LINE_SIZE
Definition: syslog.h:17
@ SOURCE_SYSLOG
Definition: syslog.h:23

Variable Documentation

◆ b

struct blob_buf b
static

Definition at line 32 of file logd.c.

◆ conn

struct ubus_auto_conn conn
static

Definition at line 32 of file logd.c.

◆ debug

int debug = 0

Definition at line 32 of file logd.c.

◆ log_methods

const struct ubus_method log_methods[]
static
Initial value:
= {
UBUS_METHOD("read", read_log, read_policy),
{ .name = "write", .handler = write_log, .policy = &write_policy, .n_policy = 1 },
}
static int write_log(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
Definition: logd.c:166
static int read_log(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
Definition: logd.c:96

Definition at line 166 of file logd.c.

◆ log_object

struct ubus_object log_object
static
Initial value:
= {
.name = "log",
.type = &log_object_type,
.methods = log_methods,
.n_methods = ARRAY_SIZE(log_methods),
}
static struct ubus_object_type log_object_type
Definition: logd.c:197
static const struct ubus_method log_methods[]
Definition: logd.c:192

Definition at line 166 of file logd.c.

◆ log_object_type

struct ubus_object_type log_object_type
static
Initial value:
=
UBUS_OBJECT_TYPE("log", log_methods)

Definition at line 166 of file logd.c.

◆ read_policy

const struct blobmsg_policy read_policy[__READ_MAX]
static
Initial value:
= {
[READ_LINES] = { .name = "lines", .type = BLOBMSG_TYPE_INT32 },
[READ_STREAM] = { .name = "stream", .type = BLOBMSG_TYPE_BOOL },
[READ_ONESHOT] = { .name = "oneshot", .type = BLOBMSG_TYPE_BOOL },
}

Definition at line 36 of file logd.c.

◆ udebug

struct udebug_ubus udebug
static

Definition at line 32 of file logd.c.

◆ write_policy

const struct blobmsg_policy write_policy
static
Initial value:
=
{ .name = "event", .type = BLOBMSG_TYPE_STRING }

Definition at line 36 of file logd.c.