All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yubin Ruan <ablacktshirt@gmail.com>
To: "Michael Kerrisk (man-pages)" <mtk.manpages@gmail.com>
Cc: linux-man@vger.kernel.org, libc-alpha@sourceware.org
Subject: Re: [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent()
Date: Wed, 13 Sep 2017 16:34:51 +0800	[thread overview]
Message-ID: <20170913083446.GA16265@HP.internal.baidu.com> (raw)
In-Reply-To: <f56e1439-7a5f-3b7a-2ec4-0970fc6ad0ad@gmail.com>

On Tue, Sep 12, 2017 at 02:41:29PM +0200, Michael Kerrisk (man-pages) wrote:
> Hello Yubin,
> 
> [...]
> > +.B PTHREAD_MUTEX_ROBUST
> > +can be set on a mutex attribute object so that when the owner of the mutex
> > +dies or when the process containing such a locked mutex performs
> > +.IR execve (2)
> > +, any future attempts to call
> > +.IR pthread_mutex_lock (3)
> > +on this mutex will suceed and return
> > +.B EOWNERDEAD
> > +to indicate that the original owner no longer exists and the mutex is left in
> > +an inconsistent state. 
> How did you verify the point regarding execve(2)? I don't see this
> detailed mentioned in the standards or in the glibc source.

Please see below the program I used to verify that. I haven't go into too much
detail in the POSIX standard, though. I think I must have read it at [1] or
somewhere else (don't remember...).

And also, it is mentioned at [1] that when the process containing such a locked
mutex unmaps the memory containing the mutex, the mutex is unlocked... I think
this is trivial so I don't add it.

Thanks,
Yubin

[1]: https://docs.oracle.com/cd/E19253-01/816-5168/pthread-mutexattr-setrobust-np-3c/index.html

/************ verify-execve.c *****************/
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#define VERIFY_KEY 20170010
#define ERROR_ON(func_name) \
    fprintf(stderr, "error: " #func_name ": line[%d]: %s\n", __LINE__, strerror(errno));

int main(int argc, char *argv[]) {
    int shmid = -1;
    struct shm *shm = NULL;
    mode_t previous_umask = -1;
    int ret_code = 0;
    pthread_mutex_t *mutexp = NULL;
    pthread_mutexattr_t attr;
    pid_t pid = 0;
    char *const * execve_arg = {"cat", NULL};
    char *const * execve_env = {NULL};

    previous_umask = umask(0);
    shmid = shmget(VERIFY_KEY, sizeof(pthread_mutex_t), IPC_CREAT | 0666);
    if (shmid < 0) {
        ERROR_ON(shmget);
        return -1;
    }

    shm = (struct shm *)shmat(shmid, NULL, 0);
    if ((void *)-1 == shm) {
        ERROR_ON(shmat);
        return -1;
    }
    memset(shm, 0, sizeof(pthread_mutex_t));

    printf("Successfully attached shared memory, trying to lock\n");

    //initialize the lock
    mutexp = (pthread_mutex_t *)shm;
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);
    pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
    pthread_mutex_init(mutexp, &attr);

    ret_code = pthread_mutex_lock(mutexp);
    if (0 == ret_code) {
        printf("successfull acquired the lock. Going to fork/execve now\n");
    } else {
        ERROR_ON(pthread_mutex_lock);
        return -1;
    }

    pid = fork();
    if (0 == pid) {
        printf("child would sleep for 2 sec and then lock the mutex\n");
        sleep(2);
        ret_code = pthread_mutex_lock(mutexp);
        if (EOWNERDEAD == ret_code) {
            printf("child see EOWNERDEAD returned. Verification completed\n");
            pthread_mutex_consistent(mutexp);
            pthread_mutex_unlock(mutexp);
            exit(0);
        } else {
            printf("child see [%d] returned\n", ret_code);
            exit(1);
        }
    } else {
        printf("parent going to execve(/bin/cat)\n");
        execve("/bin/cat", execve_arg, execve_env);
    }
    return 0;
}

  reply	other threads:[~2017-09-13  8:34 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-20  9:42 [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent() Yubin Ruan
     [not found] ` <CAJYFCiMZN09HaHpvKgxz3mqzmcaXAWmkz=BmgOW9WVdRKOhDsg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2017-08-21  2:24   ` Yubin Ruan
     [not found]     ` <CAJYFCiPP54qAKdBswWuu-1Ms60KLoY7X=k5Lva5pXSYWFaks3Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2017-08-21  2:30       ` Yubin Ruan
2017-08-22  0:33   ` Michael Kerrisk (man-pages)
     [not found]     ` <36ab9ec0-b496-c007-c12f-065fd618e7fd-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2017-08-22  2:19       ` Yubin Ruan
     [not found]         ` <CAJYFCiPM6Hy1cPF2mUBu5bVqxNX+5kvKnJZLhYzMMwuMiHCKeg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2017-08-26 21:05           ` Yubin Ruan
2017-09-11  1:50             ` Yubin Ruan
     [not found]               ` <CAJYFCiN-g+GjD8StCLue60i6pOw7jZnRGn2=2znPFkFw_FDyqQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2017-09-11 20:34                 ` Michael Kerrisk (man-pages)
     [not found]             ` <20170826210528.GA32472-BUpDV9vzqx74NUO8LpiZZi+1pv2Z1Xu3@public.gmane.org>
2017-09-12 12:41               ` Michael Kerrisk (man-pages)
2017-09-13  8:34                 ` Yubin Ruan [this message]
     [not found]                   ` <20170913083446.GA16265-BUpDV9vzqx74NUO8LpiZZi+1pv2Z1Xu3@public.gmane.org>
2017-09-13  4:09                     ` Yubin Ruan
2017-09-13 12:28                     ` Michael Kerrisk (man-pages)
     [not found]                       ` <CAKgNAkh4iAkz8oL70S3dzztF_oZy+fXohS4OZ2nyySAaAMGX3w-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2017-09-15  8:29                         ` Yubin Ruan
2017-09-13 15:00               ` Michael Kerrisk (man-opages)
     [not found]                 ` <1159fa2e-006e-d3dd-7d1b-0e055cad42b5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2017-09-15  9:45                   ` Yubin Ruan
     [not found]                     ` <20170915094414.GA18043-BUpDV9vzqx74NUO8LpiZZi+1pv2Z1Xu3@public.gmane.org>
2017-09-15  7:53                       ` Michael Kerrisk (man-pages)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170913083446.GA16265@HP.internal.baidu.com \
    --to=ablacktshirt@gmail.com \
    --cc=libc-alpha@sourceware.org \
    --cc=linux-man@vger.kernel.org \
    --cc=mtk.manpages@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.