Ubox
OpenWrt core utilities
lsbloader.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include <glob.h>
#include <libubox/list.h>
#include <libubox/blobmsg_json.h>
#include "libubus.h"

Go to the source code of this file.

Data Structures

struct  initd
 

Macros

#define __USE_GNU
 

Functions

static LIST_HEAD (initds)
 
static void initd_free (struct initd *i)
 
static int initd_parse (const char *file)
 
static void initd_init (void)
 
static int init_services (void)
 
int main (int argc, char **argv)
 

Variables

static regex_t pat_provides
 
static regex_t pat_require
 
static regex_t pat_start
 
static regex_t pat_stop
 
static regex_t pat_desc
 
static regex_t pat_exec
 
static regex_t pat_tpl
 
static struct ubus_context * ctx
 
static struct blob_buf b
 
static uint32_t service
 

Macro Definition Documentation

◆ __USE_GNU

#define __USE_GNU

Definition at line 20 of file lsbloader.c.

Function Documentation

◆ init_services()

static int init_services ( void  )
static

Definition at line 137 of file lsbloader.c.

138 {
139  struct initd *i;
140  void *instances, *instance, *command;
141 
142  list_for_each_entry(i, &initds, list) {
143  char *t;
144 
145  blob_buf_init(&b, 0);
146  blobmsg_add_string(&b, "name", i->name);
147  instances = blobmsg_open_table(&b, "instances");
148  instance = blobmsg_open_table(&b, "instance");
149  command = blobmsg_open_array(&b, "command");
150  t = strtok(i->exec, " ");
151  while (t) {
152  blobmsg_add_string(&b, NULL, t);
153  t = strtok(NULL, " ");
154  }
155  blobmsg_close_array(&b, command);
156  blobmsg_close_table(&b, instance);
157  blobmsg_close_table(&b, instances);
158  ubus_invoke(ctx, service, "add", b.head, NULL, 0, 1000);
159  }
160 
161  return 0;
162 }
static struct ubus_context * ctx
Definition: lsbloader.c:43
static struct blob_buf b
Definition: lsbloader.c:44
static uint32_t service
Definition: lsbloader.c:45
char * exec
Definition: lsbloader.c:33
struct list_head list
Definition: lsbloader.c:30
char * name
Definition: lsbloader.c:32

◆ initd_free()

static void initd_free ( struct initd i)
static

Definition at line 47 of file lsbloader.c.

48 {
49  if (i->name)
50  free(i->name);
51  if (i->exec)
52  free(i->exec);
53  if (i->desc)
54  free(i->desc);
55  if (i->tpl)
56  free(i->tpl);
57 }
char * tpl
Definition: lsbloader.c:35
char * desc
Definition: lsbloader.c:34
Here is the caller graph for this function:

◆ initd_init()

static void initd_init ( void  )
static

Definition at line 108 of file lsbloader.c.

109 {
110  int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
111  glob_t gl;
112 
113  regcomp(&pat_provides, "# Provides:[ \t]*([a-zA-Z0-9]+)", REG_EXTENDED);
114  regcomp(&pat_require, "# Required-Start:[ \t]*([a-zA-Z0-9 ]+)", REG_EXTENDED);
115  regcomp(&pat_start, "# Default-Start:[ \t]*([0-9])", REG_EXTENDED);
116  regcomp(&pat_stop, "# Default-Stop:[ \t]*([0-9])", REG_EXTENDED);
117  regcomp(&pat_desc, "# Description:[ \t]*([a-zA-Z0-9 ]+)", REG_EXTENDED);
118  regcomp(&pat_exec, "# X-Exec:[ \t]*([a-zA-Z0-9/ ]+)", REG_EXTENDED);
119  regcomp(&pat_tpl, "# X-Template:[ \t]*([a-zA-Z0-9/.]+)", REG_EXTENDED);
120 
121  if (glob("/etc/rc.d/P*", gl_flags, NULL, &gl) >= 0) {
122  int j;
123  for (j = 0; j < gl.gl_pathc; j++)
124  initd_parse(gl.gl_pathv[j]);
125  }
126  globfree(&gl);
127 
128  regfree(&pat_provides);
129  regfree(&pat_require);
130  regfree(&pat_start);
131  regfree(&pat_stop);
132  regfree(&pat_desc);
133  regfree(&pat_exec);
134  regfree(&pat_tpl);
135 }
static regex_t pat_require
Definition: lsbloader.c:42
static regex_t pat_start
Definition: lsbloader.c:42
static regex_t pat_desc
Definition: lsbloader.c:42
static int initd_parse(const char *file)
Definition: lsbloader.c:59
static regex_t pat_stop
Definition: lsbloader.c:42
static regex_t pat_provides
Definition: lsbloader.c:42
static regex_t pat_tpl
Definition: lsbloader.c:42
static regex_t pat_exec
Definition: lsbloader.c:42
Here is the call graph for this function:

◆ initd_parse()

static int initd_parse ( const char *  file)
static

Definition at line 59 of file lsbloader.c.

60 {
61  FILE *fp;
62  struct initd *i;
63  regmatch_t matches[2];
64  char buffer[1024];
65  ssize_t len;
66 
67  fp = fopen(file, "r");
68  if (!fp) {
69  fprintf(stderr, "failed to open %s\n", file);
70  return -1;
71  }
72  len = fread(buffer, 1, sizeof(buffer) - 1, fp);
73  fclose(fp);
74 
75  if (len < 1) {
76  fprintf(stderr, "failed to read from %s\n", file);
77  return -1;
78  }
79  buffer[len] = '\0';
80 
81  i = calloc(1, sizeof(struct initd));
82  if (!i) {
83  fprintf(stderr, "failed to alloc initd struct\n");
84  return -1;
85  }
86 
87  if (!regexec(&pat_provides, buffer, 2, matches, 0))
88  i->name = strndup(buffer + matches[1].rm_so, (size_t)matches[1].rm_eo - matches[1].rm_so);
89  if (!regexec(&pat_exec, buffer, 2, matches, 0))
90  i->exec = strndup(buffer + matches[1].rm_so, matches[1].rm_eo - matches[1].rm_so);
91  if (!regexec(&pat_desc, buffer, 2, matches, 0))
92  i->desc = strndup(buffer + matches[1].rm_so, matches[1].rm_eo - matches[1].rm_so);
93  if (!regexec(&pat_tpl, buffer, 2, matches, 0))
94  i->tpl = strndup(buffer + matches[1].rm_so, matches[1].rm_eo - matches[1].rm_so);
95  if (!regexec(&pat_start, buffer, 2, matches, 0))
96  i->start += atoi(buffer + matches[1].rm_so);
97  if (!regexec(&pat_stop, buffer, 2, matches, 0))
98  i->stop += atoi(buffer + matches[1].rm_so);
99 
100  if (i->name && i->exec)
101  list_add(&i->list, &initds);
102  else
103  initd_free(i);
104 
105  return 0;
106 }
static void initd_free(struct initd *i)
Definition: lsbloader.c:47
int stop
Definition: lsbloader.c:38
int start
Definition: lsbloader.c:37
Here is the call graph for this function:
Here is the caller graph for this function:

◆ LIST_HEAD()

static LIST_HEAD ( initds  )
static

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 164 of file lsbloader.c.

165 {
166  int ret;
167 
168  initd_init();
169 
170  if (list_empty(&initds))
171  return 0;
172 
173  ctx = ubus_connect(NULL);
174  if (!ctx) {
175  fprintf(stderr, "Failed to connect to ubus\n");
176  return -1;
177  }
178 
179  ret = ubus_lookup_id(ctx, "service", &service);
180  if (ret) {
181  fprintf(stderr, "Failed to find service object: %s\n", ubus_strerror(ret));
182  return -1;
183  }
184 
185  return init_services();
186 }
static void initd_init(void)
Definition: lsbloader.c:108
static int init_services(void)
Definition: lsbloader.c:137

Variable Documentation

◆ b

struct blob_buf b
static

Definition at line 43 of file lsbloader.c.

◆ ctx

struct ubus_context* ctx
static

Definition at line 43 of file lsbloader.c.

◆ pat_desc

regex_t pat_desc
static

Definition at line 42 of file lsbloader.c.

◆ pat_exec

regex_t pat_exec
static

Definition at line 42 of file lsbloader.c.

◆ pat_provides

regex_t pat_provides
static

Definition at line 42 of file lsbloader.c.

◆ pat_require

regex_t pat_require
static

Definition at line 42 of file lsbloader.c.

◆ pat_start

regex_t pat_start
static

Definition at line 42 of file lsbloader.c.

◆ pat_stop

regex_t pat_stop
static

Definition at line 42 of file lsbloader.c.

◆ pat_tpl

regex_t pat_tpl
static

Definition at line 42 of file lsbloader.c.

◆ service

uint32_t service
static

Definition at line 45 of file lsbloader.c.