/* * --------------------------------------------------- * Job command main entry point * --------------------------------------------------- */ int op_job(int4 argcnt, ...) { va_list var; int4 i; mval *label, *inp; int4 offset; mval *routine, *param_buf; int4 timeout; /* timeout in seconds */ int4 msec_timeout; /* timeout in milliseconds */ boolean_t timed, single_attempt, non_exit_return; unsigned char buff[128], *c; int4 status, exit_stat, term_sig, stop_sig; pid_t zjob_pid = 0; /* zjob_pid should exactly match in type with child_pid(ojstartchild.c) */ int pipe_fds[2], pipe_status; # ifdef _BSD union wait wait_stat; # else int4 wait_stat; # endif job_params_type job_params; char combuf[128]; mstr command; job_parm *jp; DCL_THREADGBL_ACCESS; SETUP_THREADGBL_ACCESS; VAR_START(var, argcnt); assert(argcnt >= 5); label = va_arg(var, mval *); offset = va_arg(var, int4); routine = va_arg(var, mval *); param_buf = va_arg(var, mval *); timeout = va_arg(var, int4); /* in seconds */ argcnt -= 5; /* initialize $zjob = 0, in case JOB fails */ dollar_zjob = 0; MV_FORCE_DEFINED(label); MV_FORCE_DEFINED(routine); MV_FORCE_DEFINED(param_buf); /* create a pipe to channel the PID of the jobbed off process(J) from middle level * process(M) to the current process (P) */ OPEN_PIPE(pipe_fds, pipe_status); if (-1 == pipe_status) { va_end(var); rts_error_csa(CSA_ARG(NULL) VARLSTCNT(7) ERR_JOBFAIL, 0, ERR_TEXT, 2, LEN_AND_LIT("Error creating pipe"), errno); } jobcnt++; command.addr = &combuf[0]; /* Setup job parameters by parsing param_buf and using label, offset, routine, & timeout). */ job_params.routine = routine->str; job_params.label = label->str; job_params.offset = offset; ojparams(param_buf->str.addr, &job_params); /* * Verify that entryref to JOB command is not NULL. */ if (!job_params.routine.len) { va_end(var); rts_error_csa(CSA_ARG(NULL) VARLSTCNT(4) ERR_JOBFAIL, 0, ERR_NULLENTRYREF, 0); } /* Clear the buffers */ flush_pio(); /* Start the timer */ ojtimeout = FALSE; if (timeout < 0) timeout = 0; else if (TREF(tpnotacidtime) < timeout) TPNOTACID_CHECK(JOBTIMESTR); if (NO_M_TIMEOUT == timeout) { timed = FALSE; msec_timeout = NO_M_TIMEOUT; } else { timed = TRUE; msec_timeout = timeout2msec(timeout); if (msec_timeout > 0) start_timer((TID)&tid, msec_timeout, job_timer_handler, 0, NULL); } if (argcnt) { jp = job_params.parms = (job_parm *)malloc(SIZEOF(job_parm) * argcnt); i = argcnt; for(;;) { inp = va_arg(var, mval *); jp->parm = inp; if (0 == --i) break; jp->next = jp + 1; jp = jp->next; } jp->next = 0; } else
pid_t __popen3(const char* cmd, FILE** strm_w, FILE** strm_r, FILE** strm_e) { #define OPEN_PIPE(strm, fds) \ do { \ if (strm) { \ if (pipe(fds) != 0) { \ LM_ERR("failed to create reading pipe (%d: %s)\n", \ errno, strerror(errno)); \ return -1; \ } \ } \ } while (0); /* * cl - pipe end to be closed * op - pipe end to be redirected * re - fds where to redirect 'op' */ #define CLOSE_AND_REDIRECT(strm, fds, cl, op, re) \ do { \ if (strm) { \ close(fds[cl]); \ dup2(fds[op], re); \ close(fds[op]); \ } \ } while (0); #define CLOSE_END_AND_OPEN_STREAM(strm, way, fds, end2close) \ do { \ if (strm) { \ close(fds[end2close]); \ *strm = fdopen(fds[(1^end2close)], way); \ } \ }while (0); pid_t ret; int r_fds[2], w_fds[2], e_fds[2]; if (strm_r == NULL && strm_w == NULL && strm_e == NULL) { LM_WARN("no descriptor redirect required\n"); } OPEN_PIPE(strm_w, w_fds); OPEN_PIPE(strm_r, r_fds); OPEN_PIPE(strm_e, e_fds); ret=fork(); if (ret==0) { /* write pipe */ CLOSE_AND_REDIRECT(strm_w, w_fds, 1, 0 ,0); /* read pipe */ CLOSE_AND_REDIRECT(strm_r, r_fds, 0, 1, 1); /* error pipe */ CLOSE_AND_REDIRECT(strm_e, e_fds, 0, 1, 2); execl("/bin/sh", "/bin/sh", "-c", cmd, NULL); exit(-1); } CLOSE_END_AND_OPEN_STREAM(strm_w, "w", w_fds, 0); CLOSE_END_AND_OPEN_STREAM(strm_r, "r", r_fds, 1); CLOSE_END_AND_OPEN_STREAM(strm_e, "r", e_fds, 1); return ret; #undef OPEN_PIPE #undef CLOSE_AND_REDIRECT #undef CLOSE_AND_OPEN_STREAM }
void InitProc(struct ProcState_s *PState) { int ret, rc; enum ProcTypes_e ProcessorType=NONE; // Set default value and then check #ifdef BF537_DEFINED ProcessorType = BF537; LOG_MSG("BF537 defined\n"); #elif RPI_DEFINED ProcessorType = RPI; LOG_MSG("RaspberryPi defined\n"); #elif BB_DEFINED ProcessorType = BB; LOG_MSG("BeagleBone defined\n"); #elif HOST_DEFINED ProcessorType = HOSTENV; LOG_MSG("HOST defined\n"); #else sprintf(InfoText, "Unknown processor type defined: %d \n", ProcessorType); CHECK(FALSE, InfoText); #endif PState->ProcType = ProcessorType; remove(KBD_PIPE); remove(ONEWIRE_PIPE); remove(MAIN_PIPE); remove(TIMO_PIPE); remove(BYTEPHNDL_PIPE); umask(0); //mknod("tmp/ByteportReports/", S_IFDIR|0666, 0); ret = mknod(KBD_PIPE, S_IFIFO|0666, 0); if (ret < 0) { sprintf(InfoText, "Error: %s, %d, %s \r\n", KBD_PIPE, errno, strerror(errno)); LOG_MSG(InfoText); } ret = mknod(ONEWIRE_PIPE, S_IFIFO|0666, 0); if (ret < 0) { sprintf(InfoText, "Error: %s, %d, %s \r\n", ONEWIRE_PIPE, errno, strerror(errno)); LOG_MSG(InfoText); } ret = mknod(BYTEPHNDL_PIPE, S_IFIFO|0666, 0); if (ret < 0) { sprintf(InfoText, "Error: %s, %d, %s \r\n", BYTEPHNDL_PIPE, errno, strerror(errno)); LOG_MSG(InfoText); } ret = mknod(MAIN_PIPE, S_IFIFO|0666, 0); if (ret < 0) { sprintf(InfoText, "Error: %s, %d, %s \r\n", MAIN_PIPE, errno, strerror(errno)); LOG_MSG(InfoText); } ret = mknod(TIMO_PIPE, S_IFIFO|0666, 0); if (ret < 0) { sprintf(InfoText, "Error: %s, %d, %s \r\n", TIMO_PIPE, errno, strerror(errno)); LOG_MSG(InfoText); } // Initiate all pipes for communication OPEN_PIPE(PState->fd.RD_BPRepPipe, BYTEPHNDL_PIPE, O_RDONLY|O_NONBLOCK); OPEN_PIPE(PState->fd.WR_BPRepPipe, BYTEPHNDL_PIPE, O_WRONLY); OPEN_PIPE(PState->fd.RD_TimoPipe, TIMO_PIPE, O_RDONLY|O_NONBLOCK); OPEN_PIPE(PState->fd.WR_TimoPipe, TIMO_PIPE, O_WRONLY); OPEN_PIPE(PState->fd.RD_OWPipe, ONEWIRE_PIPE, O_RDONLY|O_NONBLOCK); OPEN_PIPE(PState->fd.WR_OWPipe, ONEWIRE_PIPE, O_WRONLY); OPEN_PIPE(PState->fd.RD_MainPipe, MAIN_PIPE, O_RDONLY|O_NONBLOCK); OPEN_PIPE(PState->fd.WR_MainPipe, MAIN_PIPE, O_WRONLY); // sprintf(InfoText, " WR_Main %d RD_Main %d WR Timo % d RD_Timo %d \r\n", // PState->fd.WR_MainPipe, PState->fd.RD_MainPipe, PState->fd.WR_TimoPipe, PState->fd.RD_TimoPipe ); // LOG_MSG(InfoText); // LOG_MSG("Pipes initiated\n\r"); // Define which processes to use, several options! You need to include correct file also! ret= pthread_create( &PState->Thread.Timeout, NULL, (void *) TimeoutHandler, (void *) PState); if (ret != 0) printf("%s %d %s open error %s\n", __FILE__, __LINE__, "Timout thread", strerror(errno)); errno = 0; // sprintf(InfoText, "Initiated process Timeouthandler: %x..\n\r", &PState->Thread.Timeout); // LOG_MSG(InfoText); sleep(2); // Not used as LCD buttons works well 20160212 // ret = pthread_create( &PState->Thread.Button, NULL, (void *) RdButton, (void *) ProcessorType); // if (ret != 0) printf("%s %d %s open error %s\n", __FILE__, __LINE__, "Button thread", strerror(errno)); // errno = 0; // Not used, LCD buttons used instead-perhaps useful when debugging! // ret = pthread_create( &PState->Thread.Kbd, NULL, (void *) RdKeyboard, (void *) ProcessorType); // if (ret != 0) printf("%s %d %s open error %s\n", __FILE__, __LINE__, "Kbd thread", strerror(errno)); // errno = 0; ret = pthread_create( &PState->Thread.OneWire, NULL, (void *) OneWireHandler, (void *) PState); if (ret != 0) printf("%s %d %s open error %s\n", __FILE__, __LINE__, "OneWire thread", strerror(errno)); errno = 0; // sprintf(InfoText, "Initiated process: Onewire: %x..\n\r", &PState->Thread.OneWire); // LOG_MSG(InfoText); sleep(8); ret = pthread_create( &PState->Thread.WDog, NULL, (void *) Watchdog, (void *) PState); if (ret != 0) printf("%s %d %s open error %s\n", __FILE__, __LINE__, "Watchdog thread", strerror(errno)); errno = 0; // sprintf(InfoText, "Initiated process: Watchdog: %x..\n\r", &PState->Thread.WDog); // LOG_MSG(InfoText); sleep(8); //Not used, sockets not used right now. Add if needed //ret = pthread_create( &PState->Thread.SockServ, NULL, (void *) SockServer, (void *) ProcessorType); //if (ret != 0) printf("%s %d %s open error %s\n", __FILE__, __LINE__, "Socket server", strerror(errno)); //errno = 0; ret = pthread_create( &PState->Thread.LCDKbd, NULL, (void *) RdLCDButtons, (void *) PState); if (ret != 0) printf("%s %d %s open error %s\n", __FILE__, __LINE__, "LCD Buttons thread", strerror(errno)); errno = 0; //LOG_MSG("Initiate process: LCD & Kbd..\n\r"); sleep(8); ret = pthread_create( &PState->Thread.ByteportHandler, NULL, (void *) BPHandler, (void *) PState); if (ret != 0) printf("%s %d %s open error %s\n", __FILE__, __LINE__, "Byteport handler thread", strerror(errno)); errno = 0; //LOG_MSG("Initiate process: Byteporthandler..\n\r"); //sleep(8); // Wait until all threads are ready, i.e have opened all resources }