Exemplo n.º 1
0
/*!
 *  ======== DriverTable_match ======== 
 */
Int DriverTable_match(String name, IDriver_Handle *handle, Error_Block *eb)
{
    return (NameServer_match((NameServer_Handle)DriverTable_module->drvTable,
            name, (UInt32 *)handle));
}
Exemplo n.º 2
0
/*
 *  ======== 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 (&params);
        ret = copy_to_user (cargs.args.ParamsInit.params,
                            &params,
                            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(&params, 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, &params);

            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;
}
Exemplo n.º 3
0
/*
 *  ======== Stream_match ========    
 *  This is an internal function, If a match is found in in the Stream 
 *  IConverter table, it returns the rest of name and the entry. If a match 
 *  is not found, it returns the name and a null entry. This allows 
 *  Stream_createStack to look for name in the DriverTable.
 *
 */
Int Stream_match(String name, IConverter_Handle *handle, Error_Block *eb)
{
    return (NameServer_match((NameServer_Handle)Stream_module->convTable, name, 
        (UInt32 *)handle));
}