/** * uid_map_from_file: called from oh_uid_initialize() during intializaion * this function if a uid map file exists reads the current value for * uid, and intializes the memory resident uid map file from file. * * * * Return value: success 0, error -1. **/ static int uid_map_from_file(void) { char *uid_map_file; int file; int rval; /* initialize uid map file */ uid_map_file = (char *)getenv("UID_MAP"); if (uid_map_file == NULL) { uid_map_file = OH_DEFAULT_UID_MAP; } file = open(uid_map_file, O_RDONLY); if(file < 0) { /* create map file with resource id initial value */ dbg("Configuration file '%s' does not exist, initializing", uid_map_file); file = open(uid_map_file, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH ); if(file < 0) { dbg("Could not initialize uid map file, %s", uid_map_file ); return(-1); } /* write initial uid value */ if( write(file,(void *)&resource_id, sizeof(resource_id)) < 0 ) { dbg("failed to write uid, on uid map file initialization"); close(file); return(-1); } if(close(file) != 0) { dbg("Couldn't close file '%s'.during uid map file initialization", uid_map_file); return(-1); } /* return from successful initialization, from newly created uid map file */ return(0); } /* read uid/resouce_id highest count from uid map file */ if (read(file,&resource_id, sizeof(resource_id)) != sizeof(resource_id)) { dbg("error setting uid from existing uid map file"); return(-1); } rval = build_uid_map_data(file); close(file); if (rval < 0) return(-1); /* return from successful initialization from existing uid map file */ return(0); }
/* * uid_map_from_file: called from oh_uid_initialize() during intialization * This function, if a uid map file exists, reads the current value for * uid and intializes the memory resident uid map file from file. * * Return value: success 0, error -1. */ static gint uid_map_from_file() { FILE *fp; int rval; #ifndef _WIN32 mode_t prev_umask; #endif if (!oh_uid_map_file) { return 0; } fp = fopen(oh_uid_map_file, "rb"); if(!fp) { /* create map file with resource id initial value */ WARN("uid_map file '%s' could not be opened, initializing", oh_uid_map_file); #ifndef _WIN32 prev_umask = umask(022); #endif fp = fopen(oh_uid_map_file, "wb"); if(!fp) { CRIT("Could not initialize uid map file, %s", oh_uid_map_file ); #ifndef _WIN32 if (geteuid() != 0) INFO("Use OPENHPI_UID_MAP env var to set uid_map file path"); umask (prev_umask); #endif return -1; } #ifndef _WIN32 umask (prev_umask); #endif /* write initial uid value */ if(fwrite(&resource_id, sizeof(resource_id), 1, fp) != 1 ) { CRIT("failed to write uid, on uid map file initialization"); fclose(fp); return -1; } if(fclose(fp) != 0) { CRIT("Couldn't close file '%s'.during uid map file initialization", oh_uid_map_file); return -1; } /* return from successful initialization, from newly created uid map file */ return 0; } /* read uid/resouce_id highest count from uid map file */ if (fread(&resource_id, sizeof(resource_id), 1, fp) != 1) { CRIT("error setting uid from existing uid map file"); fclose(fp); return -1; } rval = build_uid_map_data(fp); fclose(fp); if (rval < 0) return -1; /* return from successful initialization from existing uid map file */ return 0; }