char* aim_vfstrdup(const char* fmt, va_list vargs) { char b[128]; AIM_VSNPRINTF(b, sizeof(b)-1, fmt, vargs); return aim_strdup(b); }
int aim_log_handler_basic_init_all(const char* ident, const char* debug_log, int max_debug_log_size, int max_debug_logs) { aim_log_handler_config_t config; AIM_MEMSET(&config, 0, sizeof(config)); if(isatty(1)) { /** Assume interactive and log to stdout */ config.flags |= AIM_LOG_HANDLER_FLAG_TO_STDOUT; } else if(ident) { /** Assume daemonized and send to syslog */ openlog(ident, LOG_PID, LOG_DAEMON); config.flags |= AIM_LOG_HANDLER_FLAG_TO_SYSLOG; config.syslog_facility = LOG_DAEMON; } if(debug_log) { /** Log to debug log file */ config.flags |= AIM_LOG_HANDLER_FLAG_TO_DBGLOG; config.debug_log_name = aim_strdup(debug_log); config.max_debug_log_size = max_debug_log_size; config.max_debug_logs = max_debug_logs; } basic_handler__ = aim_log_handler_create(&config); aim_logf_set_all("{aim_log_handler}", aim_log_handler_logf, basic_handler__); aim_log_option_set_all(AIM_LOG_OPTION_TIMESTAMP, 0); return 0; }
int onlp_sysi_onie_info_get(onlp_onie_info_t* onie) { if(onie){ onie->platform_name = aim_strdup(ONIE_PLATFORM_NAME); } return ONLP_STATUS_OK; }
static int aim_datatype_ts__rmap__(aim_datatype_context_t* dtc, aim_va_list_t* vargs, const char** rv) { aim_datatype_map_t* map = (aim_datatype_map_t*) dtc->dt->cookie; aim_datatype_map_t* p; if((p = aim_datatype_map_find_name(map, va_arg(vargs->val, int)))) { *rv = aim_strdup(p->s); } else {
int onlp_sysi_onie_info_get(onlp_onie_info_t* onie) { int rv = onlp_onie_read_json(onie, "/lib/platform-config/current/onl/etc/onie/eeprom.json"); if(rv >= 0) { if(onie->platform_name) { aim_free(onie->platform_name); } onie->platform_name = aim_strdup(ONIE_PLATFORM_NAME); } return rv; }
/**************************************************************************//** * * Parse a string based on the specified delimiter. * * *****************************************************************************/ biglist_t* vpi_parse_list(const char* string, char* delim, int* len) { biglist_t* bl = NULL; /* Internal token buffer */ char* _tokens = aim_strdup(string); char* arg; char* save_ptr; int count = 0; arg = aim_strtok_r(_tokens, delim, &save_ptr); while(arg) { bl = biglist_append(bl, aim_strdup(arg)); arg = aim_strtok_r(NULL, delim, &save_ptr); count++; } aim_free(_tokens); if(len) { *len = count; } return bl; }
aim_log_handler_t aim_log_handler_create(aim_log_handler_config_t* config) { aim_log_handler_t rv; rv = aim_zmalloc(sizeof(*rv)); AIM_MEMCPY(&rv->config, config, sizeof(rv->config)); if(config->debug_log_name) { rv->config.debug_log_name = aim_strdup(config->debug_log_name); rv->debug_fp = fopen(rv->config.debug_log_name, "a"); rv->debug_lock = aim_sem_create(1); } return rv; }
/** * @brief Open a file or domain socket. * @param dst Receives the full filename (for logging purposes). * @param flags The open flags. * @param fmt Format specifier. * @param vargs Format specifier arguments. */ static int vopen__(char** dst, int flags, const char* fmt, va_list vargs) { int fd; struct stat sb; char fname[PATH_MAX]; char* asterisk; ONLPLIB_VSNPRINTF(fname, sizeof(fname)-1, fmt, vargs); /** * An asterisk in the filename separates a search root * directory from a filename. */ if( (asterisk = strchr(fname, '*')) ) { char* root = fname; char* rpath = NULL; *asterisk = 0; if(onlp_file_find(root, asterisk+1, &rpath) < 0) { return ONLP_STATUS_E_MISSING; } strcpy(fname, rpath); aim_free(rpath); } if(dst) { *dst = aim_strdup(fname); } if(stat(fname, &sb) == -1) { return ONLP_STATUS_E_MISSING; } if(S_ISSOCK(sb.st_mode)) { fd = ds_connect__(fname); } else { fd = open(fname, flags); } return (fd > 0) ? fd : ONLP_STATUS_E_MISSING; }
ucli_tokens_t* ucli_tokens(const char* string, const char* delim) { const char** tokens; char* tok; int count; char* saveptr; char* s; ucli_tokens_t* rv; /* * Determine how many tokens we'll have */ s = aim_strdup(string); count = 0; tok = aim_strtok_r(s, delim, &saveptr); while(tok) { count++; tok = aim_strtok_r(NULL, delim, &saveptr); } tokens = aim_zmalloc((sizeof(char*)*(count+2))); /* reset string */ UCLI_STRCPY(s, string); tok = aim_strtok_r(s, delim, &saveptr); count = 0; while(tok) { tokens[count++] = tok; tok = aim_strtok_r(NULL, delim, &saveptr); } rv = aim_zmalloc(sizeof(*rv)); rv->count = count; rv->tokens = tokens; rv->_string = s; return rv; }
int wnc_sys_eeprom_to_onie(const wnc_sys_eeprom_t* src, onlp_onie_info_t* dst) { if(src == NULL || dst == NULL) { return -1; } memset(dst, 0, sizeof(*dst)); list_init(&dst->vx_list); dst->product_name = aim_strdup(src->product_name); dst->part_number = aim_strdup(src->part_number); dst->serial_number = aim_strdup(src->serial_number); memcpy(dst->mac, src->mac_address, 6); dst->manufacture_date = aim_strdup(src->manufacture_date); dst->label_revision = aim_strdup(src->label_version); dst->mac_range = 1; dst->manufacturer = aim_strdup("Wnc"); dst->vendor = aim_strdup("WncMesh"); return 0; }
static void platform_string_register(int index, const char* desc, char* value) { oid tree[] = { 1, 3, 6, 1, 4, 1, 42623, 1, 1, 1, 1, 1}; tree[11] = index; if(!value || !value[0]) { return; } char* s = aim_strdup(value); netsnmp_handler_registration *reg = netsnmp_create_handler_registration( desc, NULL, tree, OID_LENGTH(tree), HANDLER_CAN_RONLY); netsnmp_watcher_info *winfo = netsnmp_create_watcher_info( s, strlen(s), ASN_OCTET_STR, WATCHER_FIXED_SIZE); netsnmp_register_watched_scalar( reg, winfo ); }
} aim_pvs_printf(dtc->epvs, "\n"); return -1; } static int aim_datatype_ts__rmap__(aim_datatype_context_t* dtc, aim_va_list_t* vargs, const char** rv) { aim_datatype_map_t* map = (aim_datatype_map_t*) dtc->dt->cookie; aim_datatype_map_t* p; if((p = aim_datatype_map_find_name(map, va_arg(vargs->val, int)))) { *rv = aim_strdup(p->s); } else { *rv = aim_strdup("[Unknown]"); } return 0; } int aim_datatype_register_map(char c, const char* type, const char* desc, aim_datatype_map_t* map) { return aim_datatype_register(c, type, desc, aim_datatype_fs__rmap__, aim_datatype_ts__rmap__, map); } /**************************************************************************//**
vpi_ip_endpoint_t* vpi_parse_ip_endpoint(const char* string) { vpi_ip_endpoint_t* ne; char** tokens; int len = 0; char** arg; if( (tokens = vpi_parse_array(string, ":", &len)) == NULL) { return NULL; } if(len != 2 && len != 3) { /* Need between 2 and 3 tokens only */ vpi_parse_arrayFree(tokens); return NULL; } arg = tokens; /* * First token must be 'send', or 's', or 'recv', or 'r' */ if(VPI_STRCMP(*arg, "send") && VPI_STRCMP(*arg, "recv") && VPI_STRCMP(*arg, "r") && VPI_STRCMP(*arg, "s")) { /* None of them */ vpi_parse_arrayFree(tokens); return NULL; } ne = aim_zmalloc(sizeof(*ne)); ne->direction = aim_strdup(*arg); arg++; if(len == 2) { /* The argument must be a port number. */ ne->port = VPI_ATOI(*arg); if(ne->port < 0) { /* Invalid */ goto vpi_parse_ip_endpoint_Error; } else { /* localhost is implied if not specified */ ne->host = aim_strdup("0.0.0.0"); } } else { ne->host = aim_strdup(*arg); arg++; ne->port = VPI_ATOI(*arg); if(ne->port <= 0) { /* Invalid */ goto vpi_parse_ip_endpoint_Error; } } /* Success - 'ne' is now initialized */ vpi_parse_arrayFree(tokens); return ne; vpi_parse_ip_endpoint_Error: vpi_parse_arrayFree(tokens); vpi_free_ip_endpoint(&ne); return NULL; }
static void commit_lua_upload(indigo_cxn_id_t cxn_id, of_object_t *msg) { uint16_t flags; of_bsn_lua_upload_flags_get(msg, &flags); /* TODO use stronger hash function */ uint32_t new_checksum = murmur_hash(xbuf_data(&upload_chunks), xbuf_length(&upload_chunks), 0); if (!(flags & OFP_BSN_LUA_UPLOAD_FORCE) && checksum == new_checksum) { AIM_LOG_VERBOSE("Skipping Lua commit, checksums match"); goto cleanup; } checksum = 0; reset_lua(); uint32_t offset = 0; while (offset < xbuf_length(&upload_chunks)) { struct upload_chunk *chunk = xbuf_data(&upload_chunks) + offset; offset += sizeof(*chunk) + chunk->size; AIM_LOG_VERBOSE("Loading Lua chunk %s, %u bytes", chunk->filename, chunk->size); char name[64]; snprintf(name, sizeof(name), "=%s", chunk->filename); if (luaL_loadbuffer(lua, chunk->data, chunk->size, name) != 0) { AIM_LOG_ERROR("Failed to load code: %s", lua_tostring(lua, -1)); indigo_cxn_send_error_reply( cxn_id, msg, OF_ERROR_TYPE_BAD_REQUEST, OF_REQUEST_FAILED_EPERM); goto cleanup; } /* Set the environment of the new chunk to the sandbox */ lua_getglobal(lua, "sandbox"); lua_setfenv(lua, -2); if (lua_pcall(lua, 0, 1, 0) != 0) { AIM_LOG_ERROR("Failed to execute code %s: %s", chunk->filename, lua_tostring(lua, -1)); indigo_cxn_send_error_reply( cxn_id, msg, OF_ERROR_TYPE_BAD_REQUEST, OF_REQUEST_FAILED_EPERM); goto cleanup; } /* Save the return value in the "modules" table, used by require */ char *module_name = aim_strdup(chunk->filename); char *dot = strrchr(module_name, '.'); if (dot) *dot = 0; /* strip file extension */ lua_getglobal(lua, "modules"); lua_pushstring(lua, module_name); lua_pushvalue(lua, -3); /* return value from pcall */ lua_rawset(lua, -3); /* modules[filename] = return_value */ lua_pop(lua, 2); /* pop modules and return value */ free(module_name); } checksum = new_checksum; cleanup: cleanup_lua_upload(); return; }
int sff_tool(int argc, char* argv[]) { int rv = 0; int i; int c; int s = 0; int e = 0; int help = 0; int n = 0; int v = 0; biglist_t* fnames=NULL; while( (c = getopt(argc, argv, "sehnv")) != -1) { switch(c) { case 's': s = 1; break; case 'e': e = 1; break; case 'h': help=1; rv=0; break; case 'n': n=1; break; case 'v': v=1; break; default: help=1; rv = 1; break; } } if(help) { printf("Usage: %s [OPTIONS] [FILES]\n", argv[0]); printf(" -s Read filenames from stdin. \n"); printf(" -n Print the filename if successful.\n"); printf(" -v Print the filename always.\n"); printf(" -e Show the raw eeprom data.\n"); printf(" -h Help.\n"); return rv; } if(s) { /* Read filenames from stdin */ char b[PATH_MAX]; char* l; while((l = fgets(b, PATH_MAX, stdin))) { int len=SFF_STRLEN(l); if(len) { if(l[len-1] == '\n') { l[len-1] = 0; } fnames = biglist_append(fnames, aim_strdup(l)); } } } /* Read from command line. This can be used together with -s */ for(i = optind; i < argc; i++) { fnames = biglist_append(fnames, aim_strdup(argv[i])); } biglist_t* ble; char* f; BIGLIST_FOREACH_DATA(ble, fnames, char*, f) { sff_info_t info; memset(&info, 0, sizeof(info)); if( (rv = sff_info_init_file(&info, f)) >= 0) { if(n || v) { aim_printf(&aim_pvs_stdout, "%s\n", f); } if(e) { aim_printf(&aim_pvs_stdout, "eeprom:\n%{data}\n", info.eeprom, sizeof(info.eeprom)); } sff_info_show(&info, &aim_pvs_stdout); } else if(v) { aim_printf(&aim_pvs_stdout, "%s: failed.\n", f); } }
char* vpi_bpp_interface_preprocess(char* args[], const char* create_spec) { char** arg = args; char track_script[256]; char cmd[256]; int port; char* type = "udp"; AIM_REFERENCE(create_spec); /* * First arg is bpp */ arg++; /* * The second argument may be an optional selection for * UDP or TCP. The default is udp. */ if(!VPI_STRCMP("tcp", *arg) || !VPI_STRCMP("udp", *arg)) { type = *arg; arg++; } /* * Support special localhost processing. * The special "localhost:port" target points directly to * localhost on the given port number. * * This is mostly a testing convenience. * */ if(VPI_SSCANF(*arg, "localhost:%d", &port) == 1) { VPI_SPRINTF(cmd, "*s|%s|send:localhost:%d|recv:0", type, port); } else { /* * Determine the UDP spec we need to talk to the target. */ char server[64] = {0}; FILE* rv; if(getenv("TRACK_SCRIPT")) { VPI_STRCPY(track_script, getenv("TRACK_SCRIPT")); } else if(getenv("BIGLAB")) { VPI_SPRINTF(track_script, "%s/track", getenv("BIGLAB")); } else { VPI_STRCPY(track_script, "/usr/bin/track"); } if(access(track_script, F_OK) != 0) { VPI_MERROR("Cannot access track script @ %s", track_script); return NULL; } VPI_SPRINTF(cmd, "%s vpi %s %s", track_script, type, *arg); rv = popen(cmd, "r"); if(VPI_SSCANF(fgets(cmd, sizeof(cmd), rv), "%[^:]:%d", server, &port) == 2) { VPI_MINFO("track: server=%s port=%d", server, port); } else { VPI_MERROR("error reading output from track script."); return NULL; } VPI_SPRINTF(cmd, "*s|%s|send:%s:%d|recv:0", type, server, port); } return aim_strdup(cmd); }