main( int argc, // Number of Arguments char **argv ) // Pointer to Arguments { int shrd_param_id; // Shared Memory ID struct SHM_PARAM *param_ptr; // Pointer to the Shared Param /* -------------------------------------------- ALLOC SHARED MEMORY */ /*-------- SHARED PARAMETERS --------*/ shm_access( SHM_PARAM_KEY, // ACCESS KEY sizeof(struct SHM_PARAM), // SIZE OF SHM &shrd_param_id, // SHM ID ¶m_ptr); // Pointer to the SHM /* -------------------------------------------- WAIT UNTIL FINISH */ /*-------- Immediate FINISH --------*/ if( argc >= 2){ param_ptr->validity ^= atoi(argv[1]); // Toggle Flag } else { param_ptr->validity |= FINISH; } printf("SET VALIDITY %X\n", param_ptr->validity); /* -------------------------------------------- RELEASE the SHM */ return(0); }
static int shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred) { struct shm_mapping *map; int error; LIST_FOREACH(map, SHM_HASH(fnv), sm_link) { if (map->sm_fnv != fnv) continue; if (strcmp(map->sm_path, path) == 0) { #ifdef MAC error = mac_posixshm_check_unlink(ucred, map->sm_shmfd); if (error) return (error); #endif error = shm_access(map->sm_shmfd, ucred, FREAD | FWRITE); if (error) return (error); map->sm_shmfd->shm_path = NULL; LIST_REMOVE(map, sm_link); shm_drop(map->sm_shmfd); free(map->sm_path, M_SHMFD); free(map, M_SHMFD); return (0); } } return (ENOENT); }
static int ProcShmAttach(ClientPtr client) { SHMSTAT_TYPE buf; ShmDescPtr shmdesc; REQUEST(xShmAttachReq); REQUEST_SIZE_MATCH(xShmAttachReq); LEGAL_NEW_RESOURCE(stuff->shmseg, client); if ((stuff->readOnly != xTrue) && (stuff->readOnly != xFalse)) { client->errorValue = stuff->readOnly; return BadValue; } for (shmdesc = Shmsegs; shmdesc && (shmdesc->shmid != stuff->shmid); shmdesc = shmdesc->next) ; if (shmdesc) { if (!stuff->readOnly && !shmdesc->writable) return BadAccess; shmdesc->refcnt++; } else { shmdesc = malloc(sizeof(ShmDescRec)); if (!shmdesc) return BadAlloc; shmdesc->addr = shmat(stuff->shmid, 0, stuff->readOnly ? SHM_RDONLY : 0); if ((shmdesc->addr == ((char *)-1)) || SHMSTAT(stuff->shmid, &buf)) { free(shmdesc); return BadAccess; } /* The attach was performed with root privs. We must * do manual checking of access rights for the credentials * of the client */ if (shm_access(client, &(SHM_PERM(buf)), stuff->readOnly) == -1) { shmdt(shmdesc->addr); free(shmdesc); return BadAccess; } shmdesc->shmid = stuff->shmid; shmdesc->refcnt = 1; shmdesc->writable = !stuff->readOnly; shmdesc->size = SHM_SEGSZ(buf); shmdesc->next = Shmsegs; Shmsegs = shmdesc; } if (!AddResource(stuff->shmseg, ShmSegType, (pointer)shmdesc)) return BadAlloc; return Success; }
main( int argc, // Number of Arguments char **argv ) // Pointer to Arguments { int shrd_param_id; // Shared Memory ID struct SHM_PARAM *param_ptr; // Pointer to the Shared Param struct sembuf sops; // Semaphore for data area int IFindex; int index; float bitPower[MAX_NIF][POWER_TIME_NUM]; // Power Monitor Data float tempPower[POWER_TIME_NUM]; // Buffer char pg_text[256]; // Text to plot char xlabel[64]; // X-axis label //------------------------------------------ Access to the SHARED MEMORY //------- SHARED PARAMETERS -------- if(shm_access( SHM_PARAM_KEY, // ACCESS KEY sizeof(struct SHM_PARAM), // SIZE OF SHM &shrd_param_id, // SHM ID ¶m_ptr) != -1){ // Pointer to the SHM printf("PowerView: Succeeded to access the shared parameter [%d]!\n", param_ptr->shrd_param_id); } memset(bitPower, 0, param_ptr->num_st* POWER_TIME_NUM* sizeof(float)); //------------------------------------------ K5 Header and Data setvbuf(stdout, (char *)NULL, _IONBF, 0); // Disable stdout cache cpgbeg(1, argv[1], 1, 1); while(param_ptr->validity & ACTIVE){ cpgbbuf(); sprintf(xlabel, "Elapsed Time [sec]\0"); cpg_setup(xlabel); if( param_ptr->validity & (FINISH + ABSFIN) ){ break; } //-------- Wait for Semaphore sops.sem_num = (ushort)SEM_POWER; sops.sem_op = (short)-1; sops.sem_flg = (short)0; semop( param_ptr->sem_data_id, &sops, 1); //-------- Plot Power Monitor for(IFindex=0; IFindex<param_ptr->num_st; IFindex++){ memcpy(tempPower, bitPower[IFindex], POWER_TIME_NUM*sizeof(float)); bitPower[IFindex][0] = 10.0* log10(param_ptr->power[IFindex]); memcpy(&bitPower[IFindex][1], tempPower, (POWER_TIME_NUM-1)*sizeof(float)); } cpg_power(param_ptr, bitPower); } cpgend(); //------------------------------------------ RELEASE the SHM return(0); }
/* System calls. */ int sys_shm_open(struct thread *td, struct shm_open_args *uap) { struct filedesc *fdp; struct shmfd *shmfd; struct file *fp; char *path; Fnv32_t fnv; mode_t cmode; int fd, error; #ifdef CAPABILITY_MODE /* * shm_open(2) is only allowed for anonymous objects. */ if (IN_CAPABILITY_MODE(td) && (uap->path != SHM_ANON)) return (ECAPMODE); #endif if ((uap->flags & O_ACCMODE) != O_RDONLY && (uap->flags & O_ACCMODE) != O_RDWR) return (EINVAL); if ((uap->flags & ~(O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC)) != 0) return (EINVAL); fdp = td->td_proc->p_fd; cmode = (uap->mode & ~fdp->fd_cmask) & ACCESSPERMS; error = falloc(td, &fp, &fd, 0); if (error) return (error); /* A SHM_ANON path pointer creates an anonymous object. */ if (uap->path == SHM_ANON) { /* A read-only anonymous object is pointless. */ if ((uap->flags & O_ACCMODE) == O_RDONLY) { fdclose(fdp, fp, fd, td); fdrop(fp, td); return (EINVAL); } shmfd = shm_alloc(td->td_ucred, cmode); } else { path = malloc(MAXPATHLEN, M_SHMFD, M_WAITOK); error = copyinstr(uap->path, path, MAXPATHLEN, NULL); /* Require paths to start with a '/' character. */ if (error == 0 && path[0] != '/') error = EINVAL; if (error) { fdclose(fdp, fp, fd, td); fdrop(fp, td); free(path, M_SHMFD); return (error); } fnv = fnv_32_str(path, FNV1_32_INIT); sx_xlock(&shm_dict_lock); shmfd = shm_lookup(path, fnv); if (shmfd == NULL) { /* Object does not yet exist, create it if requested. */ if (uap->flags & O_CREAT) { #ifdef MAC error = mac_posixshm_check_create(td->td_ucred, path); if (error == 0) { #endif shmfd = shm_alloc(td->td_ucred, cmode); shm_insert(path, fnv, shmfd); #ifdef MAC } #endif } else { free(path, M_SHMFD); error = ENOENT; } } else { /* * Object already exists, obtain a new * reference if requested and permitted. */ free(path, M_SHMFD); if ((uap->flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) error = EEXIST; else { #ifdef MAC error = mac_posixshm_check_open(td->td_ucred, shmfd, FFLAGS(uap->flags & O_ACCMODE)); if (error == 0) #endif error = shm_access(shmfd, td->td_ucred, FFLAGS(uap->flags & O_ACCMODE)); } /* * Truncate the file back to zero length if * O_TRUNC was specified and the object was * opened with read/write. */ if (error == 0 && (uap->flags & (O_ACCMODE | O_TRUNC)) == (O_RDWR | O_TRUNC)) { #ifdef MAC error = mac_posixshm_check_truncate( td->td_ucred, fp->f_cred, shmfd); if (error == 0) #endif shm_dotruncate(shmfd, 0); } if (error == 0) shm_hold(shmfd); } sx_xunlock(&shm_dict_lock); if (error) { fdclose(fdp, fp, fd, td); fdrop(fp, td); return (error); } } finit(fp, FFLAGS(uap->flags & O_ACCMODE), DTYPE_SHM, shmfd, &shm_ops); FILEDESC_XLOCK(fdp); if (fdp->fd_ofiles[fd] == fp) fdp->fd_ofileflags[fd] |= UF_EXCLOSE; FILEDESC_XUNLOCK(fdp); td->td_retval[0] = fd; fdrop(fp, td); return (0); }