+ sysctl_shminfo_ret = 0;
+sysctl_shminfo_out:
+ SYSV_SHM_SUBSYS_UNLOCK();
+ return sysctl_shminfo_ret;
+}
+
+static int
+IPCS_shm_sysctl(__unused struct sysctl_oid *oidp, __unused void *arg1,
+ __unused int arg2, struct sysctl_req *req)
+{
+ int error;
+ int cursor;
+ union {
+ struct IPCS_command u32;
+ struct user_IPCS_command u64;
+ } ipcs;
+ struct shmid_ds shmid_ds32; /* post conversion, 32 bit version */
+ void *shmid_dsp;
+ size_t ipcs_sz = sizeof(struct user_IPCS_command);
+ size_t shmid_ds_sz = sizeof(struct user_shmid_ds);
+ struct proc *p = current_proc();
+
+ int ipcs__shminfo_ret = 0;
+
+ SYSV_SHM_SUBSYS_LOCK();
+
+ if (!shm_inited) {
+ error = EINVAL;
+ goto ipcs_shm_sysctl_out;
+ }
+
+ if (!IS_64BIT_PROCESS(p)) {
+ ipcs_sz = sizeof(struct IPCS_command);
+ shmid_ds_sz = sizeof(struct shmid_ds);
+ }
+
+ /* Copy in the command structure */
+ if ((error = SYSCTL_IN(req, &ipcs, ipcs_sz)) != 0) {
+ goto ipcs_shm_sysctl_out;
+ }
+
+ if (!IS_64BIT_PROCESS(p)) /* convert in place */
+ ipcs.u64.ipcs_data = CAST_USER_ADDR_T(ipcs.u32.ipcs_data);
+
+ /* Let us version this interface... */
+ if (ipcs.u64.ipcs_magic != IPCS_MAGIC) {
+ error = EINVAL;
+ goto ipcs_shm_sysctl_out;
+ }
+
+ switch(ipcs.u64.ipcs_op) {
+ case IPCS_SHM_CONF: /* Obtain global configuration data */
+ if (ipcs.u64.ipcs_datalen != sizeof(struct shminfo)) {
+ if (ipcs.u64.ipcs_cursor != 0) { /* fwd. compat. */
+ error = ENOMEM;
+ break;
+ }
+ error = ERANGE;
+ break;
+ }
+ error = copyout(&shminfo, ipcs.u64.ipcs_data, ipcs.u64.ipcs_datalen);
+ break;
+
+ case IPCS_SHM_ITER: /* Iterate over existing segments */
+ cursor = ipcs.u64.ipcs_cursor;
+ if (cursor < 0 || cursor >= shminfo.shmmni) {
+ error = ERANGE;
+ break;
+ }
+ if (ipcs.u64.ipcs_datalen != (int)shmid_ds_sz) {
+ error = ENOMEM;
+ break;
+ }
+ for( ; cursor < shminfo.shmmni; cursor++) {
+ if (shmsegs[cursor].shm_perm.mode & SHMSEG_ALLOCATED)
+ break;
+ continue;
+ }
+ if (cursor == shminfo.shmmni) {
+ error = ENOENT;
+ break;
+ }
+
+ shmid_dsp = &shmsegs[cursor]; /* default: 64 bit */
+
+ /*
+ * If necessary, convert the 64 bit kernel segment
+ * descriptor to a 32 bit user one.
+ */
+ if (!IS_64BIT_PROCESS(p)) {
+ shmid_ds_64to32(shmid_dsp, &shmid_ds32);
+ shmid_dsp = &shmid_ds32;
+ }
+ error = copyout(shmid_dsp, ipcs.u64.ipcs_data, ipcs.u64.ipcs_datalen);
+ if (!error) {
+ /* update cursor */
+ ipcs.u64.ipcs_cursor = cursor + 1;
+
+ if (!IS_64BIT_PROCESS(p)) /* convert in place */
+ ipcs.u32.ipcs_data = CAST_DOWN(void *,ipcs.u64.ipcs_data);
+ error = SYSCTL_OUT(req, &ipcs, ipcs_sz);
+ }
+ break;
+
+ default:
+ error = EINVAL;
+ break;
+ }
+ipcs_shm_sysctl_out:
+ SYSV_SHM_SUBSYS_UNLOCK();
+ return(error);