⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 halcmd.c

📁 Source code for an Numeric Cmputer
💻 C
📖 第 1 页 / 共 5 页
字号:
    if (rtapi_get_msg_level() == RTAPI_MSG_NONE) {	/* must be -Q, don't print anything */	return 0;    }    if (*type == '\0') {	/* print everything */	print_comp_info("");	print_pin_info("");	print_sig_info("");	print_param_info("");	print_funct_info("");	print_thread_info("");    } else if (strcmp(type, "all") == 0) {	/* print everything, using the pattern */	print_comp_info(pattern);	print_pin_info(pattern);	print_sig_info(pattern);	print_param_info(pattern);	print_funct_info(pattern);	print_thread_info(pattern);    } else if (strcmp(type, "comp") == 0) {	print_comp_info(pattern);    } else if (strcmp(type, "pin") == 0) {	print_pin_info(pattern);    } else if (strcmp(type, "sig") == 0) {	print_sig_info(pattern);    } else if (strcmp(type, "param") == 0) {	print_param_info(pattern);    } else if (strcmp(type, "funct") == 0) {	print_funct_info(pattern);    } else if (strcmp(type, "thread") == 0) {	print_thread_info(pattern);    } else {	rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: Unknown 'show' type '%s'\n", linenumber, type);	return -1;    }    return 0;}static int do_list_cmd(char *type, char *pattern){    if (rtapi_get_msg_level() == RTAPI_MSG_NONE) {	/* must be -Q, don't print anything */	return 0;    }    if (strcmp(type, "comp") == 0) {	print_comp_names(pattern);    } else if (strcmp(type, "pin") == 0) {	print_pin_names(pattern);    } else if (strcmp(type, "sig") == 0) {	print_sig_names(pattern);    } else if (strcmp(type, "param") == 0) {	print_param_names(pattern);    } else if (strcmp(type, "funct") == 0) {	print_funct_names(pattern);    } else if (strcmp(type, "thread") == 0) {	print_thread_names(pattern);    } else {	rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: Unknown 'list' type '%s'\n", linenumber, type);	return -1;    }    return 0;}static int do_status_cmd(char *type){    if (rtapi_get_msg_level() == RTAPI_MSG_NONE) {	/* must be -Q, don't print anything */	return 0;    }    if ((*type == '\0') || (strcmp(type, "all") == 0)) {	/* print everything */	/* add other status functions here if/when they are defined */	print_lock_status();	print_mem_status();    } else if (strcmp(type, "lock") == 0) {	print_lock_status();    } else if (strcmp(type, "mem") == 0) {	print_mem_status();    } else {	rtapi_print_msg(RTAPI_MSG_ERR, "HAL:%d: Unknown 'status' type '%s'\n", linenumber, type);	return -1;    }    return 0;}static int do_loadrt_cmd(char *mod_name, char *args[]){    /* note: these are static so that the various searches can       be skipped for subsequent commands */    static char *rtmod_dir = HAL_RTMOD_DIR;    static char path_buf[MAX_CMD_LEN+1];    struct stat stat_buf;    char mod_path[MAX_CMD_LEN+1];    char *cp1, *cp2;    char *argv[MAX_TOK+1];    char arg_string[MAX_CMD_LEN+1];    int n, m, retval, status;    hal_comp_t *comp;    pid_t pid;    if (hal_get_lock()&HAL_LOCK_LOAD) {	rtapi_print_msg(RTAPI_MSG_ERR,	    "HAL:%d: ERROR: HAL is locked, loading of modules is not permitted\n", linenumber);	return HAL_PERM;    }    if ( (strlen(rtmod_dir)+strlen(mod_name)+5) > MAX_CMD_LEN ) {	rtapi_print_msg(RTAPI_MSG_ERR,	    "HAL:%d: ERROR: Module path too long\n", linenumber);	return -1;    }    /* make full module name '<path>/<name>.o' */    strcpy (mod_path, rtmod_dir);    strcat (mod_path, "/");    strcat (mod_path, mod_name);    strcat (mod_path, ".o");    /* is there a file with that name? */    if ( stat(mod_path, &stat_buf) != 0 ) {	/* nope, try .ko (for kernel 2.6 */	cp2 = strrchr(mod_path, '.' );	strcpy(cp2, ".ko" );	if ( stat(mod_path, &stat_buf) != 0 ) {	    /* can't find it */	    rtapi_print_msg(RTAPI_MSG_ERR,		"HAL:%d: ERROR: Can't find module '%s' in %s\n", linenumber, mod_name, path_buf);	    return -1;	}    }    /* now we need to fork, and then exec insmod.... */    /* 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: loadrt fork() failed\n", linenumber);	/* reconnect to the HAL shmem area */	comp_id = hal_init(comp_name);	if (comp_id < 0) {	    fprintf(stderr, "halcmd: hal_init() failed after fork\n" );	    exit(-1);	}	return -1;    }    if ( pid == 0 ) {	/* this is the child process - prepare to exec() insmod */	argv[0] = EMC2_BIN_DIR "/emc_module_helper";        argv[1] = "insert";	argv[2] = mod_path;	/* loop thru remaining arguments */	n = 0;	m = 3;	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 %s %s ", argv[0], argv[1], argv[2] );	n = 3;	while ( argv[n] != NULL ) {	    rtapi_print_msg(RTAPI_MSG_DBG, "%s ", argv[n++] );	}	rtapi_print_msg(RTAPI_MSG_DBG, "\n" );        /* call execv() to invoke insmod */	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, "halcmd: hal_init() failed after loadrt\n" );	exit(-1);    }    /* 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: insmod failed, returned %d\n", linenumber, retval );	return -1;    }    /* make the args that were passed to the module into a single string */    n = 0;    arg_string[0] = '\0';    while ( args[n][0] != '\0' ) {	strncat(arg_string, args[n++], MAX_CMD_LEN);	strncat(arg_string, " ", MAX_CMD_LEN);    }    /* allocate HAL shmem for the string */    cp1 = hal_malloc(strlen(arg_string)+1);    if ( cp1 == NULL ) {	rtapi_print_msg(RTAPI_MSG_ERR,	    "HAL:%d: ERROR: failed to allocate memory for module args\n",	     linenumber );	return -1;    }    /* copy string to shmem */    strcpy (cp1, arg_string);    /* get mutex before accessing shared data */    rtapi_mutex_get(&(hal_data->mutex));    /* search component list for the newly loaded component */    comp = halpr_find_comp_by_name(mod_name);    if (comp == 0) {	rtapi_mutex_give(&(hal_data->mutex));	rtapi_print_msg(RTAPI_MSG_ERR,	    "HAL:%d: ERROR: module '%s' not loaded\n", linenumber, mod_name);	return HAL_INVAL;    }    /* link args to comp struct */    comp->insmod_args = SHMOFF(cp1);    rtapi_mutex_give(&(hal_data->mutex));    /* print success message */    rtapi_print_msg(RTAPI_MSG_INFO, "Realtime module '%s' loaded\n",	mod_name);    return 0;}static int do_delsig_cmd(char *mod_name){    int next, retval, retval1, n;    hal_sig_t *sig;    char sigs[MAX_EXPECTED_SIGS][HAL_NAME_LEN+1];    /* check for "all" */    if ( strcmp(mod_name, "all" ) != 0 ) {	retval = hal_signal_delete(mod_name);	if (retval == 0) {	    /* print success message */	    rtapi_print_msg(RTAPI_MSG_INFO, "Signal '%s' deleted'\n",		mod_name);	}	return retval;    } else {	/* build a list of signal(s) to delete */	n = 0;	rtapi_mutex_get(&(hal_data->mutex));	next = hal_data->sig_list_ptr;	while (next != 0) {	    sig = SHMPTR(next);	    /* we want to unload this signal, remember it's name */	    if ( n < ( MAX_EXPECTED_SIGS - 1 ) ) {	        strncpy(sigs[n++], sig->name, HAL_NAME_LEN );	    }	    next = sig->next_ptr;	}	rtapi_mutex_give(&(hal_data->mutex));	sigs[n][0] = '\0';	if ( ( sigs[0][0] == '\0' )) {	    /* desired signals not found */	    rtapi_print_msg(RTAPI_MSG_ERR,		"HAL:%d: ERROR: no signals found to be deleted\n", linenumber);	    return -1;	}	/* we now have a list of components, unload them */	n = 0;	retval1 = 0;	while ( sigs[n][0] != '\0' ) {	    retval = hal_signal_delete(sigs[n]);	/* check for fatal error */	    if ( retval < -1 ) {		return retval;	    }	    /* check for other error */	    if ( retval != 0 ) {		retval1 = retval;	    }	    if (retval == 0) {		/* print success message */		rtapi_print_msg(RTAPI_MSG_INFO, "Signal '%s' deleted'\n",		sigs[n]);	    }	    n++;	}    }    return retval1;}static int do_unloadrt_cmd(char *mod_name){    int next, retval, retval1, n, all;    hal_comp_t *comp;    char comps[64][HAL_NAME_LEN+1];    /* check for "all" */    if ( strcmp(mod_name, "all" ) == 0 ) {	all = 1;    } else {	all = 0;    }    /* build a list of component(s) to unload */    n = 0;    rtapi_mutex_get(&(hal_data->mutex));    next = hal_data->comp_list_ptr;    while (next != 0) {	comp = SHMPTR(next);	if ( comp->type == 1 ) {	    /* found a realtime component */	    if ( all || ( strcmp(mod_name, comp->name) == 0 )) {		/* we want to unload this component, remember it's name */		if ( n < 63 ) {		    strncpy(comps[n++], comp->name, HAL_NAME_LEN );		}	    }	}	next = comp->next_ptr;    }    rtapi_mutex_give(&(hal_data->mutex));    /* mark end of list */    comps[n][0] = '\0';    if ( !all && ( comps[0][0] == '\0' )) {	/* 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, "halcmd: hal_init() failed after fork\n" );	    exit(-1);	}	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, "halcmd: hal_init() failed after unloadrt\n" );	exit(-1);    }    /* 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 do_loadusr_cmd(char *args[]){    int wait_flag, ignore_flag;    char *prog_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,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -