+static int
+IPCS_sem_sysctl(__unused struct sysctl_oid *oidp, __unused void *arg1,
+ __unused int arg2, struct sysctl_req *req)
+{
+ int error;
+ int cursor;
+ union {
+ struct user32_IPCS_command u32;
+ struct user_IPCS_command u64;
+ } ipcs;
+ struct user32_semid_ds semid_ds32; /* post conversion, 32 bit version */
+ struct user64_semid_ds semid_ds64; /* post conversion, 64 bit version */
+ void *semid_dsp;
+ size_t ipcs_sz;
+ size_t semid_ds_sz;
+ struct proc *p = current_proc();
+
+ if (IS_64BIT_PROCESS(p)) {
+ ipcs_sz = sizeof(struct user_IPCS_command);
+ semid_ds_sz = sizeof(struct user64_semid_ds);
+ } else {
+ ipcs_sz = sizeof(struct user32_IPCS_command);
+ semid_ds_sz = sizeof(struct user32_semid_ds);
+ }
+
+ /* Copy in the command structure */
+ if ((error = SYSCTL_IN(req, &ipcs, ipcs_sz)) != 0) {
+ return(error);
+ }
+
+ 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) {
+ return(EINVAL);
+ }
+
+ SYSV_SEM_SUBSYS_LOCK();
+ switch(ipcs.u64.ipcs_op) {
+ case IPCS_SEM_CONF: /* Obtain global configuration data */
+ if (ipcs.u64.ipcs_datalen != sizeof(struct seminfo)) {
+ error = ERANGE;
+ break;
+ }
+ if (ipcs.u64.ipcs_cursor != 0) { /* fwd. compat. */
+ error = EINVAL;
+ break;
+ }
+ error = copyout(&seminfo, ipcs.u64.ipcs_data, ipcs.u64.ipcs_datalen);
+ break;
+
+ case IPCS_SEM_ITER: /* Iterate over existing segments */
+ cursor = ipcs.u64.ipcs_cursor;
+ if (cursor < 0 || cursor >= seminfo.semmni) {
+ error = ERANGE;
+ break;
+ }
+ if (ipcs.u64.ipcs_datalen != (int)semid_ds_sz ) {
+ error = EINVAL;
+ break;
+ }
+ for( ; cursor < seminfo.semmni; cursor++) {
+ if (sema[cursor].u.sem_perm.mode & SEM_ALLOC)
+ break;
+ continue;
+ }
+ if (cursor == seminfo.semmni) {
+ error = ENOENT;
+ break;
+ }
+
+ semid_dsp = &sema[cursor].u; /* default: 64 bit */
+
+ /*
+ * If necessary, convert the 64 bit kernel segment
+ * descriptor to a 32 bit user one.
+ */
+ if (!IS_64BIT_PROCESS(p)) {
+ bzero(&semid_ds32, sizeof(semid_ds32));
+ semid_ds_kernelto32(semid_dsp, &semid_ds32);
+ semid_dsp = &semid_ds32;
+ } else {
+ bzero(&semid_ds64, sizeof(semid_ds64));
+ semid_ds_kernelto64(semid_dsp, &semid_ds64);
+ semid_dsp = &semid_ds64;
+ }
+
+ error = copyout(semid_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_EXPLICIT(user32_addr_t,ipcs.u64.ipcs_data);
+
+ error = SYSCTL_OUT(req, &ipcs, ipcs_sz);
+ }
+ break;
+
+ default:
+ error = EINVAL;
+ break;
+ }
+ SYSV_SEM_SUBSYS_UNLOCK();
+ return(error);
+}
+
+SYSCTL_DECL(_kern_sysv_ipcs);
+SYSCTL_PROC(_kern_sysv_ipcs, OID_AUTO, sem, CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LOCKED,
+ 0, 0, IPCS_sem_sysctl,
+ "S,IPCS_sem_command",
+ "ipcs sem command interface");
+
+#endif /* SYSV_SEM */