C++ (Cpp) mutex_lock Examples

C++ (Cpp) mutex_lock - 30 examples found. These are the top rated real world C++ (Cpp) examples of mutex_lock extracted from open source projects. You can rate examples to help us improve the quality of examples.
/**
 * ecryptfs_send_miscdev
 * @data: Data to send to daemon; may be NULL
 * @data_size: Amount of data to send to daemon
 * @msg_ctx: Message context, which is used to handle the reply. If
 *           this is NULL, then we do not expect a reply.
 * @msg_type: Type of message
 * @msg_flags: Flags for message
 * @daemon: eCryptfs daemon object
 *
 * Add msg_ctx to queue and then, if it exists, notify the blocked
 * miscdevess about the data being available. Must be called with
 * ecryptfs_daemon_hash_mux held.
 *
 * Returns zero on success; non-zero otherwise
 */
int ecryptfs_send_miscdev(char *data, size_t data_size,
                          struct ecryptfs_msg_ctx *msg_ctx, u8 msg_type,
                          u16 msg_flags, struct ecryptfs_daemon *daemon)
{
    struct ecryptfs_message *msg;

    msg = kmalloc((sizeof(*msg) + data_size), GFP_KERNEL);
    if (!msg) {
        printk(KERN_ERR "%s: Out of memory whilst attempting "
               "to kmalloc(%zd, GFP_KERNEL)\n", __func__,
               (sizeof(*msg) + data_size));
        return -ENOMEM;
    }

    mutex_lock(&msg_ctx->mux);
    msg_ctx->msg = msg;
    msg_ctx->msg->index = msg_ctx->index;
    msg_ctx->msg->data_len = data_size;
    msg_ctx->type = msg_type;
    memcpy(msg_ctx->msg->data, data, data_size);
    msg_ctx->msg_size = (sizeof(*msg_ctx->msg) + data_size);
    list_add_tail(&msg_ctx->daemon_out_list, &daemon->msg_ctx_out_queue);
    mutex_unlock(&msg_ctx->mux);

    mutex_lock(&daemon->mux);
    daemon->num_queued_msg_ctx++;
    wake_up_interruptible(&daemon->wait);
    mutex_unlock(&daemon->mux);

    return 0;
}
/**
 * ecryptfs_miscdev_release
 * @inode: inode of fs/ecryptfs/euid handle (ignored)
 * @file: file for fs/ecryptfs/euid handle (ignored)
 *
 * This keeps the daemon registered until the daemon sends another
 * ioctl to fs/ecryptfs/ctl or until the kernel module unregisters.
 *
 * Returns zero on success; non-zero otherwise
 */
static int
ecryptfs_miscdev_release(struct inode *inode, struct file *file)
{
    struct ecryptfs_daemon *daemon = NULL;
    uid_t euid = current_euid();
    int rc;

    mutex_lock(&ecryptfs_daemon_hash_mux);
    rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
    if (rc || !daemon)
        daemon = file->private_data;
    mutex_lock(&daemon->mux);
    BUG_ON(!(daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN));
    daemon->flags &= ~ECRYPTFS_DAEMON_MISCDEV_OPEN;
    atomic_dec(&ecryptfs_num_miscdev_opens);
    mutex_unlock(&daemon->mux);
    rc = ecryptfs_exorcise_daemon(daemon);
    if (rc) {
        printk(KERN_CRIT "%s: Fatal error whilst attempting to "
               "shut down daemon; rc = [%d]. Please report this "
               "bug.\n", __func__, rc);
        BUG();
    }
    module_put(THIS_MODULE);
    mutex_unlock(&ecryptfs_daemon_hash_mux);
    return rc;
}
Example #3
0
/**
 * ecryptfs_lookup
 * @ecryptfs_dir_inode: The eCryptfs directory inode
 * @ecryptfs_dentry: The eCryptfs dentry that we are looking up
 * @ecryptfs_nd: nameidata; may be NULL
 *
 * Find a file on disk. If the file does not exist, then we'll add it to the
 * dentry cache and continue on to read it from the disk.
 */
static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
                                      struct dentry *ecryptfs_dentry,
                                      unsigned int flags)
{
    char *encrypted_and_encoded_name = NULL;
    size_t encrypted_and_encoded_name_size;
    struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
    struct dentry *lower_dir_dentry, *lower_dentry;
    int rc = 0;

    lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent);
    mutex_lock(&lower_dir_dentry->d_inode->i_mutex);
    lower_dentry = lookup_one_len(ecryptfs_dentry->d_name.name,
                                  lower_dir_dentry,
                                  ecryptfs_dentry->d_name.len);
    mutex_unlock(&lower_dir_dentry->d_inode->i_mutex);
    if (IS_ERR(lower_dentry)) {
        rc = PTR_ERR(lower_dentry);
        ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
                        "[%d] on lower_dentry = [%s]\n", __func__, rc,
                        ecryptfs_dentry->d_name.name);
        goto out;
    }
    if (lower_dentry->d_inode)
        goto interpose;
    mount_crypt_stat = &ecryptfs_superblock_to_private(
                           ecryptfs_dentry->d_sb)->mount_crypt_stat;
    if (!(mount_crypt_stat
            && (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)))
        goto interpose;
    dput(lower_dentry);
    rc = ecryptfs_encrypt_and_encode_filename(
             &encrypted_and_encoded_name, &encrypted_and_encoded_name_size,
             NULL, mount_crypt_stat, ecryptfs_dentry->d_name.name,
             ecryptfs_dentry->d_name.len);
    if (rc) {
        printk(KERN_ERR "%s: Error attempting to encrypt and encode "
               "filename; rc = [%d]\n", __func__, rc);
        goto out;
    }
    mutex_lock(&lower_dir_dentry->d_inode->i_mutex);
    lower_dentry = lookup_one_len(encrypted_and_encoded_name,
                                  lower_dir_dentry,
                                  encrypted_and_encoded_name_size);
    mutex_unlock(&lower_dir_dentry->d_inode->i_mutex);
    if (IS_ERR(lower_dentry)) {
        rc = PTR_ERR(lower_dentry);
        ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
                        "[%d] on lower_dentry = [%s]\n", __func__, rc,
                        encrypted_and_encoded_name);
        goto out;
    }
interpose:
    rc = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry,
                                   ecryptfs_dir_inode);
out:
    kfree(encrypted_and_encoded_name);
    return ERR_PTR(rc);
}
Example #4
0
static ssize_t hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
{
    struct hidraw_list *list = file->private_data;
    int ret = 0, len;
    DECLARE_WAITQUEUE(wait, current);

    mutex_lock(&list->read_mutex);

    while (ret == 0) {
        if (list->head == list->tail) {
            add_wait_queue(&list->hidraw->wait, &wait);
            set_current_state(TASK_INTERRUPTIBLE);

            while (list->head == list->tail) {
                if (file->f_flags & O_NONBLOCK) {
                    ret = -EAGAIN;
                    break;
                }
                if (signal_pending(current)) {
                    ret = -ERESTARTSYS;
                    break;
                }
                if (!list->hidraw->exist) {
                    ret = -EIO;
                    break;
                }

                /* allow O_NONBLOCK to work well from other threads */
                mutex_unlock(&list->read_mutex);
                schedule();
                mutex_lock(&list->read_mutex);
                set_current_state(TASK_INTERRUPTIBLE);
            }

            set_current_state(TASK_RUNNING);
            remove_wait_queue(&list->hidraw->wait, &wait);
        }

        if (ret)
            goto out;

        len = list->buffer[list->tail].len > count ?
              count : list->buffer[list->tail].len;

        if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
            ret = -EFAULT;
            goto out;
        }
        ret = len;

        kfree(list->buffer[list->tail].value);
        list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
    }
out:
    mutex_unlock(&list->read_mutex);
    return ret;
}
/**
 * ecryptfs_miscdev_open
 * @inode: inode of miscdev handle (ignored)
 * @file: file for miscdev handle (ignored)
 *
 * Returns zero on success; non-zero otherwise
 */
static int
ecryptfs_miscdev_open(struct inode *inode, struct file *file)
{
    struct ecryptfs_daemon *daemon = NULL;
    uid_t euid = current_euid();
    int rc;

    mutex_lock(&ecryptfs_daemon_hash_mux);
    rc = try_module_get(THIS_MODULE);
    if (rc == 0) {
        rc = -EIO;
        printk(KERN_ERR "%s: Error attempting to increment module use "
               "count; rc = [%d]\n", __func__, rc);
        goto out_unlock_daemon_list;
    }
    rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
    if (rc || !daemon) {
        rc = ecryptfs_spawn_daemon(&daemon, euid, current_user_ns(),
                                   task_pid(current));
        if (rc) {
            printk(KERN_ERR "%s: Error attempting to spawn daemon; "
                   "rc = [%d]\n", __func__, rc);
            goto out_module_put_unlock_daemon_list;
        }
    }
    mutex_lock(&daemon->mux);
    if (daemon->pid != task_pid(current)) {
        rc = -EINVAL;
        printk(KERN_ERR "%s: pid [0x%p] has registered with euid [%d], "
               "but pid [0x%p] has attempted to open the handle "
               "instead\n", __func__, daemon->pid, daemon->euid,
               task_pid(current));
        goto out_unlock_daemon;
    }
    if (daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN) {
        rc = -EBUSY;
        printk(KERN_ERR "%s: Miscellaneous device handle may only be "
               "opened once per daemon; pid [0x%p] already has this "
               "handle open\n", __func__, daemon->pid);
        goto out_unlock_daemon;
    }
    daemon->flags |= ECRYPTFS_DAEMON_MISCDEV_OPEN;
    file->private_data = daemon;
    atomic_inc(&ecryptfs_num_miscdev_opens);
out_unlock_daemon:
    mutex_unlock(&daemon->mux);
out_module_put_unlock_daemon_list:
    if (rc)
        module_put(THIS_MODULE);
out_unlock_daemon_list:
    mutex_unlock(&ecryptfs_daemon_hash_mux);
    return rc;
}
Example #6
0
static void ctl_server_cleanup(struct ctl_server_state *state)
{
    void *ret;

    mutex_lock(&state->lock);

    state->exiting = true;

    if (state->thread_created) {
        thread_cancel(state->thread);

        mutex_unlock(&state->lock);
        thread_join(state->thread, &ret);
        mutex_lock(&state->lock);

        while (state->clients) {
            struct ctl_client_state *current = state->clients;
            if (current->thread_created) {
                const thread_t client_thread = current->thread;
                thread_cancel(client_thread);

                mutex_unlock(&state->lock);
                thread_join(client_thread, &ret);
                mutex_lock(&state->lock);
            }

            if (state->clients == current) {
                ctl_client_cleanup(current);
            }
        }

        state->clients = NULL;

        if (state->fd >= 0) {
            close(state->fd);
            state->fd = -1;
        }

        if (state->binded) {
            if (remove(HAKA_CTL_SOCKET_FILE)) {
                messagef(HAKA_LOG_ERROR, MODULE, L"cannot remove socket file: %s", errno_error(errno));
            }

            state->binded = false;
        }
    }

    mutex_unlock(&state->lock);

    mutex_destroy(&state->lock);
}