void *programThread(void *param) { struct programs_t *p = (struct programs_t *)param; int pid = 0; int result = 0; if((pid = (int)findproc(p->program, p->arguments)) > 0) { result = system(p->stop); } else { result = system(p->start); } /* Check of the user wanted to stop pilight */ if(WIFSIGNALED(result)) { int ppid = 0; /* Find the pilight daemon pid */ if((ppid = (int)findproc(progname, NULL)) > 0) { /* Send a sigint to ourself */ kill(ppid, SIGINT); } } p->wait = 0; p->pth = 0; return NULL; }
/*===========================================================================* * do_trace * *===========================================================================*/ PUBLIC int do_trace() { register struct mproc *child; /* the T_OK call is made by the child fork of the debugger before it execs * the process to be traced */ if (request == T_OK) {/* enable tracing by parent for this process */ mp->mp_flags |= TRACED; mm_out.m2_l2 = 0; return(OK); } if ((child = findproc(pid)) == NIL_MPROC || !(child->mp_flags & STOPPED)) { return(ESRCH); } /* all the other calls are made by the parent fork of the debugger to * control execution of the child */ switch (request) { case T_EXIT: /* exit */ mm_exit(child, (int)data); mm_out.m2_l2 = 0; return(OK); case T_RESUME: case T_STEP: /* resume execution */ if (data < 0 || data > _NSIG) return(EIO); if (data > 0) { /* issue signal */ child->mp_flags &= ~TRACED; /* so signal is not diverted */ sig_proc(child, (int) data); child->mp_flags |= TRACED; } child->mp_flags &= ~STOPPED; break; } if (sys_trace(request, (int) (child - mproc), taddr, &data) != OK) return(-errno); mm_out.m2_l2 = data; return(OK); }
int main(int argc, char **argv) { gc_attach(main_gc); /* Catch all exit signals for gc */ gc_catch(); log_shell_enable(); log_file_disable(); log_level_set(LOG_NOTICE); struct options_t *options = NULL; char *args = NULL; char *hwfile = NULL; pid_t pid = 0; if(!(settingsfile = malloc(strlen(SETTINGS_FILE)+1))) { logprintf(LOG_ERR, "out of memory"); exit(EXIT_FAILURE); } strcpy(settingsfile, SETTINGS_FILE); if(!(progname = malloc(12))) { logprintf(LOG_ERR, "out of memory"); exit(EXIT_FAILURE); } strcpy(progname, "pilight-raw"); options_add(&options, 'H', "help", OPTION_NO_VALUE, 0, JSON_NULL, NULL, NULL); options_add(&options, 'V', "version", OPTION_NO_VALUE, 0, JSON_NULL, NULL, NULL); options_add(&options, 'S', "settings", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, NULL); while (1) { int c; c = options_parse(&options, argc, argv, 1, &args); if(c == -1) break; if(c == -2) c = 'H'; switch (c) { case 'H': printf("Usage: %s [options]\n", progname); printf("\t -H --help\t\tdisplay usage summary\n"); printf("\t -V --version\t\tdisplay version\n"); printf("\t -S --settings\t\tsettings file\n"); return (EXIT_SUCCESS); break; case 'V': printf("%s %s\n", progname, VERSION); return (EXIT_SUCCESS); break; case 'S': if(access(args, F_OK) != -1) { settingsfile = realloc(settingsfile, strlen(args)+1); if(!settingsfile) { logprintf(LOG_ERR, "out of memory"); exit(EXIT_FAILURE); } strcpy(settingsfile, args); settings_set_file(args); } else { fprintf(stderr, "%s: the settings file %s does not exists\n", progname, args); return EXIT_FAILURE; } break; default: printf("Usage: %s [options]\n", progname); return (EXIT_FAILURE); break; } } options_delete(options); char pilight_daemon[] = "pilight-daemon"; char pilight_learn[] = "pilight-learn"; char pilight_debug[] = "pilight-debug"; if((pid = findproc(pilight_daemon, NULL)) > 0) { logprintf(LOG_ERR, "pilight-daemon instance found (%d)", (int)pid); return (EXIT_FAILURE); } if((pid = findproc(pilight_learn, NULL)) > 0) { logprintf(LOG_ERR, "pilight-learn instance found (%d)", (int)pid); return (EXIT_FAILURE); } if((pid = findproc(pilight_debug, NULL)) > 0) { logprintf(LOG_ERR, "pilight-debug instance found (%d)", (int)pid); return (EXIT_FAILURE); } if(access(settingsfile, F_OK) != -1) { if(settings_read() != 0) { return EXIT_FAILURE; } } hardware_init(); if(settings_find_string("hardware-file", &hwfile) == 0) { hardware_set_file(hwfile); if(hardware_read() == EXIT_FAILURE) { goto clear; } } /* Start threads library that keeps track of all threads used */ threads_create(&pth, NULL, &threads_start, (void *)NULL); struct conf_hardware_t *tmp_confhw = conf_hardware; while(tmp_confhw) { if(tmp_confhw->hardware->init() == EXIT_FAILURE) { logprintf(LOG_ERR, "could not initialize %s hardware mode", tmp_confhw->hardware->id); goto clear; } threads_register(tmp_confhw->hardware->id, &receive_code, (void *)tmp_confhw->hardware, 0); tmp_confhw = tmp_confhw->next; } while(main_loop) { sleep(1); } clear: main_gc(); return (EXIT_FAILURE); }
PUBLIC void osdep_eth_init() { int i, r, tasknr, rport; struct eth_conf *ecp; eth_port_t *eth_port, *rep; message mess; /* First initialize normal ethernet interfaces */ for (i= 0, ecp= eth_conf, eth_port= eth_port_table; i<eth_conf_nr; i++, ecp++, eth_port++) { if (eth_is_vlan(ecp)) continue; #ifdef __minix_vmd r= sys_findproc(ecp->ec_task, &tasknr, 0); #else /* Minix 3 */ r = findproc(ecp->ec_task, &tasknr); #endif if (r != OK) { printf("eth%d: unable to find task %s: %d\n", i, ecp->ec_task, r); continue; } eth_port->etp_osdep.etp_port= ecp->ec_port; eth_port->etp_osdep.etp_task= tasknr; ev_init(ð_port->etp_osdep.etp_recvev); mess.m_type= DL_INIT; mess.DL_PORT= eth_port->etp_osdep.etp_port; mess.DL_PROC= this_proc; mess.DL_MODE= DL_NOMODE; r= send(eth_port->etp_osdep.etp_task, &mess); if (r<0) { printf( "osdep_eth_init: unable to send to ethernet task, error= %d\n", r); continue; } if (receive(eth_port->etp_osdep.etp_task, &mess)<0) ip_panic(("unable to receive")); if (mess.m3_i1 == ENXIO) { printf( "osdep_eth_init: no ethernet device at task=%d,port=%d\n", eth_port->etp_osdep.etp_task, eth_port->etp_osdep.etp_port); continue; } if (mess.m3_i1 < 0) ip_panic(("osdep_eth_init: DL_INIT returned error %d\n", mess.m3_i1)); if (mess.m3_i1 != eth_port->etp_osdep.etp_port) { ip_panic(( "osdep_eth_init: got reply for wrong port (got %d, expected %d)\n", mess.m3_i1, eth_port->etp_osdep.etp_port)); } eth_port->etp_ethaddr= *(ether_addr_t *)mess.m3_ca1; sr_add_minor(if2minor(ecp->ec_ifno, ETH_DEV_OFF), i, eth_open, eth_close, eth_read, eth_write, eth_ioctl, eth_cancel, eth_select); eth_port->etp_flags |= EPF_ENABLED; eth_port->etp_vlan= 0; eth_port->etp_vlan_port= NULL; eth_port->etp_wr_pack= 0; eth_port->etp_rd_pack= 0; setup_read (eth_port); } /* And now come the VLANs */ for (i= 0, ecp= eth_conf, eth_port= eth_port_table; i<eth_conf_nr; i++, ecp++, eth_port++) { if (!eth_is_vlan(ecp)) continue; eth_port->etp_osdep.etp_port= ecp->ec_port; eth_port->etp_osdep.etp_task= ANY; ev_init(ð_port->etp_osdep.etp_recvev); rport= eth_port->etp_osdep.etp_port; assert(rport >= 0 && rport < eth_conf_nr); rep= ð_port_table[rport]; if (!(rep->etp_flags & EPF_ENABLED)) { printf( "eth%d: underlying ethernet device %d not enabled", i, rport); continue; } if (rep->etp_vlan != 0) { printf( "eth%d: underlying ethernet device %d is a VLAN", i, rport); continue; } eth_port->etp_ethaddr= rep->etp_ethaddr; sr_add_minor(if2minor(ecp->ec_ifno, ETH_DEV_OFF), i, eth_open, eth_close, eth_read, eth_write, eth_ioctl, eth_cancel, eth_select); eth_port->etp_flags |= EPF_ENABLED; eth_port->etp_vlan= ecp->ec_vlan; eth_port->etp_vlan_port= rep; assert(eth_port->etp_vlan != 0); eth_port->etp_wr_pack= 0; eth_port->etp_rd_pack= 0; eth_reg_vlan(rep, eth_port); } }
void *programParse(void *param) { struct protocol_threads_t *pnode = (struct protocol_threads_t *)param; struct JsonNode *json = (struct JsonNode *)pnode->param; struct JsonNode *jid = NULL; struct JsonNode *jchild = NULL; struct JsonNode *jchild1 = NULL; char *prog = NULL, *args = NULL, *stopcmd = NULL, *startcmd = NULL; int interval = 1, nrloops = 0, currentstate = 0, laststate = -1; int pid = 0; double itmp = 0; program_threads++; json_find_string(json, "program", &prog); json_find_string(json, "arguments", &args); json_find_string(json, "stop-command", &stopcmd); json_find_string(json, "start-command", &startcmd); struct programs_t *lnode = malloc(sizeof(struct programs_t)); lnode->wait = 0; lnode->pth = 0; if(args && strlen(args) > 0) { if(!(lnode->arguments = malloc(strlen(args)+1))) { logprintf(LOG_ERR, "out of memory"); exit(EXIT_FAILURE); } strcpy(lnode->arguments, args); } else { lnode->arguments = NULL; } if(prog) { if(!(lnode->program = malloc(strlen(prog)+1))) { logprintf(LOG_ERR, "out of memory"); exit(EXIT_FAILURE); } strcpy(lnode->program, prog); } else { lnode->program = NULL; } if(stopcmd) { if(!(lnode->stop = malloc(strlen(stopcmd)+1))) { logprintf(LOG_ERR, "out of memory"); exit(EXIT_FAILURE); } strcpy(lnode->stop, stopcmd); } else { lnode->stop = NULL; } if(startcmd) { if(!(lnode->start = malloc(strlen(startcmd)+1))) { logprintf(LOG_ERR, "out of memory"); exit(EXIT_FAILURE); } strcpy(lnode->start, startcmd); } else { lnode->start = NULL; } if((jid = json_find_member(json, "id"))) { jchild = json_first_child(jid); while(jchild) { jchild1 = json_first_child(jchild); while(jchild1) { if(strcmp(jchild1->key, "name") == 0) { if(!(lnode->name = malloc(strlen(jchild1->string_)+1))) { logprintf(LOG_ERR, "out of memory"); exit(EXIT_FAILURE); } strcpy(lnode->name, jchild1->string_); } jchild1 = jchild1->next; } jchild = jchild->next; } } struct programs_t *tmp = programs; if(tmp) { while(tmp->next != NULL) { tmp = tmp->next; } tmp->next = lnode; } else { lnode->next = tmp; programs = lnode; } if(json_find_number(json, "poll-interval", &itmp) == 0) interval = (int)round(itmp); while(program_loop) { if(protocol_thread_wait(pnode, interval, &nrloops) == ETIMEDOUT) { if(lnode->wait == 0) { program->message = json_mkobject(); JsonNode *code = json_mkobject(); json_append_member(code, "name", json_mkstring(lnode->name)); if((pid = (int)findproc(lnode->program, lnode->arguments)) > 0) { currentstate = 1; json_append_member(code, "state", json_mkstring("running")); json_append_member(code, "pid", json_mknumber((int)pid)); } else { currentstate = 0; json_append_member(code, "state", json_mkstring("stopped")); json_append_member(code, "pid", json_mknumber(0)); } json_append_member(program->message, "message", code); json_append_member(program->message, "origin", json_mkstring("receiver")); json_append_member(program->message, "protocol", json_mkstring(program->id)); if(currentstate != laststate) { laststate = currentstate; pilight.broadcast(program->id, program->message); } json_delete(program->message); program->message = NULL; } } } program_threads--; return (void *)NULL; }
int programCreateCode(JsonNode *code) { char *name = NULL; double itmp = -1; int state = -1; int pid = 0; if(json_find_string(code, "name", &name) == 0) { if(strstr(progname, "daemon") != NULL) { struct programs_t *tmp = programs; while(tmp) { if(strcmp(tmp->name, name) == 0) { if(tmp->wait == 0 && tmp->pth == 0) { if(tmp->name != NULL && tmp->stop != NULL && tmp->start != NULL) { if(json_find_number(code, "running", &itmp) == 0) state = 1; else if(json_find_number(code, "stopped", &itmp) == 0) state = 0; if((pid = (int)findproc(tmp->program, tmp->arguments)) > 0 && state == 1) { logprintf(LOG_ERR, "program \"%s\" already running", tmp->name); } else if(pid == -1 && state == 0) { logprintf(LOG_ERR, "program \"%s\" already stopped", tmp->name); break; } else { if(state > -1) { program->message = json_mkobject(); JsonNode *code1 = json_mkobject(); json_append_member(code1, "name", json_mkstring(name)); if(state == 1) { json_append_member(code1, "state", json_mkstring("running")); } else { json_append_member(code1, "state", json_mkstring("stopped")); } json_append_member(program->message, "message", code1); json_append_member(program->message, "origin", json_mkstring("receiver")); json_append_member(program->message, "protocol", json_mkstring(program->id)); pilight.broadcast(program->id, program->message); json_delete(program->message); program->message = NULL; } tmp->wait = 1; threads_create(&tmp->pth, NULL, programThread, (void *)tmp); pthread_detach(tmp->pth); program->message = json_mkobject(); json_append_member(program->message, "name", json_mkstring(name)); json_append_member(program->message, "state", json_mkstring("pending")); } } else { logprintf(LOG_ERR, "program \"%s\" cannot be controlled", tmp->name); } } else { logprintf(LOG_ERR, "please wait for program \"%s\" to finish it's state change", tmp->name); } break; } tmp = tmp->next; } } else { program->message = json_mkobject(); if(json_find_number(code, "running", &itmp) == 0) json_append_member(program->message, "state", json_mkstring("running")); else if(json_find_number(code, "stopped", &itmp) == 0) json_append_member(program->message, "state", json_mkstring("stopped")); json_append_member(program->message, "name", json_mkstring(name)); json_append_member(program->message, "state", json_mkstring("pending")); } } else { return EXIT_FAILURE; } return EXIT_SUCCESS; }
docommand() { register char *p; register int i; register ADDR addr, bkaddr; struct proct *procp; char s[4]; cntval = 1; adrflg = 0; errflg = 0; if (scallf) { doscall(); setcur(1); lastcom = NOCOM; return; } if (reflag) { /* search for regular expression */ dore(); lastcom = PRCOM; return; } if (cmd == '\0') { if (integ != 0 && var[0] != '\0') { error("Invalid command (1)"); return; } if (integ != 0) { /* print line number */ ffind(integ); fprint(); lastcom = PRCOM; return; } if (var[0] != 0) { printf("Unexpected null command\n"); return; } } switch (cmd) { case 'Y': debug = !debug; break; case 'V': version(); break; case 'M': if (args[0]) { setmap(args); } else { printmap("? map", &txtmap); printmap("/ map", &datmap); } break; case 'x': printregs(); break; case 'X': printpc(); break; case 'a': if (integ) { cpstr(args, "l\n"); } else if (proc[0]) { cpall(args, "T\n"); } else { error("Bad arguments"); break; } goto setbrk; break; case 'l': setcur(1); lastcom = NOCOM; break; case 'T': prfrx(1); lastcom = NOCOM; break; case 't': prframe(); lastcom = NOCOM; break; case 'e': p = args; if (*p == '\0') { #ifndef FLEXNAMES printf("%.16s() in \"%s\"\n", curproc()->pname, curfile); #else printf("%s() in \"%s\"\n", curproc()->pname, curfile); #endif break; } while (*p != '\0') if (*p++ == '.') goto l1; /* argument is procedure name */ procp = findproc(args); if ((procp->pname[0] != '\0') && (procp->sfptr != badfile)) { finit(adrtofilep(procp->paddr)->sfilename); ffind(procp->lineno); } else printf("Can't find %s\n", args); #ifndef FLEXNAMES printf("%.16s() in \"%s\"\n", curproc()->pname, curfile); #else printf("%s() in \"%s\"\n", curproc()->pname, curfile); #endif lastcom = PRCOM; break; l1: /* argument is filename */ finit(args); printf("\"%s\"\n", curfile); lastcom = PRCOM; break; case 'p': if (integ) ffind(integ); fprint(); lastcom = PRCOM; break; case 'q': exit(0); case 'w': if (integ) ffind(integ); i = fline; fback(WINDOW/2); fprintn(WINDOW); ffind(i); lastcom = PRCOM; break; case 'Q': prdebug(); break; case 'z': if (integ) ffind(integ); fprintn(WINDOW); lastcom = PRCOM; break; case '-': fback(integ ? integ : 1); fpargs(); lastcom = PRCOM; break; case '+': fforward(integ ? integ : 1); fpargs(); lastcom = PRCOM; break; case '\n': switch (lastcom) { case PRCOM: fforward(1); fprint(); break; case DSCOM: oaddr += oincr ? oincr : typetosize(otype, WORDSIZE); printf("0x%x/ ", oaddr); dispf((ADDR) oaddr, odesc, oclass == N_RSYM ? oclass : N_GSYM, otype, 0, 0, DSP); break; case DSICOM: dot += oincr; prisploc(); dispi(dot, odesc, N_GSYM, 0, 0); break; } break; case '\004': if (!isatty(0)) exit(0); switch (lastcom) { case PRCOM: fforward(1); printf("\b"); fprintn(WINDOW); lastcom = PRCOM; break; case DSICOM: printf("\b"); for (i=0; i<WINDOW; i++) { dot += oincr; prisploc(); if (dispi(dot, odesc, N_GSYM, 0, 0) == -1) break; } break; case DSCOM: printf("\b"); for (i=0; i<WINDOW; i++) { oaddr += oincr ? oincr : typetosize(otype, WORDSIZE); printf("0x%x/ ", oaddr); if (dispf((ADDR) oaddr, odesc, oclass == N_RSYM ? oclass : N_GSYM, otype, 0, 0, DSP) == -1) break; } break; default: printf("\n"); } break; case 'r': if (args[0] == '\0') getargs(); case 'R': signo = 0; cpstr(oldargs, args); if (debug) error("calling dopcs"); if (integ) cntval = integ; if (!executing) { executing = TRUE; if (integ) cntval = integ; dopcs('r'); executing = FALSE; } if (debug) error("exiting dopcs"); bkaddr = -1; goto f1; case 'c': signo = 0; case 'C': if (proc[0] != '\0' || integ != 0) { setdot(); if (dot == -1) { error("Cannot set temporary breakpoint"); break; } dopcs('b'); bkaddr = dot; } else bkaddr = -1; integ = atoi(args); f1: if (debug) error("calling dopcs"); if (integ) cntval = integ; dopcs('c'); if (debug) error("exiting dopcs"); if (bkaddr != -1) { ADDR dotsave; dotsave = dot; dot = bkaddr; dopcs('d'); dot = dotsave; } if (!signo) printf("Breakpoint"); printf(" at\n"); setcur(1); lastcom = NOCOM; break; case 'S': case 's': signo = 0; integ = atoi(args); singstep(integ ? integ : 1, cmd); if (signo) printf("\n"); setcur(1); lastcom = NOCOM; break; case 'g': if (pid == 0 || signo) { error("Not stopped at breakpoint"); break; } setdot(); if (dot == -1) { error("Bad address"); break; } adrflg = 1; integ = atoi(args); if (integ) cntval = integ; dopcs('c'); if (!signo) printf("Breakpoint"); printf(" at\n"); setcur(1); lastcom = NOCOM; break; case 'k': if (scallx) { userpc = dot = *(ADDR *)(((ADDR)&u)+PC) = pcs; *(ADDR *)(((ADDR)&u)+FP) = fps; *(ADDR *)(((ADDR)&u)+AP) = aps; if (bkpts) bkpts->flag = flagss; scallx = 0; error("Procedure killed"); longjmp(env, 0); } else { dopcs('k'); printf("\n"); lastcom = NOCOM; break; } case 'B': prbkpt(); break; case 'b': setbrk: if (proc[0] == '\0' && integ == 0) { integ = fline; } setdot(); if (dot == -1 || dot == 0) { error("Cannot set breakpoint"); break; } dopcs('b'); s[0] = ' '; s[1] = cmd; s[2] = '\n'; s[3] = 0; s[1] = cmd; printbkpt(s, adrtoprocp(dot), dot); break; case 'd': if (proc[0] == '\0' && integ == 0) { idbkpt(); break; } setdot(); if (dot == -1) { error("Non existent breakpoint"); break; } dopcs('d'); break; case 'D': dabkpt(); error("All breakpoints deleted"); break; case 'm': addr = varaddr(proc[0] ? proc : curproc()->pname, var); printf("stopped with value %d\n", monex(addr, 'd')); setcur(1); lastcom = NOCOM; break; case '?': if (!(var[0] == '.' && var[1] == '\0')) setdot(); if (errflg) { error(errflg); break; } prisploc(); dispi(dot, args[0] ? args : "i", N_GSYM, 0, 0); lastcom = DSICOM; break; case '/': if (var[0] == '.' && var[1] == '\0') { if (integ == 0) integ = oaddr; dispf((ADDR) integ, args[0] ? args : odesc, oclass == N_RSYM ? oclass : N_GSYM, otype, 0, 0, DSP); oaddr = integ; } else if (integ && (var[0] == '\0')) { dispf((ADDR) integ, args, N_GSYM, 0, 0, 0, DSP); oaddr = integ; cpstr(odesc, args); oclass = N_GSYM; otype = 0; } else dispvar(proc, var, args); lastcom = DSCOM; break; case '=': if (var[0] == '\0') { if (proc[0]) { addr = getaddr(proc, integ); if (addr == -1) { error("Unknown address"); break; } } else addr = integ; dispf(addr, args[0] ? args : "x", 0, -1, 0, 0, DSP); } else findvar(proc, var, args[0] ? args : "x", 2); break; case '!': if (var[0] == '\0') addr = getaddr(proc, integ); else addr = varaddr(proc, var); if (addr == -1) error("Unknown variable"); else { if (number(args[0]) || eqany(args[0], ".-")) { char *p; double atof(); union { struct{ int w1, w2; } ww; double d; } dbl; p = (args[0] == '-') ? args+1 : args; for (; *p != '.' && *p != 'e'; p++) { if (!number(*p)) goto l2; } dbl.d = atof(args); putval(addr, 'd', dbl.ww.w1); if (typetodesc(sl_type,0)[0] == 'g') putval(addr+WORDSIZE, 'd', dbl.ww.w2); break; } l2: if (percentflag) *(ADDR *)(((ADDR)&u)+addr) = argvalue(args); else if (sl_class == N_RSYM && addr < 16) putreg(addr,typetodesc(sl_type,subflag)[0], argvalue(args)); else putval(addr,typetodesc(sl_type,subflag)[0], argvalue(args)); } lastcom = NOCOM; break; case '"': printf(args); break; } }