static int mod_insert ( char *mod, int argc, char **argv ) { struct mod_list_t *tail = 0; struct mod_list_t *head = 0; int rc = 0; // get dep list for module mod check_dep ( mod, &head, &tail ); if ( head && tail ) { int i; int l = 0; // append module args l = xstrlen ( head-> m_module ); for ( i = 0; i < argc; i++ ) l += ( xstrlen ( argv [i] ) + 1 ); head-> m_module = xrealloc ( head-> m_module, l + 1 ); for ( i = 0; i < argc; i++ ) { strcat ( head-> m_module, " " ); strcat ( head-> m_module, argv [i] ); } // process tail ---> head rc |= mod_process ( tail, 1 ); } else rc = 1; return rc; }
static int mod_insert ( char *mod, int argc, char **argv ) { struct mod_list_t *tail = 0; struct mod_list_t *head = 0; int rc; // get dep list for module mod check_dep ( mod, &head, &tail ); if ( head && tail ) { if( argc ) { int i; // append module args for ( i = 0; i < argc; i++ ) head->m_options = append_option( head->m_options, argv[i] ); } // process tail ---> head if ((rc = mod_process ( tail, 1 )) != 0) { /* * In case of using udev, multiple instances of modprobe can be * spawned to load the same module (think of two same usb devices, * for example; or cold-plugging at boot time). Thus we shouldn't * fail if the module was loaded, and not by us. */ if (already_loaded (mod) ) rc = 0; } } else rc = 1; return rc; }
struct task *dc_new_task(struct task_group *group, const char *name) { struct task *task = calloc(1, sizeof(struct task)); for (struct task_group *i = task_groups; i ; i = i->next) { for (struct task *j = i->tasks; j ; j = j->next) { if (strcmp(task->name, name) == 0) { log_error("dc", "task %s already registered in group %s", name, i->name); return 0; } } } task->group = group; task->name = strdup(name); if (group->tasks) { assert (group->tasks->prev == 0); group->tasks->prev = task; task->next = group->tasks; } group->tasks = task; if (mod_process (new_task(task)) != 0) goto unroll; if (mod_ubus (new_task(task)) != 0) goto unroll; return task; unroll: dc_delete_task(task); return 0; }
static int mod_insert ( char *mod, int argc, char **argv ) { struct mod_list_t *tail = 0; struct mod_list_t *head = 0; int rc; // get dep list for module mod check_dep ( mod, &head, &tail ); if ( head && tail ) { if( argc ) { int i; // append module args for ( i = 0; i < argc; i++ ) head->m_options = append_option( head->m_options, argv[i] ); } // process tail ---> head rc = mod_process ( tail, 1 ); } else rc = 1; return rc; }
int dc_task_stop(struct task *task) { int r = 0; if (task->running == 0 ) return 0; mod_process(task_stop(task)); task->running = 0; dc_on_task_stopped(task); return 0; }
int main(int argc, char **argv) { _SIZEOF(mod_channel_data_t); _SIZEOF(mod_division_data_t); _SIZEOF(mod_pattern_data_t); if (argc == 2) { mod_setup(); mod_load(argv[1]); mod_process(); mod_save("../../test.mod"); } return 0; }
static void mod_remove ( char *mod ) { static struct mod_list_t rm_a_dummy = { "-a", 0, 0 }; struct mod_list_t *head = 0; struct mod_list_t *tail = 0; if ( mod ) check_dep ( mod, &head, &tail ); else // autoclean head = tail = &rm_a_dummy; if ( head && tail ) mod_process ( head, 0 ); // process head ---> tail }
int dc_task_start(struct task *task) { if (task->running != 0) return 0; task->running = 1; int r = 0; r = mod_process(task_start(task)); if (r != 0) { log_error("dc", "task starting failed for %s", task->name); task->running = 4; dc_task_stop(r); return r; } dc_on_task_started(task); return r; }
static int mod_remove(char *mod) { int rc; static const struct mod_list_t rm_a_dummy = { "-a", NULL, NULL, NULL, NULL }; struct mod_list_t *head = NULL; struct mod_list_t *tail = NULL; if (mod) check_dep(mod, &head, &tail); else // autoclean head = tail = (struct mod_list_t*) &rm_a_dummy; rc = 1; if (head && tail) rc = mod_process(head, 0); // process head ---> tail return rc; }
void dc_delete_task(struct task *task) { mod_ubus (delete_task(task)); mod_process (delete_task(task)); if (task->prev) task->prev->next = task->next; if (task->next) task->next->prev = task->prev; if (task->group->tasks == task) task->group->tasks = task->next; if (task->name) free (task->name); if (task->cmd) free (task->cmd); free (task); }
int main( int argc , char *argv[] ) { if( argc == 1 ) { usage(); exit(0); } argv++; argc--; /* 启动服务 */ if (strcmp(*argv, "start") == 0) { argv++; argc--; start_process(argc, argv); } else if (strcmp(*argv, "add") == 0) { /* 新增服务 */ argv++; argc--; add_process(argc, argv); } else if (strcmp(*argv, "reload") == 0) { /* 重载服务 */ argv++; argc--; mod_process(argc, argv); } else if (strcmp(*argv, "close") == 0) { /* 关闭服务 */ argv++; argc--; stop_process(argc, argv); } else if (strcmp(*argv, "force") == 0) { /* 关闭服务 */ argv++; argc--; force_stop_process(argc, argv); } else if (strcmp(*argv, "view") == 0) { /* 查看服务 */ argv++; argc--; view_process(argc, argv); } else if (strcmp(*argv, "stop") == 0) { /* 停止服务 */ argv++; argc--; close_process(argc, argv); } else { printf("无效的参数:%s\n", *argv); usage(); } return 0; }
int main(int argc, char **argv) { if (getuid() != 0) panic("sorry, must be superuser"); int iaminit = 0; if (strcmp(argv[0], "/sbin/init") == 0) { iaminit = 1; //FIXME: ugh system("mount -n tmpfs -t tmpfs /task"); } log_info("dc", "Moorlight 1 booting up"); int r = 0; if (iaminit) r = mod_sysv (init()); r = mod_process (init()); r = mod_ubus (init()); struct task_group *default_group = dc_new_group("default"); struct task *test = dc_new_task(default_group, "test"); test->cmd = strdup("/home/aep/proj/moorlight/test/testdaemon.sh"); for (struct task_group *i = task_groups; i ; i = i->next) for (struct task *j = i->tasks; j ; j = j->next) dc_task_start(j); while (running) { fd_set rfds; FD_ZERO(&rfds); int maxfd = 0; mod_ubus (select(&rfds, &maxfd)); mod_process (select(&rfds, &maxfd)); int ret = 0; do { int ret = select(maxfd + 2, &rfds, NULL, NULL, NULL); } while (ret == EINTR); if (ret < 0) { assume(ret); } log_debug("dc", "activated %i ", ret); mod_ubus (activate(&rfds)); mod_process (activate(&rfds)); } for (struct task_group *i = task_groups; i ; i = i->next) { for (struct task *j = i->tasks; j ; j = j->next) { dc_task_stop(j); dc_delete_task(j); } int r = 0; dc_delete_group(i); } mod_ubus (teardown()); mod_process (teardown()); if (iaminit) mod_sysv (teardown()); return 0; }