void EnvReadInsert(FILE_NUM fnum, int offset, OBJECT env) { int pos; OBJECT x, loser; debug3(DET, DD, "EnvReadInsert(%s, %d, env %d)", FileName(fnum), offset, (int) env); /* to limit the cache size, remove least recently used entry if full */ if( cache_count >= MAX_CACHE ) { Child(loser, Down(env_cache)); DeleteLink(Up(loser)); DisposeChild(Up(loser)); cache_count--; } /* insert the new entry */ hash2(pos, fnum, offset); if( tab[pos] == nilobj ) New(tab[pos], ACAT); New(x, ACAT); env_fnum(x) = fnum; env_offset(x) = offset; env_read(x) = TRUE; Link(tab[pos], x); Link(env_cache, x); Link(x, env); cache_count++; debug1(DET, DD, "EnvReadInsert returning (cache_count = %d)", cache_count); } /* end EnvReadInsert */
static ssize_t show_fan_fault(struct device *dev, struct device_attribute *attr, char *buf) { int fan_nr = to_sensor_dev_attr(attr)->index; struct env *p = dev_get_drvdata(dev); u8 val = env_read(p, IREG_FAN_STAT); return sprintf(buf, "%d\n", (val & (1 << fan_nr)) ? 1 : 0); }
BOOLEAN EnvWriteRetrieve(OBJECT env, FILE_NUM fnum, int *offset, int *lnum) { unsigned int pos; OBJECT link, y, z; debug2(DET, DD, "EnvWriteRetrieve(env %d, %s)", (int) env, FileName(fnum)); debug1(DET, DDD, " %s", EchoObject(env)); stat_writes++; hash1(pos, env, fnum); if( tab[pos] != nilobj ) { for( link = Down(tab[pos]); link != tab[pos]; link = NextDown(link) ) { Child(y, link); Child(z, Down(y)); if( env_fnum(y) == fnum && z == env && !env_read(y) ) { MoveLink(LastUp(y), env_cache, PARENT); *offset = env_offset(y); *lnum = env_lnum(y); stat_write_hits++; debug2(DET, DD, "EnvWriteRetrieve returning TRUE (offset %d, lnum %d)", *offset, *lnum); return TRUE; } } } debug0(DET, DD, "EnvWriteRetrieve returning FALSE"); return FALSE; } /* end EnvWriteRetrieve */
int env_jail_status( struct envelope *env, int jail ) { SNET *snet_lock; int rc; if ( env == NULL ) { return( 0 ); } if (( jail != ENV_JAIL_PRISONER ) && ( jail != ENV_JAIL_PAROLEE )) { syslog( LOG_ERR, "Envelope.jail env <%s>: illegal jail code: %d", env->e_id, jail ); return( 1 ); } if (( env->e_jail != ENV_JAIL_PRISONER ) && ( env->e_jail != ENV_JAIL_PAROLEE )) { syslog( LOG_ERR, "Envelope.jail env <%s>: illegal env code: %d", env->e_id, env->e_jail ); return( 1 ); } if ( env->e_jail == jail ) { return( 0 ); } if ( env->e_hq != NULL ) { if ( jail == ENV_JAIL_PRISONER ) { env->e_hq->hq_jail_envs--; } else { env->e_hq->hq_jail_envs++; } } if ( env_read( READ_JAIL_INFO, env, &snet_lock ) != 0 ) { return( 0 ); } env_jail_set( env, jail ); rc = env_outfile( env ); if ( snet_close( snet_lock ) < 0 ) { syslog( LOG_ERR, "Liberror: env_jail_status snet_close: %m" ); } if ( rc != 0 ) { return( 1 ); } env_rcpt_free( env ); syslog( LOG_INFO, "Envelope.jail %s: %s", env->e_id, ( jail == ENV_JAIL_PRISONER ) ? "immured in durance vile" : "paroled" ); return( 0 ); }
static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf) { int temp_nr = to_sensor_dev_attr(attr)->index; struct env *p = dev_get_drvdata(dev); s8 val; val = env_read(p, IREG_LCL_TEMP + temp_nr); return sprintf(buf, "%d\n", ((int) val) - 64); }
/* ************************************************************************************************************ * * function * * 函数名称: * * 参数列表: * * 返回值 : * * 说明 : * * ************************************************************************************************************ */ static void env_restore(const char *env_name) { char *env_src; env_src = env_read(env_flash_dram_base, env_name); if(!env_src) { __inf("env error: no env need be rewrite\n"); } else { strcpy(mac_addr_store, env_src); env_write(env_data_dram_base, env_src, env_name); } }
static ssize_t show_fan_speed(struct device *dev, struct device_attribute *attr, char *buf) { int fan_nr = to_sensor_dev_attr(attr)->index; struct env *p = dev_get_drvdata(dev); int rpm, period; u8 val; val = env_read(p, IREG_FAN0 + fan_nr); period = (int) val << 8; if (FAN_DATA_VALID(period)) rpm = FAN_PERIOD_TO_RPM(period); else rpm = 0; return sprintf(buf, "%d\n", rpm); }
BOOLEAN EnvReadRetrieve(FILE_NUM fnum, int offset, OBJECT *env) { int pos; OBJECT link, y, z; debug2(DET, DD, "EnvReadRetrieve(%s, %d)", FileName(fnum), offset); stat_reads++; hash2(pos, fnum, offset); if( tab[pos] != nilobj ) { for( link = Down(tab[pos]); link != tab[pos]; link = NextDown(link) ) { Child(y, link); Child(z, Down(y)); if( env_fnum(y) == fnum && env_offset(y) == offset && env_read(y) ) { MoveLink(LastUp(y), env_cache, PARENT); Child(*env, Down(y)); stat_read_hits++; debug1(DET, DD, "EnvReadRetrieve returning env %d", (int) *env); return TRUE; } } } debug0(DET, DD, "EnvReadRetrieve returning FALSE"); return FALSE; } /* end EnvReadRetrieve */
int cmd_exec(cmd* head) { char* path = NULL; char* token = NULL; char* try = NULL; while(head != NULL) { head->pid = fork(); if(head->pid == 0) { // Child // Substitute STDIN if necessary if(head->fd_in != STDIN_FILENO) { close(STDIN_FILENO); dup(head->fd_in); close(head->fd_in); } // Substitute STDOUT if necessary if(head->fd_out != STDOUT_FILENO) { close(STDOUT_FILENO); dup(head->fd_out); close(head->fd_out); } if(head->argv[0][0] == '/') { // If full path then execute this way. execve(head->argv[0], head->argv, my_envp); } else { env_read("PATH", &path); token = strtok(path, ":"); while(token != NULL) { // printf("Trying %s\n", token); try = malloc(sizeof(char)*(strlen(token)+strlen(head->argv[0])+2)); try[0] = '\0'; strcat(try, token); strcat(try, "/"); strcat(try, head->argv[0]); execve(try, head->argv, my_envp); token = strtok(NULL, ":"); free(try); } } perror("execve"); // printf("Failed to execute\n"); exit(EXIT_FAILURE); } else { // Parent if(head->fd_in != STDIN_FILENO) close(head->fd_in); if(head->fd_out != STDOUT_FILENO) close(head->fd_out); wait(NULL); head = (cmd*) head->next; } } return 0; }
int rtcnvet_do_setenv(char *name, char *val) { struct env_data ed; char *p, *q; int j, k, nxt; unsigned int crc32; int len; #ifdef DEBUGME printk(KERN_ERR\ "********* %s called! ***********\n",\ __func__); #endif /*DEBUGME*/ /*sanity*/ if (NULL == name) { printk(KERN_ERR\ "%s: Called with incorrect data!\n", __func__); return -1; } if (strchr(name, '=')) { printk(KERN_ERR\ "%s: Illegal character '=' in variable name \"%s\"\n",\ __func__, name); return -2; } /*read env*/ if (0 == env_read(&ed)) { #ifdef DEBUGME printk(KERN_ERR\ "%s: Env read, processing!\n",\ __func__); #endif /*DEBUGME*/ /* find prev. value, if any * If found, move env over it! */ k = -1; nxt = 0; p = ed.data + ENV_HEADER_SIZE; for (j = 0; *(p+j) != '\0' && j+ENV_HEADER_SIZE < ed.size; j = nxt+1) { for (nxt = j; *(p+nxt) != '\0' && nxt+ENV_HEADER_SIZE < ed.size; ++nxt); /* search name in *(p+j) ~ key=value*/ if (j+ENV_HEADER_SIZE < ed.size) { q = strstr(p+j, "="); if (NULL != q) { *q = 0; if (0 == strcmp(name, p+j)) { /* remove it! */ *q = '='; k = 1; break; } *q = '='; } } } if ( 1 == k) { #ifdef DEBUGME printk(KERN_ERR\ "%s: Prev. value found, removing...!\n",\ __func__); #endif /*DEBUGME*/ /* p+j - start of env */ /* p+nxt - start of next env - 1 */ nxt = nxt + 1; /* last thing on the env? */ if (*(p+nxt) == '\0') { *(p+j) = '\0'; } else { for (;;) { *(p+j) = *(p+nxt); nxt++; if ((*(p+j) == '\0') && (*(p+nxt) == '\0')) break; j++; } } j++; *(p+j) = '\0'; } /* set something? */ if (NULL != val) { #ifdef DEBUGME printk(KERN_ERR\ "%s: Inserting a new value!\n",\ __func__); #endif /*DEBUGME*/ for (j = 0; (*(p+j) || *(p+j+1)) && (j < ed.size - ENV_HEADER_SIZE); j++); if (j > 0 && j < ed.size - ENV_HEADER_SIZE) j++; /* j is before the last 0 */ /* check for overflow */ /* name + "=" + val + "\0\0" > ed.size - ENV_HEADER_SIZE - j */ len = strlen(name); len = len + 1; len = strlen(val); len = len + 2; if (len > ed.size - ENV_HEADER_SIZE - j) { printk(KERN_ERR \ "%s: Environment overflow, \"%s\" deleted!\n",\ __func__, name); } else { /* set it */ while ((*(p+j) = *name++) != '\0') j++; *(p+j) = '='; j++; while ((*(p+j) = *val++) != '\0') j++; /* end is marked with double '\0' */ j++; *(p+j) = '\0'; } } #ifdef DEBUGME printk(KERN_ERR\ "%s: Env changed, updating CRC!\n",\ __func__); #endif /*DEBUGME*/ /* update crc now */ crc32 = ubcrc32(0, &ed.data[ENV_HEADER_SIZE], ed.size - ENV_HEADER_SIZE); *((uint32_t *)&(ed.data[0])) = crc32; env_save(&ed); /* ed is not valid anymore! */ return 0; } else { printk(KERN_ERR\ "%s: Cannot find a valid env!\n",\ __func__); return -1; } #ifdef DEBUGME printk(KERN_ERR\ "%s: All done, exiting!\n",\ __func__); #endif /*DEBUGME*/ return 0; }
int rtcnvet_get_env(char *name, char **buf) { struct env_data ed; int j, k, nxt; char *p; char *q; #ifdef DEBUGME printk(KERN_ERR\ "********* %s called! ***********\n",\ __func__); #endif /*DEBUGME*/ /*sanity*/ if (NULL == buf || NULL == name) { printk(KERN_ERR\ "%s: Called with invalid data!\n",\ __func__); return -1; } if (NULL == name) { *buf = NULL; printk(KERN_ERR\ "%s: Returnig error, no name provided!\n",\ __func__); return -2; } /*read env*/ if (0 == env_read(&ed)) { #ifdef DEBUGME printk(KERN_ERR\ "%s: Env processing!\n",\ __func__); #endif /*DEBUGME*/ /*get env*/ k = -1; nxt = 0; p = ed.data + ENV_HEADER_SIZE; for (j = 0; *(p+j) != '\0' && j+ENV_HEADER_SIZE < ed.size; j = nxt+1) { for (nxt = j; *(p+nxt) != '\0' && nxt+ENV_HEADER_SIZE < ed.size; ++nxt); /* search name in *(p+j) ~ key=value*/ if (j+ENV_HEADER_SIZE < ed.size) { q = strstr(p+j, "="); if (NULL != q) { *q = 0; if (0 == strcmp(name, p+j)) { k = q-p+1; *q = '='; break; } *q = '='; } else { printk(KERN_ERR\ "%s: \"%s\" is not in the key=value format!\n",\ __func__, p+j); } } } if (k < 0) { printk(KERN_ERR\ "%s: Error: \"%s\" not defined!\n",\ __func__, name); *buf = NULL; } else { nxt = strlen(p+k); nxt++; *buf=kmalloc(nxt,GFP_KERNEL); strncpy(*buf, p+k, nxt); #ifdef DEBUGME printk(KERN_ERR\ "%s: Got: \"%s\"!\n",\ __func__, *buf); #endif /*DEBUGME*/ } /* tear down ed*/ #ifdef DEBUGME printk(KERN_ERR\ "%s: Tearing down data!\n",\ __func__); #endif /*DEBUGME*/ ed.mtd = -1; kfree(ed.data); ed.data = NULL; put_mtd_device(ed.mtds[0]); ed.mtds[0] = NULL; put_mtd_device(ed.mtds[1]); ed.mtds[1] = NULL; #ifdef DEBUGME printk(KERN_ERR\ "%s: Tearing down complete!\n",\ __func__); #endif /*DEBUGME*/ return (*buf) ? 0 : -1; } else { printk(KERN_ERR\ "%s: Failed to read the env block!\n",\ __func__); return -3; } return 0; }
int main(int argc, char *argv[]) { FILE *f = NULL; int count; int err = 0; int len; int idx; int i; char *name; char *value; init_private(); f = fopen(BOOTENV_MMCBLK, "rb+"); if (NULL==f) { println("can not open " BOOTENV_MMCBLK); err = -1; goto exit; } if (env_read(f)) { err = -1; goto exit; } /* * display all */ if (1==argc) { for (i=1; i<AT_ENV_COUNT; i++) { if (bootenv[i][0] && !(ENV_HIDDEN & envctl[i].flag)) { println("%s=%s", envctl[i].name, bootenv[i]); } } err = 0; goto exit; } else if (2==argc) { char *name = argv[1]; /* * help */ if (0==strcmp("-h", name) || 0==strcmp("--help", name)) { usage(argc, argv); err = 0; goto exit; } /* * get by name */ else if (NULL==strchr(name, '=')) { value = getvalue_byname(name); if (NULL==value) { println("argv[1](%s) bad name", name); err = -1; } else if (0==value[0]) { /* empty */ err = 0; } else { println("%s", value); err = 0; } goto exit; } } /* * set by name */ for (i=1; i<argc; i++) { char line[2*AT_ENV_LINE_SIZE] = {0}; len = strlen(argv[i]); /* * check input length */ if (len > (2*AT_ENV_LINE_SIZE-1)) { println("argv[%d](%s) too long", i, argv[i]); err = -1; goto exit; } strcpy(line, argv[i]); /* * get name */ name = line; value = strchr(line, '='); if (NULL==value) { println("argv[%d](%s) should as xxx=xxxx", i, argv[i]); err = -1; goto exit; } *value = 0; value++; /* * check name */ idx = getidx_byname(name); if (idx<0) { println("argv[%d](%s) bad name(%s)", i, argv[i], name); err = -1; goto exit; } /* * check value */ if (0==value[0]) { bootenv[idx][0] = 0; } else if (strlen(value) > (AT_ENV_LINE_SIZE-1)) { println("argv[%d](%s) value length max %d", i, argv[i], (AT_ENV_LINE_SIZE-1)); err = -1; goto exit; } else if (env_check(idx, value) < 0) { println("argv[%d](%s) value is invalid", i, argv[i]); err = -1; goto exit; } else if (ENV_READONLY & envctl[idx].flag) { println("argv[%d](%s) is readonly", i, argv[i]); err = -1; goto exit; } else { strcpy(bootenv[idx], value); envctl[idx].flag |= ENV_CHANGED; } } if (env_write(f)) { err = -1; goto exit; } exit: fclose(f); return err; }