+
+static int
+IPCS_msg_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 msqid_ds msqid_ds32; /* post conversion, 32 bit version */
+ void *msqid_dsp;
+ size_t ipcs_sz = sizeof(struct user_IPCS_command);
+ size_t msqid_ds_sz = sizeof(struct user_msqid_ds);
+ struct proc *p = current_proc();
+
+ if (!IS_64BIT_PROCESS(p)) {
+ ipcs_sz = sizeof(struct IPCS_command);
+ msqid_ds_sz = sizeof(struct msqid_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_MSG_SUBSYS_LOCK();
+
+ switch(ipcs.u64.ipcs_op) {
+ case IPCS_MSG_CONF: /* Obtain global configuration data */
+ if (ipcs.u64.ipcs_datalen != sizeof(struct msginfo)) {
+ error = ERANGE;
+ break;
+ }
+ if (ipcs.u64.ipcs_cursor != 0) { /* fwd. compat. */
+ error = EINVAL;
+ break;
+ }
+ SYSV_MSG_SUBSYS_UNLOCK();
+ error = copyout(&msginfo, ipcs.u64.ipcs_data, ipcs.u64.ipcs_datalen);
+ SYSV_MSG_SUBSYS_LOCK();
+ break;
+
+ case IPCS_MSG_ITER: /* Iterate over existing segments */
+ /* Not done up top so we can set limits via sysctl (later) */
+ msginit( 0);
+
+ cursor = ipcs.u64.ipcs_cursor;
+ if (cursor < 0 || cursor >= msginfo.msgmni) {
+ error = ERANGE;
+ break;
+ }
+ if (ipcs.u64.ipcs_datalen != (int)msqid_ds_sz) {
+ error = ENOMEM;
+ break;
+ }
+ for( ; cursor < msginfo.msgmni; cursor++) {
+ if (msqids[cursor].msg_qbytes != 0) /* allocated */
+ break;
+ continue;
+ }
+ if (cursor == msginfo.msgmni) {
+ error = ENOENT;
+ break;
+ }
+
+ msqid_dsp = &msqids[cursor]; /* default: 64 bit */
+
+ /*
+ * If necessary, convert the 64 bit kernel segment
+ * descriptor to a 32 bit user one.
+ */
+ if (!IS_64BIT_PROCESS(p)) {
+ msqid_ds_64to32(msqid_dsp, &msqid_ds32);
+ msqid_dsp = &msqid_ds32;
+ }
+ SYSV_MSG_SUBSYS_UNLOCK();
+ error = copyout(msqid_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);
+ }
+ SYSV_MSG_SUBSYS_LOCK();
+ break;
+
+ default:
+ error = EINVAL;
+ break;
+ }
+
+ SYSV_MSG_SUBSYS_UNLOCK();
+ return(error);
+}
+
+SYSCTL_DECL(_kern_sysv_ipcs);
+SYSCTL_PROC(_kern_sysv_ipcs, OID_AUTO, msg, CTLFLAG_RW|CTLFLAG_ANYBODY,
+ 0, 0, IPCS_msg_sysctl,
+ "S,IPCS_msg_command",
+ "ipcs msg command interface");