📄 halrmt.c
字号:
/* desired component not found */ rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: component '%s' is not loaded\n", linenumber, mod_name); return -1; } /* we now have a list of components, unload them */ n = 0; retval1 = 0; while ( comps[n][0] != '\0' ) { retval = unloadrt_comp(comps[n++]); /* check for fatal error */ if ( retval < -1 ) { return retval; } /* check for other error */ if ( retval != 0 ) { retval1 = retval; } } if (retval1 != HAL_SUCCESS) { rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: unloadrt failed\n", linenumber); } return retval1;}static int unloadrt_comp(char *mod_name){ int retval, status; char *argv[3]; pid_t pid; /* now we need to fork, and then exec rmmod.... */ /* disconnect from the HAL shmem area before forking */ hal_exit(comp_id); comp_id = 0; /* now the fork() */ pid = fork(); if ( pid < 0 ) { rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: unloadrt fork() failed\n", linenumber); /* reconnect to the HAL shmem area */ comp_id = hal_init(comp_name); if (comp_id < 0) { fprintf(stderr, "halrmt: hal_init() failed after fork: %d\n", comp_id); exit(-1); } hal_ready(comp_id); return -1; } if ( pid == 0 ) { /* this is the child process - prepare to exec() rmmod */ argv[0] = EMC2_BIN_DIR "/emc_module_helper"; argv[1] = "remove"; argv[2] = mod_name; /* add a NULL to terminate the argv array */ argv[3] = NULL; /* print debugging info if "very verbose" (-V) */ rtapi_print_msg(RTAPI_MSG_DBG, "%s %s %s\n", argv[0], argv[1], argv[2] ); /* call execv() to invoke rmmod */ execv(argv[0], argv); /* should never get here */ rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: execv(%s) failed\n", linenumber, argv[0] ); exit(1); } /* this is the parent process, wait for child to end */ retval = waitpid ( pid, &status, 0 ); /* reconnect to the HAL shmem area */ comp_id = hal_init(comp_name); if (comp_id < 0) { fprintf(stderr, "halrmt: hal_init() failed after unloadrt: %d\n", comp_id ); exit(-1); } hal_ready(comp_id); /* check result of waitpid() */ if ( retval < 0 ) { rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: waitpid(%d) failed\n", linenumber, pid); return -1; } if ( WIFEXITED(status) == 0 ) { rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: child did not exit normally\n", linenumber); return -1; } retval = WEXITSTATUS(status); if ( retval != 0 ) { rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: rmmod failed, returned %d\n", linenumber, retval); return -1; } /* print success message */ rtapi_print_msg(RTAPI_MSG_INFO, "Realtime module '%s' unloaded\n", mod_name); return 0;}static int doLoadUsr(char *args[]){ int wait_flag, wait_comp_flag, name_flag, ignore_flag; char *prog_name, *new_comp_name; char prog_path[MAX_CMD_LEN+1]; char *cp1, *cp2, *envpath; struct stat stat_buf; char *argv[MAX_TOK+1]; int n, m, retval, status; pid_t pid; if (hal_get_lock()&HAL_LOCK_LOAD) { rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: HAL is locked, loading of programs is not permitted\n", linenumber); return HAL_PERM; } /* check for options (-w, -i, and/or -r) */ wait_flag = 0; wait_comp_flag = 0; name_flag = 0; ignore_flag = 0; prog_name = NULL; while ( **args == '-' ) { /* this argument contains option(s) */ cp1 = *args; cp1++; while ( *cp1 != '\0' ) { if ( *cp1 == 'w' ) { wait_flag = 1; } else if ( *cp1 == 'W' ) { wait_comp_flag = 1; } else if ( *cp1 == 'i' ) { ignore_flag = 1; } else if ( *cp1 == 'n' ) { name_flag = 1; } else { rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: unknown loadusr option '-%c'\n", linenumber, *cp1); return HAL_INVAL; } cp1++; } /* move to next arg */ args++; } /* get program and component name */ if(name_flag) { new_comp_name = *args++; prog_name = *args++; } else { new_comp_name = prog_name = *args++; } /* need to find path to a program matching "prog_name" */ prog_path[0] = '\0'; if ( prog_path[0] == '\0' ) { /* try the name by itself */ strncpy (prog_path, prog_name, MAX_CMD_LEN); rtapi_print_msg(RTAPI_MSG_DBG, "Trying '%s'\n", prog_path); if ( stat(prog_path, &stat_buf) != 0 ) { /* no luck, clear prog_path to indicate failure */ prog_path[0] = '\0'; } } if ( prog_path[0] == '\0' ) { /* no luck yet, try the emc2/bin directory where the halrmt executable is located */ n = readlink("/proc/self/exe", prog_path, MAX_CMD_LEN-10); if ( n > 0 ) { prog_path[n] = '\0'; /* have path to executabie, find last '/' */ cp2 = ""; cp1 = prog_path; while ( *cp1 != '\0' ) { if ( *cp1 == '/' ) { cp2 = cp1; } cp1++; } if ( *cp2 == '/' ) { /* chop "halrmt" from end of path */ *(++cp2) = '\0'; /* append the program name */ strncat(prog_path, prog_name, MAX_CMD_LEN-strlen(prog_path)); /* and try it */ rtapi_print_msg(RTAPI_MSG_DBG, "Trying '%s'\n", prog_path); if ( stat(prog_path, &stat_buf) != 0 ) { /* no luck, clear prog_path to indicate failure */ prog_path[0] = '\0'; } } } } if ( prog_path[0] == '\0' ) { /* no luck yet, try the user's PATH */ envpath = getenv("PATH"); if ( envpath != NULL ) { while ( *envpath != '\0' ) { /* copy a single directory from the PATH env variable */ n = 0; while ( (*envpath != ':') && (*envpath != '\0') && (n < MAX_CMD_LEN)) { prog_path[n++] = *envpath++; } /* append '/' and program name */ if ( n < MAX_CMD_LEN ) { prog_path[n++] = '/'; } cp1 = prog_name; while ((*cp1 != '\0') && ( n < MAX_CMD_LEN)) { prog_path[n++] = *cp1++; } prog_path[n] = '\0'; rtapi_print_msg(RTAPI_MSG_DBG, "Trying '%s'\n", prog_path); if ( stat(prog_path, &stat_buf) != 0 ) { /* no luck, clear prog_path to indicate failure */ prog_path[0] = '\0'; /* and get ready to try the next directory */ if ( *envpath == ':' ) { envpath++; } } else { /* success, break out of loop */ break; } } } } if ( prog_path[0] == '\0' ) { /* still can't find a program to run */ rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: Can't find program '%s'\n", linenumber, prog_name); return -1; } /* now we need to fork, and then exec the program.... */ /* disconnect from the HAL shmem area before forking */ hal_exit(comp_id); comp_id = 0; /* now the fork() */ pid = fork(); if ( pid < 0 ) { rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: loadusr fork() failed\n", linenumber); /* reconnect to the HAL shmem area */ comp_id = hal_init(comp_name); if (comp_id < 0) { fprintf(stderr, "halrmt: hal_init() failed after fork: %d\n", comp_id); exit(-1); } hal_ready(comp_id); return -1; } if ( pid == 0 ) { /* this is the child process - prepare to exec() the program */ argv[0] = prog_name; /* loop thru remaining arguments */ n = 0; m = 1; while ( args[n][0] != '\0' ) { argv[m++] = args[n++]; } /* add a NULL to terminate the argv array */ argv[m] = NULL; /* print debugging info if "very verbose" (-V) */ rtapi_print_msg(RTAPI_MSG_DBG, "%s ", argv[0] ); n = 1; while ( argv[n] != NULL ) { rtapi_print_msg(RTAPI_MSG_DBG, "%s ", argv[n++] ); } rtapi_print_msg(RTAPI_MSG_DBG, "\n" ); /* call execv() to invoke the program */ execv(prog_path, argv); /* should never get here */ rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: execv(%s) failed: %s\n", linenumber, prog_name, strerror(errno)); exit(1); } /* this is the parent process, reconnect to the HAL shmem area */ comp_id = hal_init(comp_name); if (comp_id < 0) { fprintf(stderr, "halrmt: hal_init() failed after loadusr: %d\n", comp_id); exit(-1); } hal_ready(comp_id); if ( wait_comp_flag ) { int ready = 0, count=0; int next; while(!ready) { struct timespec ts = {0, 10 * 1000 * 1000}; // 10ms nanosleep(&ts, NULL); retval = waitpid( pid, &status, WNOHANG ); if(retval != 0) goto wait_common; rtapi_mutex_get(&(hal_data->mutex)); next = hal_data->comp_list_ptr; while(next) { hal_comp_t *comp = SHMPTR(next); next = comp->next_ptr; if(strcmp(comp->name, new_comp_name) == 0 && comp->ready) { ready = 1; break; } } rtapi_mutex_give(&(hal_data->mutex)); count++; if(count == 100) { fprintf(stderr, "Waiting for component '%s' to become ready.", new_comp_name); fflush(stderr); } else if(count > 100 && count % 10 == 0) { fprintf(stderr, "."); fflush(stderr); } } if (count >= 100) { fprintf(stderr, "\n"); } rtapi_print_msg(RTAPI_MSG_INFO, "Component '%s' ready\n", new_comp_name); } if ( wait_flag ) { /* wait for child process to complete */ retval = waitpid ( pid, &status, 0 ); /* check result of waitpid() */wait_common: if ( retval < 0 ) { rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: waitpid(%d) failed\n", linenumber, pid); return -1; } if ( WIFEXITED(status) == 0 ) { rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: program '%s' did not exit normally\n", linenumber, prog_name ); return -1; } if ( ignore_flag == 0 ) { retval = WEXITSTATUS(status); if ( retval != 0 ) { rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: ERROR: program '%s' failed, returned %d\n", linenumber, prog_name, retval ); return -1; } } /* print success message */ rtapi_print_msg(RTAPI_MSG_INFO, "Program '%s' finished\n", prog_name); } else { /* print success message */ rtapi_print_msg(RTAPI_MSG_INFO, "Program '%s' started\n", prog_name); } return 0;}static void getCompInfo(char *pattern, connectionRecType *context){ int next, len; hal_comp_t *comp; rtapi_mutex_get(&(hal_data->mutex)); len = strlen(pattern); next = hal_data->comp_list_ptr; while (next != 0) { comp = SHMPTR(next); if (strncmp(pattern, comp->name, len) == 0) { sprintf(context->outBuf, "COMP %s %02d %s", comp->name, comp->comp_id, (comp->type ? "RT " : "User")); sockWrite(context); } next = comp->next_ptr; } rtapi_mutex_give(&(hal_data->mutex));}static void getPinInfo(char *pattern, int valuesOnly, connectionRecType *context){ int next, len; hal_pin_t *pin; hal_comp_t *comp; hal_sig_t *sig; void *dptr; rtapi_mutex_get(&(hal_data->mutex)); len = strlen(pattern); next = hal_data->pin_list_ptr; while (next != 0) { pin = SHMPTR(next); if (strncmp(pattern, pin->name, len) == 0) { comp = SHMPTR(pin->owner_ptr); if (pin->signal != 0) { sig = SHMPTR(pin->signal); dptr = SHMPTR(sig->data_ptr); } else { sig = 0; dptr = &(pin->dummysig); } if (valuesOnly == 0) sprintf(context->outBuf, "PIN %s %s %02d %s %s", pin->name, data_value2((int) pin->type, dptr), comp->comp_id, data_type((int) pin->type), pin_data_dir((int) pin->dir)); else sprintf(context->outBuf, "PINVAL %s %s", pin->name, data_value2((int) pin->type, dptr)); sockWrite(context); } next = pin->next_ptr; } rtapi_mutex_give(&(hal_data->mutex));}static void getSigInfo(char *pattern, int valuesOnly, connectionRecType *context){ int next, len; hal_sig_t *sig; void *dptr; rtapi_mutex_get(&(hal_data->mutex)); len = strlen(pattern); next = hal_data->sig_list_ptr; while (next != 0) { sig = SHMPTR(next); if (strncmp(pattern, sig->name, len) == 0) { dptr = SHMPTR(sig->data_ptr); if (valuesOnly == 0) sprintf(context->outBuf, "SIGNAL %s %s %s", sig->name, data_value((int) sig->type, dptr), data_type((int) sig->type)); else sprintf(context->outBuf, "SIGNALVAL %s %s", sig->name, data_value((int) sig->type, dptr)); sockWrite(context);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -