/* * ======== NameServerDrv_ioctl ======== * */ static long NameServerDrv_ioctl(struct file *filp, unsigned int cmd, unsigned long args) { int osStatus = 0; Int32 status = NameServer_S_SUCCESS; Int32 ret; NameServerDrv_CmdArgs cargs; Osal_Pid pid; GT_3trace(curTrace, GT_ENTER, "NameServerDrv_ioctl", filp, cmd, args); /* save the process id for resource tracking */ pid = pid_nr(filp->f_owner.pid); /* copy the full args from user space */ ret = copy_from_user(&cargs, (Ptr)args, sizeof(NameServerDrv_CmdArgs)); GT_assert(curTrace, (ret == 0)); switch (cmd) { case CMD_NAMESERVER_ADD: { NameServerDrv_Res * res = NULL; Ptr buf; /* allocate resource tracker object */ res = Memory_alloc(NULL, sizeof(NameServerDrv_Res), 0, NULL); if (res == NULL) { status = NameServer_E_MEMORY; GT_setFailureReason(curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "out of memory"); } /* allocate memory for the name */ if (status == NameServer_S_SUCCESS) { res->args.add.len = cargs.args.add.nameLen; res->args.add.name = Memory_alloc(NULL, cargs.args.add.nameLen, 0, NULL); if (res->args.add.name == NULL) { status = NameServer_E_MEMORY; GT_setFailureReason(curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "out of memory"); } } /* copy the name from user memory */ if (status == NameServer_S_SUCCESS) { status = copy_from_user(res->args.add.name, cargs.args.add.name, cargs.args.add.nameLen); if (status != 0) { GT_setFailureReason(curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "copy_from_user failed"); status = NameServer_E_OSFAILURE; osStatus = -EFAULT; } } /* allocate memory for the buf */ if (status == NameServer_S_SUCCESS) { buf = Memory_alloc(NULL, cargs.args.add.len, 0, NULL); if (buf == NULL) { status = NameServer_E_MEMORY; GT_setFailureReason(curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "out of memory"); } } /* copy the value from user buf */ if (status == NameServer_S_SUCCESS) { status = copy_from_user(buf, cargs.args.add.buf, cargs.args.add.len); if (status != 0) { GT_setFailureReason(curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "copy_from_user failed"); status = NameServer_E_OSFAILURE; osStatus = -EFAULT; } } /* invoke the module api */ if (status == NameServer_S_SUCCESS) { cargs.args.add.entry = NameServer_add(cargs.args.add.handle, res->args.add.name, buf, cargs.args.add.len); if (cargs.args.addUInt32.entry == NULL) { status = NameServer_E_FAIL; GT_setFailureReason(curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "NameServer_addUInt32 failed"); } } /* track the resource by process id */ if (status == NameServer_S_SUCCESS) { Int rstat; res->cmd = CMD_NAMESERVER_ADD; /* use this command for both */ res->args.add.handle = cargs.args.add.handle; res->args.add.entry = cargs.args.add.entry; rstat = ResTrack_push(NameServerDrv_state.resTrack, pid, (List_Elem *)res); GT_assert(curTrace, (rstat >= 0)); } /* don't need the buf memory anymore */ if (buf != NULL) { Memory_free(NULL, buf, cargs.args.add.len); } /* failure cleanup */ if (status < 0) { if ((res != NULL) && (res->args.add.name != NULL)) { Memory_free(NULL, res->args.add.name, cargs.args.add.nameLen); } if (res != NULL) { Memory_free(NULL, res, sizeof(NameServerDrv_Res)); } } } break; case CMD_NAMESERVER_ADDUINT32: { NameServerDrv_Res * res = NULL; /* allocate resource tracker object */ res = Memory_alloc(NULL, sizeof(NameServerDrv_Res), 0, NULL); if (res == NULL) { status = NameServer_E_MEMORY; GT_setFailureReason(curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "out of memory"); } /* allocate memory for the name */ if (status == NameServer_S_SUCCESS) { res->args.add.len = cargs.args.addUInt32.nameLen; res->args.add.name = Memory_alloc(NULL, cargs.args.addUInt32.nameLen, 0, NULL); if (res->args.add.name == NULL) { status = NameServer_E_MEMORY; GT_setFailureReason(curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "out of memory"); } } /* copy the name from user memory */ if (status == NameServer_S_SUCCESS) { status = copy_from_user(res->args.add.name, cargs.args.addUInt32.name, cargs.args.addUInt32.nameLen); if (status != 0) { GT_setFailureReason(curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "copy_from_user failed"); status = NameServer_E_OSFAILURE; osStatus = -EFAULT; } } /* invoke the module api */ if (status == NameServer_S_SUCCESS) { cargs.args.addUInt32.entry = NameServer_addUInt32( cargs.args.addUInt32.handle, res->args.add.name, cargs.args.addUInt32.value); if (cargs.args.addUInt32.entry == NULL) { status = NameServer_E_FAIL; GT_setFailureReason(curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "NameServer_addUInt32 failed"); } } /* track the resource by process id */ if (status == NameServer_S_SUCCESS) { Int rstat; res->cmd = CMD_NAMESERVER_ADD; /* use this command for both */ res->args.add.handle = cargs.args.addUInt32.handle; res->args.add.entry = cargs.args.addUInt32.entry; rstat = ResTrack_push(NameServerDrv_state.resTrack, pid, (List_Elem *)res); GT_assert(curTrace, (rstat >= 0)); } /* failure cleanup */ if (status < 0) { if ((res != NULL) && (res->args.add.name != NULL)) { Memory_free(NULL, res->args.add.name, cargs.args.addUInt32.nameLen); } if (res != NULL) { Memory_free(NULL, res, sizeof(NameServerDrv_Res)); } } } break; case CMD_NAMESERVER_GET: { String name; Ptr value; UInt16 * procId = NULL; /* Allocate memory for the name */ name = Memory_alloc (NULL, cargs.args.get.nameLen, 0, NULL); GT_assert (curTrace, (name != NULL)); /* Copy the name */ ret = copy_from_user (name, cargs.args.get.name, cargs.args.get.nameLen); GT_assert (curTrace, (ret == 0)); /* Allocate memory for the buf */ value = Memory_alloc (NULL, cargs.args.get.len, 0, NULL); GT_assert (curTrace, (value != NULL)); /* Allocate memory for the procId */ if (cargs.args.get.procLen > 0) { procId = Memory_alloc (NULL, cargs.args.get.procLen * sizeof(UInt16), 0, NULL); GT_assert (curTrace, (procId != NULL)); } /* Copy the procIds */ ret = copy_from_user (procId, cargs.args.get.procId, cargs.args.get.procLen); GT_assert (curTrace, (ret == 0)); status = NameServer_get (cargs.args.get.handle, name, value, &cargs.args.get.len, procId); /* Do not assert. This can return NameServer_E_NOTFOUND * as a valid runtime failure. */ /* Copy the value */ ret = copy_to_user (cargs.args.get.value, value, cargs.args.get.len); GT_assert (curTrace, (ret == 0)); /* free the allocated memory */ Memory_free (NULL, name, cargs.args.get.nameLen); Memory_free (NULL, value, cargs.args.get.len); if (procId != NULL) { Memory_free (NULL, procId, cargs.args.get.procLen *sizeof(UInt16)); } } break; case CMD_NAMESERVER_GETLOCAL: { String name; Ptr value; /* Allocate memory for the name */ name = Memory_alloc (NULL, cargs.args.getLocal.nameLen, 0, NULL); GT_assert (curTrace, (name != NULL)); /* Copy the name */ ret = copy_from_user (name, cargs.args.getLocal.name, cargs.args.getLocal.nameLen); GT_assert (curTrace, (ret == 0)); /* Allocate memory for the buf */ value = Memory_alloc (NULL, cargs.args.getLocal.len, 0, NULL); GT_assert (curTrace, (value != NULL)); status = NameServer_getLocal (cargs.args.getLocal.handle, name, value, &cargs.args.getLocal.len); GT_assert (curTrace, (status >= 0)); /* Copy the value */ ret = copy_to_user (cargs.args.getLocal.value, value, cargs.args.getLocal.len); GT_assert (curTrace, (ret == 0)); /* free the allocated memory */ Memory_free (NULL, name, cargs.args.getLocal.nameLen); Memory_free (NULL, value, cargs.args.getLocal.len); } break; case CMD_NAMESERVER_MATCH: { String name; /* Allocate memory for the name */ name = Memory_alloc (NULL, cargs.args.match.nameLen, 0, NULL); GT_assert (curTrace, (name != NULL)); /* Copy the name */ ret = copy_from_user (name, cargs.args.match.name, cargs.args.match.nameLen); GT_assert (curTrace, (ret == 0)); cargs.args.match.count = NameServer_match ( cargs.args.match.handle, name, &cargs.args.match.value); GT_assert (curTrace, (cargs.args.match.count >= 0)); /* free the allocated memory */ Memory_free (NULL, name, cargs.args.match.nameLen); } break; case CMD_NAMESERVER_REMOVE: { NameServerDrv_Res res; List_Elem * elem; /* save for resource untracking */ res.cmd = CMD_NAMESERVER_ADD; res.args.add.entry = NULL; /* allocate memory for the name */ res.args.add.len = cargs.args.remove.nameLen; res.args.add.name = Memory_alloc(NULL, cargs.args.remove.nameLen, 0, NULL); if (res.args.add.name == NULL) { status = NameServer_E_MEMORY; GT_setFailureReason(curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "out of memory"); } /* copy the name from user memory */ if (status == NameServer_S_SUCCESS) { status = copy_from_user(res.args.add.name, cargs.args.remove.name, cargs.args.remove.nameLen); if (status != 0) { GT_setFailureReason(curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "copy_from_user failed"); status = NameServer_E_OSFAILURE; osStatus = -EFAULT; } } /* invoke the module api */ if (status == NameServer_S_SUCCESS) { status = NameServer_remove(cargs.args.remove.handle, res.args.add.name); if (status < 0) { GT_setFailureReason(curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "NameServer_remove failed"); } } /* untrack the resource */ if (status == NameServer_S_SUCCESS) { NameServerDrv_Res * resPtr; ResTrack_remove(NameServerDrv_state.resTrack, pid, (List_Elem *)(&res), NameServerDrv_resCmpFxn, &elem); GT_assert(curTrace, (elem != NULL)); resPtr = (NameServerDrv_Res *)elem; Memory_free(NULL, resPtr->args.add.name, resPtr->args.add.len); Memory_free(NULL, elem, sizeof(NameServerDrv_Res)); } /* don't need the name memory anymore */ if (res.args.add.name != NULL) { Memory_free(NULL, res.args.add.name, cargs.args.remove.nameLen); } } break; case CMD_NAMESERVER_REMOVEENTRY: { NameServerDrv_Res res; List_Elem * elem; /* save for resource untracking */ res.cmd = CMD_NAMESERVER_ADD; res.args.add.name = NULL; res.args.add.entry = cargs.args.removeEntry.entry; /* invoke the module api */ status = NameServer_removeEntry(cargs.args.removeEntry.handle, cargs.args.removeEntry.entry); if (status < 0) { GT_setFailureReason(curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "NameServer_remove failed"); } /* untrack the resource */ if (status == NameServer_S_SUCCESS) { NameServerDrv_Res * resPtr; ResTrack_remove(NameServerDrv_state.resTrack, pid, (List_Elem *)(&res), NameServerDrv_resCmpFxn, &elem); GT_assert(curTrace, (elem != NULL)); resPtr = (NameServerDrv_Res *)elem; Memory_free(NULL, resPtr->args.add.name, resPtr->args.add.len); Memory_free(NULL, elem, sizeof(NameServerDrv_Res)); } } break; case CMD_NAMESERVER_PARAMS_INIT: { NameServer_Params params; NameServer_Params_init (¶ms); ret = copy_to_user (cargs.args.ParamsInit.params, ¶ms, sizeof (NameServer_Params)); GT_assert (curTrace, (ret == 0)); } break; case CMD_NAMESERVER_CREATE: { NameServer_Params params; String name = NULL; NameServerDrv_Res * res = NULL; /* allocate memory for the name */ name = Memory_alloc(NULL, cargs.args.create.nameLen, 0, NULL); if (name == NULL) { status = NameServer_E_MEMORY; GT_setFailureReason(curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "out of memory"); } /* copy the name from user memory */ if (status == NameServer_S_SUCCESS) { status = copy_from_user(name, cargs.args.create.name, cargs.args.create.nameLen); if (status != 0) { GT_setFailureReason(curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "copy_from_user failed"); status = NameServer_E_OSFAILURE; osStatus = -EFAULT; } } /* copy the params from user memory */ if (status == NameServer_S_SUCCESS) { status = copy_from_user(¶ms, cargs.args.create.params, sizeof(NameServer_Params)); if (status != 0) { GT_setFailureReason(curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "copy_from_user failed"); status = NameServer_E_OSFAILURE; osStatus = -EFAULT; } } /* allocate resource tracker object */ if (status == NameServer_S_SUCCESS) { res = Memory_alloc(NULL, sizeof(NameServerDrv_Res), 0, NULL); if (res == NULL) { status = NameServer_E_MEMORY; GT_setFailureReason(curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "out of memory"); } } /* invoke the module api */ if (status == NameServer_S_SUCCESS) { cargs.args.create.handle = NameServer_create(name, ¶ms); if (cargs.args.create.handle == NULL) { status = NameServer_E_FAIL; GT_setFailureReason(curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "NameServer_create failed"); } } /* track the resource by process id */ if (status == NameServer_S_SUCCESS) { Int rstat; res->cmd = cmd; res->args.create.handle = cargs.args.create.handle; rstat = ResTrack_push(NameServerDrv_state.resTrack, pid, (List_Elem *)res); GT_assert(curTrace, (rstat >= 0)); } /* don't need name memory anymore */ if (name != NULL) { Memory_free(NULL, name, cargs.args.create.nameLen); } /* failure cleanup */ if (status < 0) { if (res != NULL) { Memory_free(NULL, res, sizeof(NameServerDrv_Res)); } } } break; case CMD_NAMESERVER_DELETE: { NameServerDrv_Res res; List_Elem * elem; /* save for resource untracking, handle set to null by delete */ res.cmd = CMD_NAMESERVER_CREATE; res.args.create.handle = cargs.args.delete.handle; /* common code for delete command */ status = NameServerDrv_cmd_delete(&cargs.args.delete.handle); /* untrack the resource */ if (status == NameServer_S_SUCCESS) { ResTrack_remove(NameServerDrv_state.resTrack, pid, (List_Elem *)(&res), NameServerDrv_resCmpFxn, &elem); GT_assert(curTrace, (elem != NULL)); Memory_free(NULL, elem, sizeof(NameServerDrv_Res)); } } break; case CMD_NAMESERVER_GETHANDLE: { String name; /* Allocate memory for the name */ name = Memory_alloc (NULL, cargs.args.getHandle.nameLen, 0, NULL); GT_assert (curTrace, (name != NULL)); /* Copy the name */ ret = copy_from_user (name, cargs.args.getHandle.name, cargs.args.getHandle.nameLen); GT_assert (curTrace, (ret == 0)); cargs.args.getHandle.handle = NameServer_getHandle (name); /* free the allocated memory */ Memory_free (NULL, name, cargs.args.getHandle.nameLen); } break; case CMD_NAMESERVER_SETUP: { /* register process with resource tracker */ status = ResTrack_register(NameServerDrv_state.resTrack, pid); if (status < 0) { GT_setFailureReason(curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "resource tracker register failed"); status = NameServer_E_FAIL; pid = 0; } /* setup the module */ if (status == NameServer_S_SUCCESS) { status = NameServer_setup(); if (status < 0) { GT_setFailureReason(curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "kernel-side MessageQ_setup failed"); } } /* failure case */ if (status < 0) { if (pid != 0) { ResTrack_unregister(NameServerDrv_state.resTrack, pid); } } } break; case CMD_NAMESERVER_DESTROY: { /* unregister process from resource tracker */ status = ResTrack_unregister(NameServerDrv_state.resTrack, pid); GT_assert(curTrace, (status >= 0)); /* finalize the module */ status = NameServer_destroy(); GT_assert(curTrace, (status >= 0)); } break; default: { /* This does not impact return status of this function, so retVal * comment is not used. */ status = NameServer_E_INVALIDARG; GT_setFailureReason (curTrace, GT_4CLASS, "NameServerDrv_ioctl", status, "Unsupported ioctl command specified"); } break; } cargs.apiStatus = status; /* copy the full args back to user space */ ret = copy_to_user((Ptr)args, &cargs, sizeof(NameServerDrv_CmdArgs)); GT_assert (curTrace, (ret == 0)); GT_1trace (curTrace, GT_LEAVE, "NameServerDrv_ioctl", osStatus); /*! @retval 0 Operation successfully completed. */ return osStatus; }
/* * ======== NameServerMessageQ_swiFxn ======== */ Void NameServerMessageQ_swiFxn(UArg arg0, UArg arg1) { NameServerMsg *msg; NameServer_Handle handle; MessageQ_QueueId queueId; Int status = NameServer_E_FAIL; Semaphore_Handle semRemoteWait = NameServerMessageQ_module->semRemoteWait; /* drain all messages in the messageQ */ while (1) { /* get a message, this never waits */ status = MessageQ_get( (MessageQ_Handle)NameServerMessageQ_module->msgHandle, (MessageQ_Msg *)&msg, 0); /* if no message then return */ if (status != MessageQ_S_SUCCESS) { break; } if (msg->request == NameServerMessageQ_REQUEST) { /* reset value of status */ status = NameServer_E_FAIL; /* * Message is a request. Lookup name in NameServer table. * Send a response message back to source processor. */ handle = NameServer_getHandle((String)msg->instanceName); if (handle != NULL) { /* Search for the NameServer entry */ status = NameServer_getLocalUInt32(handle, (String)msg->name, &msg->value); } /* set the request status */ if (status < 0) { msg->requestStatus = 0; } else { msg->requestStatus = 1; } /* specify message as a response */ msg->request = NameServerMessageQ_RESPONSE; /* get the remote processor from the msg header */ queueId = (UInt32)(msg->header.replyProc) << 16; /* send response message to remote processor */ MessageQ_put(queueId, (MessageQ_Msg)msg); } else { /* * This is a response message. At any given time, there is * only one of these outstanding because of semMultiBlock. * This allows us to safely set the Module state's msg pointer * and post semaphore. */ NameServerMessageQ_module->msg = msg; Semaphore_post(semRemoteWait); } } }