All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent()
@ 2017-08-20  9:42 Yubin Ruan
       [not found] ` <CAJYFCiMZN09HaHpvKgxz3mqzmcaXAWmkz=BmgOW9WVdRKOhDsg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
  0 siblings, 1 reply; 16+ messages in thread
From: Yubin Ruan @ 2017-08-20  9:42 UTC (permalink / raw)
  To: Michael Kerrisk (man-pages)
  Cc: linux-man-u79uwXL29TY76Z2rM5mHXA, libc-alpha-9JcytcrH/bA+uJoB2kUjGw

Hi Michael,
This patch series introduce three pthread functions for linux-man:
    * pthread_mutexattr_setrobust()
    * pthread_mutexattr_getrobust()
    * pthread_mutex_consistent()

I put pthread_mutexattr_setrobust() and pthread_mutexattr_getrobust()
altogether since they are related.

I obtain my information about these functions mainly from reading the
glibc source code and do some testing. There might be some mistake,
but they are mostly correct (95% or so).

Following this email would be those man pages in plain text and nroff
format. I have tested those nroff file on my Linux box using man, and
it look pretty well. Please point out if there are any mistake in
formatting.

Yubin
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent()
       [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-22  0:33   ` Michael Kerrisk (man-pages)
  1 sibling, 1 reply; 16+ messages in thread
From: Yubin Ruan @ 2017-08-21  2:24 UTC (permalink / raw)
  To: Michael Kerrisk (man-pages)
  Cc: linux-man-u79uwXL29TY76Z2rM5mHXA, libc-alpha-9JcytcrH/bA+uJoB2kUjGw

Hmm...My email system broke and all the mails were delayed. Here you go:

Below are patch generated by `git format-patch`. Also, please find
attached some verification code I wrote. I tended to write more, but I
would like to hear what people think about these first in case I am
not doing anything wrong in the first place.

Yubin

------------------------------------------------
>From 29850aa815506e8a97fb9d90c71285161ee69d82 Mon Sep 17 00:00:00 2001
From: Yubin Ruan <ablacktshirt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
Date: Mon, 21 Aug 2017 00:23:03 +0800
Subject: [PATCH] add pthread_mutexattr_setrobust() and
 pthread_mutex_consistent()

Signed-off-by: Yubin Ruan <ablacktshirt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
---
 man3/pthread_mutex_consistent.3    |  60 +++++++++++
 man3/pthread_mutexattr_setrobust.3 | 199 +++++++++++++++++++++++++++++++++++++
 2 files changed, 259 insertions(+)
 create mode 100644 man3/pthread_mutex_consistent.3
 create mode 100644 man3/pthread_mutexattr_setrobust.3

diff --git a/man3/pthread_mutex_consistent.3 b/man3/pthread_mutex_consistent.3
new file mode 100644
index 0000000..6d5bc81
--- /dev/null
+++ b/man3/pthread_mutex_consistent.3
@@ -0,0 +1,60 @@
+.\" Copyright (c) 2017, Yubin Ruan <ablacktshirt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
+.\"
+.\" %%%LICENSE_START(VERBATIM)
+.\" Permission is granted to make and distribute verbatim copies of this
+.\" manual provided the copyright notice and this permission notice are
+.\" preserved on all copies.
+.\"
+.\" Permission is granted to copy and distribute modified versions of this
+.\" manual under the conditions for verbatim copying, provided that the
+.\" entire resulting derived work is distributed under the terms of a
+.\" permission notice identical to this one.
+.\"
+.\" Since the Linux kernel and libraries are constantly changing, this
+.\" manual page may be incorrect or out-of-date.  The author(s) assume no
+.\" responsibility for errors or omissions, or for damages resulting from
+.\" the use of the information contained herein.  The author(s) may not
+.\" have taken the same level of care in the production of this manual,
+.\" which is licensed free of charge, as they might when working
+.\" professionally.
+.\"
+.\" Formatted or processed versions of this manual, if unaccompanied by
+.\" the source, must acknowledge the copyright and authors of this work.
+.\" %%%LICENSE_END
+.\"
+.TH PTHREAD_MUTEXATTR_SETROBUST 3 2017-08-20 "Linux" "Linux
Programmer's Manual"
+.SH NAME
+pthread_mutex_consistent \- make the robust mutex consistent
+.SH SYNOPSIS
+.nf
+.B #include <pthread.h>
+.PP
+.BI "int pthread_mute_consistent(pthread_mute_t *" mutex ");"
+.fi
+.PP
+Compile and link with \fI\-pthread\fP.
+.SH DESCRIPTION
+This function make a robust mutex consistent if it is in a inconsistent
+state. A mutex can be left in a inconsistent state if its owner terminate
+while holding the mutex, in which situation the next owner who acquire the
+mutex will succeed and be notified by the return value of
+.B EOWNERDEAD.
+
+.SH RETURN VALUE
+On success,
+.IR pthread_mutex_consistent()
+return 0. Otherwise an error value is returned to indicate the error.
+
+.SH ERRORS
+.TP
+.B EINVAL
+The mutex is either not robust or it is not in a inconsistent state.
+
+.SH SEE ALSO
+.ad l
+.nh
+.BR pthread_mutexattr_init (3),
+.BR pthread_mutex_lock (3),
+.BR pthread_mutexattr_setrobust (3),
+.BR pthread_mutexattr_getrobust (3),
+.BR pthreads (7)
diff --git a/man3/pthread_mutexattr_setrobust.3
b/man3/pthread_mutexattr_setrobust.3
new file mode 100644
index 0000000..e44e590
--- /dev/null
+++ b/man3/pthread_mutexattr_setrobust.3
@@ -0,0 +1,199 @@
+.\" Copyright (c) 2017, Yubin Ruan <ablacktshirt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
+.\"
+.\" %%%LICENSE_START(VERBATIM)
+.\" Permission is granted to make and distribute verbatim copies of this
+.\" manual provided the copyright notice and this permission notice are
+.\" preserved on all copies.
+.\"
+.\" Permission is granted to copy and distribute modified versions of this
+.\" manual under the conditions for verbatim copying, provided that the
+.\" entire resulting derived work is distributed under the terms of a
+.\" permission notice identical to this one.
+.\"
+.\" Since the Linux kernel and libraries are constantly changing, this
+.\" manual page may be incorrect or out-of-date.  The author(s) assume no
+.\" responsibility for errors or omissions, or for damages resulting from
+.\" the use of the information contained herein.  The author(s) may not
+.\" have taken the same level of care in the production of this manual,
+.\" which is licensed free of charge, as they might when working
+.\" professionally.
+.\"
+.\" Formatted or processed versions of this manual, if unaccompanied by
+.\" the source, must acknowledge the copyright and authors of this work.
+.\" %%%LICENSE_END
+.\"
+.TH PTHREAD_MUTEXATTR_SETROBUST 3 2017-08-20 "Linux" "Linux
Programmer's Manual"
+.SH NAME
+pthread_mutexattr_getrobust, pthread_mutexattr_setrobust,
+pthread_mutexattr_getrobust_np, pthread_mutexattr_setrobust_np
+\- get and set the robustness attribute of a mutex attribute object
+.SH SYNOPSIS
+.nf
+.B #include <pthread.h>
+.PP
+.BI "int pthread_mutexattr_getrobust(const pthread_mutexattr_t *"
attr, "int *" robustness ");"
+.BI "int pthread_mutexattr_setrobust(const pthread_mutexattr_t *"
attr, "int *" robustness ");"
+.BI "int pthread_mutexattr_getrobust_np(const pthread_mutexattr_t *"
attr, "int *" robustness ");"
+.BI "int pthread_mutexattr_setrobust_np(const pthread_mutexattr_t *"
attr, "int *" robustness ");"
+.fi
+.PP
+Compile and link with \fI\-pthread\fP.
+.SH DESCRIPTION
+The
+.BR pthread_mutexattr_getrobust()
+and
+.BR pthread_mutexattr_setrobust()
+functions get and set the robustness attribute of a
+initialized mutex attribute object, respectively.
+The robustness attribute specify the behavior of the mutex
+when its owner dies without unlocking it. These two
+functions are specified in POSIX. Glibc's NPTL has
+.BR pthread_mutexattr_getrobust_np (3)
+and
+.BR pthread_mutexattr_setrobust_np (3)
+respectively but they are just aliases, with the "np" standing for
"Native Posix".
+See
+.BR NPTL (7).
+.PP
+Currently there are only two possible values for the
+.IR robustness
+attribute:
+.TP
+.BR PTHREAD_MUTEX_STALLED
+is the default value for a mutex attribute object. If a mutex is initialized
+with a
+.BR PTHREAD_MUTEX_STALLED
+attribute object and its owner dies without unlocking it, it is kept locked
+afterwards and any future attempts to call
+.IR pthread_mutex_lock (3)
+on this mutex will block indefinitely.
+.TP
+.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 inconsisten state. Usually after
+.B EOWNERDEAD
+is returned, the next owner should call
+.IR pthread_mutex_consistent (3)
+on the acquired mutex to make it consistent again before using it any further.
+If the next owner unlock it using
+.IR pthread_mutex_unlock (3)
+before making it consistent, the mutex will be unusable permanently and any
+subsequent attempts to lock it using
+.IR pthread_mutex_lock (3)
+will return
+.B ENORECOVERABLE.
+If the next owner terminates before calling
+.IR pthread_mutex_consistent (3)
+, furture
+.IR pthread_mutex_lock (3)
+on this mutex will still return
+.B EOWNERDEAD.
+
+
+Glibc defined
+.B PTHREAD_MUTEX_STALLED_NP
+and
+.B PTHREAD_MUTEX_ROBUST_NP
+as aliases of
+.B PTHREAD_MUTEX_STALLED
+and
+.B PTHREAD_MUTEX_ROBUST
+respectively.
+
+Note that the
+.IR attr
+argument of
+.IR pthread_mutexattr_getrobust()
+and
+.IR pthread_mutexattr_setrobust()
+should refer to a mutex attribute object that was initialized by
+.IR pthread_mutexattr_init (3)
+, otherwise the behavior is undefined.
+.SH RETURN VALUE
+On success, zero is returned by
+.IR pthread_mutexattr_getrobust()
+and the value pointed to by the
+.IR robustness
+parameter is set to the robustness attribute of
+.IR attr.
+Otherwise, an error number shall be returned. On success
+.IR pthread_mutexattr_setrobust()
+set the robustness attribute into the mutex attribute object
+.IR attr
+and return zero, otherwise a error number is returned to indicate the error.
+.SS Glibc-specificed features
+In glibc's implementation,
+.IR pthread_mutexattr_getrobust()
+always return zero.
+.SH ERRORS
+.TP
+.B EINVAL
+A value other than
+.B PTHREAD_MUTEX_STALLED
+or
+.B PTHREAD_MUTEX_ROBUST
+is passed into
+.IR pthread_mutexattr_setrobust().
+
+.SH EXAMPLE
+The program demonstrate a simple usecase of the robustness attribute of a
+pthread mutex attribute object. In this program, a thread holding the mutex
+die prematurely withough unlocking the mutex. Another thread acquire it
+successfully and get EOWNERDEAD.
+
+.SS Program source
+\&
+.EX
+#include <stdio.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <errno.h>
+
+pthread_mutex_t lock;
+
+void *original_owner_thread(void *ptr)
+{
+    printf("[original owner] Setting lock...\n");
+    pthread_mutex_lock(&lock);
+    printf("[original owner] Locked. Now exiting without unlocking.\n");
+    pthread_exit(NULL);
+}
+
+int main(int argc, char *argv[])
+{
+    pthread_t lock_getter;
+    pthread_mutexattr_t attr;
+    pthread_mutexattr_init(&attr);   /* initialize the attribute object */
+    pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST); /* set
robustness */
+
+    pthread_mutex_init(&lock, &attr);   /* initialize the lock */
+
+    pthread_create(&lock_getter, NULL, original_owner_thread, NULL);
+    sleep(2);   /* original_owner_thread should have exited now */
+
+    printf("Attempting to acquire unlock robust mutex.\n");
+    int ret_code = pthread_mutex_lock(&lock);
+    if(EOWNERDEAD == ret_code) {
+        printf("EOWNERDEAD returned. Make the mutex consistent now\n");
+        pthread_mutex_consistent(&lock);
+    }
+
+    pthread_mutex_unlock(&lock);
+
+    return 0;
+}
+.EE
+.SH SEE ALSO
+.ad l
+.nh
+.BR pthread_mutex_init (3),
+.BR pthread_mutex_consistent (3),
+.BR pthread_mutex_lock (3)
+.BR pthreads (7)
-- 
2.7.4
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent()
       [not found]     ` <CAJYFCiPP54qAKdBswWuu-1Ms60KLoY7X=k5Lva5pXSYWFaks3Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2017-08-21  2:30       ` Yubin Ruan
  0 siblings, 0 replies; 16+ messages in thread
From: Yubin Ruan @ 2017-08-21  2:30 UTC (permalink / raw)
  To: Michael Kerrisk (man-pages)
  Cc: linux-man-u79uwXL29TY76Z2rM5mHXA, libc-alpha-9JcytcrH/bA+uJoB2kUjGw

[-- Attachment #1: Type: text/plain, Size: 6073 bytes --]

Here are the man page in plain text, in case you want to read it
directly in the email.

FYI, I obtain information about these API mainly from reading the
source and testing. If you find anything wrong, please kindly reply
and point it out.

Many thanks!
Yubin

--- pthread_mutexattr_setrobust() and pthread_mutexattr_getrobust() ---

Name
  pthread_mutexattr_getrobust, pthread_mutexattr_setrobust,
  pthread_mutexattr_getrobust_np, pthread_mutexattr_setrobust_np

  -- get and set the robustness attribute of a mutex attribute object

SYNOPSIS
  #include <pthread.h>

 int pthread_mutexattr_getrobust(const pthread_mutexattr_t *attr, int
*robustness);
 int pthread_mutexattr_setrobust(const pthread_mutexattr_t *attr, int
*robustness);
 int pthread_mutexattr_getrobust_np(const pthread_mutexattr_t *attr,
int *robustness);
 int pthread_mutexattr_setrobust_np(const pthread_mutexattr_t *attr,
int *robustness);

  Compile and link with -pthread.

DESCRIPTION
  The pthread_mutexattr_getrobust() and pthread_mutexattr_setrobust() functions
  get and set the robustness attribute of a initialized mutex attribute object,
  respectively. The robustness attribute specify the behavior of the mutex when
  its owner dies without unlocking it. These two functions are
specified in POSIX.
  Glibc's NPTL has pthread_mutexattr_getrobust_np() and
pthread_mutexattr_setrobust_np()
  respectively but they are just aliases, with the "np" standing for
"Native Posix".
  See NPTL(7).

  Currently there are only two possible values for the robustness attribute:

  PTHREAD_MUTEX_STALLED is the default value for a mutex attribute object. If a
  mutex is initialized with a PTHREAD_MUTEX_STALLED attribute object and its
  owner dies without unlocking it, it is kept locked afterwards and any future
  attempts to call pthread_mutex_lock() on this mutex will block indefinitely.

  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 execve(2), any future attempts to call pthread_mutex_lock() on this
  mutex will suceed and return EOWNERDEAD to indicate that the original owner no
  longer exists and the mutex is left in an inconsisten state. After EOWNERDEAD
  is returned, the next owner should call pthread_mutex_consistent(3) on the
  acquired mutex to make it consistent again before using it any further. If the
  next owner calls pthread_mutex_unlock(3) before making it consistent, the
  mutex will be unusable permanently and any subsequent attempts to
lock it using
  pthread_mutex_lock(3) will return ENORECOVERABLE. If the next owner terminates
  before calling pthread_mutex_consistent(3), furture pthread_mutex_lock(3) on
  this mutex will still return EOWNERDEAD.

  Glibc defined PTHREAD_MUTEX_STALLED_NP and PTHREAD_MUTEX_ROBUST_NP as aliases
  of PTHREAD_MUTEX_STALLED and PTHREAD_MUTEX_ROBUST respectively.

  Note that the *attr* argument of pthread_mutexattr_getrobust() and
  pthread_mutexattr_setrobust() should refer to a mutex attribute object that
  was initialized by pthread_mutexattr_init(3), otherwise the behavior is
  undefined.

RETURN VALUE
  On success, zero is returned by pthread_mutexattr_getrobust() and the value
  pointed to by the *robustness* parameter is set to the robustness attribute
  of *attr*. Otherwise, an error number shall be returned. On success
  pthread_mutexattr_setrobust() set the robustness attribute into the mutex
  attribute object *attr* and return zero, otherwise a error number is returned
  to indicate the error.

  [Glibc-specificed features]
  In glibc's implementation, pthread_mutexattr_getrobust() always return zero.

ERRORS

  EINVAL A value other than PTHREAD_MUTEX_STALLED or PTHREAD_MUTEX_ROBUST is
  passed into pthread_mutexattr_setrobust().

EXAMPLES

    #include <stdio.h>
    #include <unistd.h>
    #include <pthread.h>
    #include <errno.h>

    pthread_mutex_t lock;

    void *original_owner_thread(void *ptr)
    {
        printf("[original owner] Setting lock...\n");
        pthread_mutex_lock(&lock);
        printf("[original owner] Locked. Now exiting without unlocking.\n");
        pthread_exit(NULL);
    }

    int main(int argc, char *argv[])
    {
        pthread_t lock_getter;
        pthread_mutexattr_t attr;
        pthread_mutexattr_init(&attr);   /* initialize the attribute object */
        pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST); /*
set robustness */

        pthread_mutex_init(&lock, &attr);   /* initialize the lock */

        pthread_create(&lock_getter, NULL, original_owner_thread, NULL);
        sleep(2);   /* original_owner_thread should have exited now */

        printf("Attempting to acquire unlock robust mutex.\n");
        int ret_code = pthread_mutex_lock(&lock);
        if(EOWNERDEAD == ret_code) {
            printf("EOWNERDEAD returned. Make the mutex consistent now\n");
            pthread_mutex_consistent(&lock);
        }

        pthread_mutex_unlock(&lock);

        return 0;
    }


SEE ALSO
    pthread_mutexattr_init(3), pthread_mutex_consistent(3),
    pthread_mutex_lock(3)

--- END ---

--- pthread_mutex_consistent() ---

NAME
    pthread_mutex_consistent - make the robust mutex consistent

SYNOPSIS
    #include <pthread.h>
    int pthread_mutex_consistent(pthread_mutex_t *mutex)

    Compile and link with -pthread.

DESCRIPTION
    This function make a robust mutex consistent if it is in a inconsistent
    state. A mutex can be left in a inconsistent state if its owner terminate
    while holding the mutex, in which situation the next owner who acquire the
    mutex will succeed and be notified by the return value of EOWNERDEAD.

RETURN VALUE
    On success, pthread_mutex_consistent() return 0. Otherwise an error value
    is returned to indicate the error.

ERRORS
    EINVAL The mutex is either not robust or is not in a inconsistent state.

SEE ALSO
    pthread_mutex_lock(3), pthread_mutexattr_init(3),
    pthread_mutexattr_setrobust(3), pthread_mutexattr_getrobust(3)


---- END ---

[-- Attachment #2: verify_robustness.1.c --]
[-- Type: text/x-csrc, Size: 1283 bytes --]

/*
 * This program is used to verify that when the owner of a non-robust lock exit
 * prematurely without unlocking the mutex, the next owner will be block
 * indefintely.
 *
 * Compile and link with -pthread
 */

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>

pthread_mutex_t lock;

void *original_owner_thread(void *ptr)
{
    printf("[original owner] Setting lock...\n");
    pthread_mutex_lock(&lock);
    printf("[original owner] Locked. Now exiting without unlocking.\n");
    pthread_exit(NULL);
}

int main(int argc, char *argv[]) 
{
    pthread_t lock_getter;
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);   /* initialize the attribute object */
    pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);

    pthread_mutex_init(&lock, &attr);   /* initialize the lock */

    pthread_create(&lock_getter, NULL, original_owner_thread, NULL);
    sleep(2);   /* original_owner_thread should have exited now */

    printf("Attempting to acquire unlock robust mutex.\n");
    int ret_code = pthread_mutex_lock(&lock);
    if(EOWNERDEAD == ret_code) {
        printf("EOWNERDEAD returned. Make the mutex consistent now\n");
        pthread_mutex_consistent(&lock);
    }

    pthread_mutex_unlock(&lock);

    return 0;
}

[-- Attachment #3: verify_robustness.2.c --]
[-- Type: text/x-csrc, Size: 1384 bytes --]

/*
 * This program is used to verify that when the owner of a non-robust lock exit
 * prematurely without unlocking the mutex, the next owner will be block
 * indefintely.
 */

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>

pthread_mutex_t lock;

void *original_owner_thread(void *ptr)
{
    printf("[original owner] Setting lock...\n");
    pthread_mutex_lock(&lock);
    printf("[original owner] Locked. Now exiting without unlocking.\n");
    pthread_exit(NULL);
}

int main(int argc, char *argv[]) 
{
    pthread_t lock_getter;
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);   /* initialize the attribute object */
    /* the following line can be commented out because PTHREAD_MUTEX_STALLED is
     * the default value for a mutex attribute object */
    pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_STALLED);

    pthread_mutex_init(&lock, &attr);   /* initialize the lock */

    pthread_create(&lock_getter, NULL, original_owner_thread, NULL);
    sleep(2);   /* original_owner_thread should have exited now */

    printf("Attempting to acquire unlock robust mutex.\n");
    int ret_code = pthread_mutex_lock(&lock);
    if(EOWNERDEAD == ret_code) {
        printf("EOWNERDEAD returned. Make the mutex consistent now\n");
        pthread_mutex_consistent(&lock);
    }

    pthread_mutex_unlock(&lock);

    return 0;
}

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent()
       [not found] ` <CAJYFCiMZN09HaHpvKgxz3mqzmcaXAWmkz=BmgOW9WVdRKOhDsg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
  2017-08-21  2:24   ` Yubin Ruan
@ 2017-08-22  0:33   ` Michael Kerrisk (man-pages)
       [not found]     ` <36ab9ec0-b496-c007-c12f-065fd618e7fd-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
  1 sibling, 1 reply; 16+ messages in thread
From: Michael Kerrisk (man-pages) @ 2017-08-22  0:33 UTC (permalink / raw)
  To: Yubin Ruan
  Cc: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w,
	linux-man-u79uwXL29TY76Z2rM5mHXA,
	libc-alpha-9JcytcrH/bA+uJoB2kUjGw

Hello Yubin

On 08/20/2017 11:42 AM, Yubin Ruan wrote:
> Hi Michael,
> This patch series introduce three pthread functions for linux-man:
>     * pthread_mutexattr_setrobust()
>     * pthread_mutexattr_getrobust()
>     * pthread_mutex_consistent()
> 
> I put pthread_mutexattr_setrobust() and pthread_mutexattr_getrobust()
> altogether since they are related.
> 
> I obtain my information about these functions mainly from reading the
> glibc source code and do some testing. There might be some mistake,
> but they are mostly correct (95% or so).
> 
> Following this email would be those man pages in plain text and nroff
> format. I have tested those nroff file on my Linux box using man, and
> it look pretty well. Please point out if there are any mistake in
> formatting.

Thanks for the drafts. Could I ask you to review the man-pages(7)
page. There's a number of details described in that page
which should be fixed in the drafts. Also, please try rendering
your pages with "man -l <page>". This will show problems with 
the example code. See the chown(2) man page example for
the required escape sequences for "\n" and the single quote.

Thanks,

Michael

-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent()
       [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>
  0 siblings, 1 reply; 16+ messages in thread
From: Yubin Ruan @ 2017-08-22  2:19 UTC (permalink / raw)
  To: Michael Kerrisk (man-pages)
  Cc: linux-man-u79uwXL29TY76Z2rM5mHXA, libc-alpha-9JcytcrH/bA+uJoB2kUjGw

2017-08-22 8:33 GMT+08:00 Michael Kerrisk (man-pages) <mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:
> Hello Yubin
>
> On 08/20/2017 11:42 AM, Yubin Ruan wrote:
>> Hi Michael,
>> This patch series introduce three pthread functions for linux-man:
>>     * pthread_mutexattr_setrobust()
>>     * pthread_mutexattr_getrobust()
>>     * pthread_mutex_consistent()
>>
>> I put pthread_mutexattr_setrobust() and pthread_mutexattr_getrobust()
>> altogether since they are related.
>>
>> I obtain my information about these functions mainly from reading the
>> glibc source code and do some testing. There might be some mistake,
>> but they are mostly correct (95% or so).
>>
>> Following this email would be those man pages in plain text and nroff
>> format. I have tested those nroff file on my Linux box using man, and
>> it look pretty well. Please point out if there are any mistake in
>> formatting.
>
> Thanks for the drafts. Could I ask you to review the man-pages(7)
> page. There's a number of details described in that page
> which should be fixed in the drafts. Also, please try rendering
> your pages with "man -l <page>". This will show problems with
> the example code. See the chown(2) man page example for
> the required escape sequences for "\n" and the single quote.

No problem. I would fix the formatting issues.

Yubin
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent()
       [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]             ` <20170826210528.GA32472-BUpDV9vzqx74NUO8LpiZZi+1pv2Z1Xu3@public.gmane.org>
  0 siblings, 2 replies; 16+ messages in thread
From: Yubin Ruan @ 2017-08-26 21:05 UTC (permalink / raw)
  To: Michael Kerrisk (man-pages)
  Cc: linux-man-u79uwXL29TY76Z2rM5mHXA, libc-alpha-9JcytcrH/bA+uJoB2kUjGw

[-- Attachment #1: Type: text/plain, Size: 11584 bytes --]

On Tue, Aug 22, 2017 at 10:19:48AM +0800, Yubin Ruan wrote:
> 2017-08-22 8:33 GMT+08:00 Michael Kerrisk (man-pages) <mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:
> > Hello Yubin
> >
> > On 08/20/2017 11:42 AM, Yubin Ruan wrote:
> >> Hi Michael,
> >> This patch series introduce three pthread functions for linux-man:
> >>     * pthread_mutexattr_setrobust()
> >>     * pthread_mutexattr_getrobust()
> >>     * pthread_mutex_consistent()
> >>
> >> I put pthread_mutexattr_setrobust() and pthread_mutexattr_getrobust()
> >> altogether since they are related.
> >>
> >> I obtain my information about these functions mainly from reading the
> >> glibc source code and do some testing. There might be some mistake,
> >> but they are mostly correct (95% or so).
> >>
> >> Following this email would be those man pages in plain text and nroff
> >> format. I have tested those nroff file on my Linux box using man, and
> >> it look pretty well. Please point out if there are any mistake in
> >> formatting.
> >
> > Thanks for the drafts. Could I ask you to review the man-pages(7)
> > page. There's a number of details described in that page
> > which should be fixed in the drafts. Also, please try rendering
> > your pages with "man -l <page>". This will show problems with
> > the example code. See the chown(2) man page example for
> > the required escape sequences for "\n" and the single quote.
> 
> No problem. I would fix the formatting issues.

Hi Michael,

Sorry that it took so long.

Following is the renew patch. I have corrected several formatting issues (i.e.,
\n , - , but I can't find anything wrong with ' ) and some typos. The only
issue I cannot solve is that the new `pthread_mutex_consistent(3)` page is too
short that it shows at the very bottom... please help with this. 

Also, please find attached three verification snippets.


Signed-off-by: Yubin Ruan <ablacktshirt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
---
 man3/pthread_mutex_consistent.3    |  60 +++++++++++
 man3/pthread_mutexattr_setrobust.3 | 199 +++++++++++++++++++++++++++++++++++++
 2 files changed, 259 insertions(+)
 create mode 100644 man3/pthread_mutex_consistent.3
 create mode 100644 man3/pthread_mutexattr_setrobust.3

diff --git a/man3/pthread_mutex_consistent.3 b/man3/pthread_mutex_consistent.3
new file mode 100644
index 0000000..6d5bc81
--- /dev/null
+++ b/man3/pthread_mutex_consistent.3
@@ -0,0 +1,60 @@
+.\" Copyright (c) 2017, Yubin Ruan <ablacktshirt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
+.\"
+.\" %%%LICENSE_START(VERBATIM)
+.\" Permission is granted to make and distribute verbatim copies of this
+.\" manual provided the copyright notice and this permission notice are
+.\" preserved on all copies.
+.\"
+.\" Permission is granted to copy and distribute modified versions of this
+.\" manual under the conditions for verbatim copying, provided that the
+.\" entire resulting derived work is distributed under the terms of a
+.\" permission notice identical to this one.
+.\"
+.\" Since the Linux kernel and libraries are constantly changing, this
+.\" manual page may be incorrect or out-of-date.  The author(s) assume no
+.\" responsibility for errors or omissions, or for damages resulting from
+.\" the use of the information contained herein.  The author(s) may not
+.\" have taken the same level of care in the production of this manual,
+.\" which is licensed free of charge, as they might when working
+.\" professionally.
+.\"
+.\" Formatted or processed versions of this manual, if unaccompanied by
+.\" the source, must acknowledge the copyright and authors of this work.
+.\" %%%LICENSE_END
+.\"
+.TH PTHREAD_MUTEXATTR_SETROBUST 3 2017-08-20 "Linux" "Linux Programmer's Manual"
+.SH NAME
+pthread_mutex_consistent \- make the robust mutex consistent
+.SH SYNOPSIS
+.nf
+.B #include <pthread.h>
+.PP
+.BI "int pthread_mute_consistent(pthread_mute_t *" mutex ");"
+.fi
+.PP
+Compile and link with \fI\-pthread\fP.
+.SH DESCRIPTION
+This function make a robust mutex consistent if it is in a inconsistent
+state. A mutex can be left in a inconsistent state if its owner terminate
+while holding the mutex, in which situation the next owner who acquire the
+mutex will succeed and be notified by the return value of
+.B EOWNERDEAD.
+
+.SH RETURN VALUE
+On success,
+.IR pthread_mutex_consistent()
+return 0. Otherwise an error value is returned to indicate the error.
+
+.SH ERRORS
+.TP
+.B EINVAL
+The mutex is either not robust or it is not in a inconsistent state.
+
+.SH SEE ALSO
+.ad l
+.nh
+.BR pthread_mutexattr_init (3),
+.BR pthread_mutex_lock (3),
+.BR pthread_mutexattr_setrobust (3),
+.BR pthread_mutexattr_getrobust (3),
+.BR pthreads (7)
diff --git a/man3/pthread_mutexattr_setrobust.3 b/man3/pthread_mutexattr_setrobust.3
new file mode 100644
index 0000000..3add496
--- /dev/null
+++ b/man3/pthread_mutexattr_setrobust.3
@@ -0,0 +1,199 @@
+.\" Copyright (c) 2017, Yubin Ruan <ablacktshirt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
+.\"
+.\" %%%LICENSE_START(VERBATIM)
+.\" Permission is granted to make and distribute verbatim copies of this
+.\" manual provided the copyright notice and this permission notice are
+.\" preserved on all copies.
+.\"
+.\" Permission is granted to copy and distribute modified versions of this
+.\" manual under the conditions for verbatim copying, provided that the
+.\" entire resulting derived work is distributed under the terms of a
+.\" permission notice identical to this one.
+.\"
+.\" Since the Linux kernel and libraries are constantly changing, this
+.\" manual page may be incorrect or out-of-date.  The author(s) assume no
+.\" responsibility for errors or omissions, or for damages resulting from
+.\" the use of the information contained herein.  The author(s) may not
+.\" have taken the same level of care in the production of this manual,
+.\" which is licensed free of charge, as they might when working
+.\" professionally.
+.\"
+.\" Formatted or processed versions of this manual, if unaccompanied by
+.\" the source, must acknowledge the copyright and authors of this work.
+.\" %%%LICENSE_END
+.\"
+.TH PTHREAD_MUTEXATTR_SETROBUST 3 2017-08-20 "Linux" "Linux Programmer's Manual"
+.SH NAME
+pthread_mutexattr_getrobust, pthread_mutexattr_setrobust,
+pthread_mutexattr_getrobust_np, pthread_mutexattr_setrobust_np
+\- get and set the robustness attribute of a mutex attribute object 
+.SH SYNOPSIS
+.nf
+.B #include <pthread.h>
+.PP
+.BI "int pthread_mutexattr_getrobust(const pthread_mutexattr_t *" attr, "int *" robustness ");"
+.BI "int pthread_mutexattr_setrobust(const pthread_mutexattr_t *" attr, "int *" robustness ");"
+.BI "int pthread_mutexattr_getrobust_np(const pthread_mutexattr_t *" attr, "int *" robustness ");"
+.BI "int pthread_mutexattr_setrobust_np(const pthread_mutexattr_t *" attr, "int *" robustness ");" 
+.fi
+.PP
+Compile and link with \fI\-pthread\fP.
+.SH DESCRIPTION
+The
+.BR pthread_mutexattr_getrobust()
+and
+.BR pthread_mutexattr_setrobust()
+functions get and set the robustness attribute of a
+initialized mutex attribute object, respectively.
+The robustness attribute specifies the behavior of the mutex
+when its owner dies without unlocking it. These two
+functions are specified in POSIX. Glibc's NPTL has
+.BR pthread_mutexattr_getrobust_np (3)
+and
+.BR pthread_mutexattr_setrobust_np (3)
+respectively but they are just aliases, with the "np" standing for "Native Posix".
+See
+.BR NPTL (7). 
+.PP
+Currently there are only two possible values for the 
+.IR robustness
+attribute:
+.TP 
+.BR PTHREAD_MUTEX_STALLED
+is the default value for a mutex attribute object. If a mutex is initialized
+with a
+.BR PTHREAD_MUTEX_STALLED
+attribute object and its owner dies without unlocking it, it is kept locked
+afterwards and any future attempts to call
+.IR pthread_mutex_lock (3)
+on this mutex will block indefinitely. 
+.TP
+.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. Usually after
+.B EOWNERDEAD
+is returned, the next owner should call
+.IR pthread_mutex_consistent (3)
+on the acquired mutex to make it consistent again before using it any further.
+If the next owner unlock it using
+.IR pthread_mutex_unlock (3)
+before making it consistent, the mutex will be unusable permanently and any
+subsequent attempts to lock it using
+.IR pthread_mutex_lock (3)
+will return
+.B ENOTRECOVERABLE.
+If the next owner terminates before calling
+.IR pthread_mutex_consistent (3)
+, furture
+.IR pthread_mutex_lock (3)
+on this mutex will still return
+.B EOWNERDEAD. 
+
+
+Glibc defines
+.B PTHREAD_MUTEX_STALLED_NP
+and
+.B PTHREAD_MUTEX_ROBUST_NP
+as aliases of 
+.B PTHREAD_MUTEX_STALLED
+and 
+.B PTHREAD_MUTEX_ROBUST
+respectively.
+
+Note that the
+.IR attr
+argument of
+.IR pthread_mutexattr_getrobust()
+and 
+.IR pthread_mutexattr_setrobust()
+should refer to a mutex attribute object that was initialized by
+.IR pthread_mutexattr_init (3)
+, otherwise the behavior is undefined. 
+.SH RETURN VALUE
+On success, zero is returned by 
+.IR pthread_mutexattr_getrobust()
+and the value pointed to by the
+.IR robustness 
+parameter is set to the robustness attribute of
+.IR attr.
+Otherwise, an error number shall be returned. On success
+.IR pthread_mutexattr_setrobust()
+set the robustness attribute into the mutex attribute object
+.IR attr
+and return zero, otherwise a error number is returned to indicate the error.
+.SS Glibc\-specificed features
+In glibc's implementation,
+.IR pthread_mutexattr_getrobust()
+always return zero.
+.SH ERRORS
+.TP
+.B EINVAL
+A value other than
+.B PTHREAD_MUTEX_STALLED
+or
+.B PTHREAD_MUTEX_ROBUST
+is passed into
+.IR pthread_mutexattr_setrobust(). 
+
+.SH EXAMPLE
+.PP
+The program demonstrate a simple usecase of the robustness attribute of a
+pthread mutex attribute object. In this program, a thread holding the mutex
+dies prematurely withough unlocking the mutex. Another thread acquires it
+successfully and get EOWNERDEAD.
+
+.SS Program source
+.EX
+#include <stdio.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <errno.h>
+
+pthread_mutex_t lock;
+
+void *original_owner_thread(void *ptr)
+{
+    printf("[original owner] Setting lock...\\n");
+    pthread_mutex_lock(&lock);
+    printf("[original owner] Locked. Now exiting without unlocking.\\n");
+    pthread_exit(NULL);
+}
+
+int main(int argc, char *argv[]) 
+{
+    pthread_t lock_getter;
+    pthread_mutexattr_t attr;
+    pthread_mutexattr_init(&attr);   /* initialize the attribute object */
+    pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST); /* set robustness */
+
+    pthread_mutex_init(&lock, &attr);   /* initialize the lock */
+
+    pthread_create(&lock_getter, NULL, original_owner_thread, NULL);
+    sleep(2);   /* original_owner_thread should have exited now */
+
+    printf("Attempting to acquire the unlocked robust mutex.\\n");
+    int ret_code = pthread_mutex_lock(&lock);
+    if(EOWNERDEAD == ret_code) {
+        printf("EOWNERDEAD returned. Make the mutex consistent now\\n");
+        pthread_mutex_consistent(&lock);
+    }
+
+    pthread_mutex_unlock(&lock);
+
+    return 0;
+}
+.EE
+.SH SEE ALSO
+.ad l
+.nh
+.BR pthread_mutex_init (3),
+.BR pthread_mutex_consistent (3),
+.BR pthread_mutex_lock (3)
+.BR pthreads (7)
-- 
2.7.4


[-- Attachment #2: verify_robustness.1.c --]
[-- Type: text/x-csrc, Size: 1319 bytes --]

/*
 * This program is used to verify that when the owner of a robust lock exit
 * prematurely without unlocking the mutex, the next owner will be successfully
 * acquire the lock and get EOWNERDEAD returned.
 *
 * Compile and link with -pthread
 */

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>

pthread_mutex_t lock;

void *original_owner_thread(void *ptr)
{
    printf("[original owner] Setting lock...\n");
    pthread_mutex_lock(&lock);
    printf("[original owner] Locked. Now exiting without unlocking.\n");
    pthread_exit(NULL);
}

int main(int argc, char *argv[]) 
{
    pthread_t lock_getter;
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);   /* initialize the attribute object */
    pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);

    pthread_mutex_init(&lock, &attr);   /* initialize the lock */

    pthread_create(&lock_getter, NULL, original_owner_thread, NULL);
    sleep(2);   /* original_owner_thread should have exited now */

    printf("Attempting to acquire unlock robust mutex.\n");
    int ret_code = pthread_mutex_lock(&lock);
    if(EOWNERDEAD == ret_code) {
        printf("EOWNERDEAD returned. Make the mutex consistent now\n");
        pthread_mutex_consistent(&lock);
    }

    pthread_mutex_unlock(&lock);

    return 0;
}

[-- Attachment #3: verify_robustness.2.c --]
[-- Type: text/x-csrc, Size: 1421 bytes --]

/*
 * This program is used to verify that when the owner of a non-robust lock exit
 * prematurely without unlocking the mutex, the next owner will be block
 * indefintely.
 *
 * Compile and link with -pthread
 */

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>

pthread_mutex_t lock;

void *original_owner_thread(void *ptr)
{
    printf("[original owner] Setting lock...\n");
    pthread_mutex_lock(&lock);
    printf("[original owner] Locked. Now exiting without unlocking.\n");
    pthread_exit(NULL);
}

int main(int argc, char *argv[]) 
{
    pthread_t lock_getter;
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);   /* initialize the attribute object */
    /* the following line can be commented out because PTHREAD_MUTEX_STALLED is
     * the default value for a mutex attribute object */
    pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_STALLED);

    pthread_mutex_init(&lock, &attr);   /* initialize the lock */

    pthread_create(&lock_getter, NULL, original_owner_thread, NULL);
    sleep(2);   /* original_owner_thread should have exited now */

    printf("Attempting to acquire unlock robust mutex.\n");
    int ret_code = pthread_mutex_lock(&lock);
    if(EOWNERDEAD == ret_code) {
        printf("EOWNERDEAD returned. Make the mutex consistent now\n");
        pthread_mutex_consistent(&lock);
    }

    pthread_mutex_unlock(&lock);

    return 0;
}

[-- Attachment #4: verify_robustness.3.c --]
[-- Type: text/x-csrc, Size: 1772 bytes --]

/*
 * This program is used to verify that when the owner of a robust mutex exit
 * prematurely without unlocking the mutex, the next owner will be successfully
 * acquire the mutex and get EOWNERDEAD returned. And, if that "next owner"
 * unlock this mutex without making it consistent, it will be unusable
 * permanently and any subsequent attempts to lock it using
 * "pthread_mutex_lock(3)" with return ENORECOVERABLE.
 *
 * Compile and link with -pthread
 */

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>

pthread_mutex_t lock;

void *original_owner_thread(void *ptr)
{
    printf("[original owner] Setting lock...\n");
    pthread_mutex_lock(&lock);
    printf("[original owner] Locked. Now exiting without unlocking.\n");
    pthread_exit(NULL);
}

int main(int argc, char *argv[]) 
{
    pthread_t lock_getter;
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);   /* initialize the attribute object */
    pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);

    pthread_mutex_init(&lock, &attr);   /* initialize the lock */

    pthread_create(&lock_getter, NULL, original_owner_thread, NULL);
    sleep(2);   /* original_owner_thread should have exited now */

    int ret_code;

    printf("Attempting to acquire unlock robust mutex.\n");
    ret_code = pthread_mutex_lock(&lock);
    if(EOWNERDEAD == ret_code) {
        printf("EOWNERDEAD returned. Unlock the mutex without making it consistent.\n");
        pthread_mutex_unlock(&lock);
    }

    /* try to acquire the lock again, and ENORECOVERABLE will be returned */
    ret_code = pthread_mutex_lock(&lock);
    if(ENOTRECOVERABLE == ret_code) {
        printf("ENOTRECOVERABLE returned. This mutex is not recoverable now\n");
    }

    return 0;
}

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent()
  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>
       [not found]             ` <20170826210528.GA32472-BUpDV9vzqx74NUO8LpiZZi+1pv2Z1Xu3@public.gmane.org>
  1 sibling, 1 reply; 16+ messages in thread
From: Yubin Ruan @ 2017-09-11  1:50 UTC (permalink / raw)
  To: Michael Kerrisk (man-pages); +Cc: linux-man, libc-alpha

just re-sending this to see whether anybody is still interested.

Yubin

2017-08-27 5:05 GMT+08:00 Yubin Ruan <ablacktshirt@gmail.com>:
> On Tue, Aug 22, 2017 at 10:19:48AM +0800, Yubin Ruan wrote:
>> 2017-08-22 8:33 GMT+08:00 Michael Kerrisk (man-pages) <mtk.manpages@gmail.com>:
>> > Hello Yubin
>> >
>> > On 08/20/2017 11:42 AM, Yubin Ruan wrote:
>> >> Hi Michael,
>> >> This patch series introduce three pthread functions for linux-man:
>> >>     * pthread_mutexattr_setrobust()
>> >>     * pthread_mutexattr_getrobust()
>> >>     * pthread_mutex_consistent()
>> >>
>> >> I put pthread_mutexattr_setrobust() and pthread_mutexattr_getrobust()
>> >> altogether since they are related.
>> >>
>> >> I obtain my information about these functions mainly from reading the
>> >> glibc source code and do some testing. There might be some mistake,
>> >> but they are mostly correct (95% or so).
>> >>
>> >> Following this email would be those man pages in plain text and nroff
>> >> format. I have tested those nroff file on my Linux box using man, and
>> >> it look pretty well. Please point out if there are any mistake in
>> >> formatting.
>> >
>> > Thanks for the drafts. Could I ask you to review the man-pages(7)
>> > page. There's a number of details described in that page
>> > which should be fixed in the drafts. Also, please try rendering
>> > your pages with "man -l <page>". This will show problems with
>> > the example code. See the chown(2) man page example for
>> > the required escape sequences for "\n" and the single quote.
>>
>> No problem. I would fix the formatting issues.
>
> Hi Michael,
>
> Sorry that it took so long.
>
> Following is the renew patch. I have corrected several formatting issues (i.e.,
> \n , - , but I can't find anything wrong with ' ) and some typos. The only
> issue I cannot solve is that the new `pthread_mutex_consistent(3)` page is too
> short that it shows at the very bottom... please help with this.
>
> Also, please find attached three verification snippets.
>
>
> Signed-off-by: Yubin Ruan <ablacktshirt@gmail.com>
> ---
>  man3/pthread_mutex_consistent.3    |  60 +++++++++++
>  man3/pthread_mutexattr_setrobust.3 | 199 +++++++++++++++++++++++++++++++++++++
>  2 files changed, 259 insertions(+)
>  create mode 100644 man3/pthread_mutex_consistent.3
>  create mode 100644 man3/pthread_mutexattr_setrobust.3
>
> diff --git a/man3/pthread_mutex_consistent.3 b/man3/pthread_mutex_consistent.3
> new file mode 100644
> index 0000000..6d5bc81
> --- /dev/null
> +++ b/man3/pthread_mutex_consistent.3
> @@ -0,0 +1,60 @@
> +.\" Copyright (c) 2017, Yubin Ruan <ablacktshirt@gmail.com>
> +.\"
> +.\" %%%LICENSE_START(VERBATIM)
> +.\" Permission is granted to make and distribute verbatim copies of this
> +.\" manual provided the copyright notice and this permission notice are
> +.\" preserved on all copies.
> +.\"
> +.\" Permission is granted to copy and distribute modified versions of this
> +.\" manual under the conditions for verbatim copying, provided that the
> +.\" entire resulting derived work is distributed under the terms of a
> +.\" permission notice identical to this one.
> +.\"
> +.\" Since the Linux kernel and libraries are constantly changing, this
> +.\" manual page may be incorrect or out-of-date.  The author(s) assume no
> +.\" responsibility for errors or omissions, or for damages resulting from
> +.\" the use of the information contained herein.  The author(s) may not
> +.\" have taken the same level of care in the production of this manual,
> +.\" which is licensed free of charge, as they might when working
> +.\" professionally.
> +.\"
> +.\" Formatted or processed versions of this manual, if unaccompanied by
> +.\" the source, must acknowledge the copyright and authors of this work.
> +.\" %%%LICENSE_END
> +.\"
> +.TH PTHREAD_MUTEXATTR_SETROBUST 3 2017-08-20 "Linux" "Linux Programmer's Manual"
> +.SH NAME
> +pthread_mutex_consistent \- make the robust mutex consistent
> +.SH SYNOPSIS
> +.nf
> +.B #include <pthread.h>
> +.PP
> +.BI "int pthread_mute_consistent(pthread_mute_t *" mutex ");"
> +.fi
> +.PP
> +Compile and link with \fI\-pthread\fP.
> +.SH DESCRIPTION
> +This function make a robust mutex consistent if it is in a inconsistent
> +state. A mutex can be left in a inconsistent state if its owner terminate
> +while holding the mutex, in which situation the next owner who acquire the
> +mutex will succeed and be notified by the return value of
> +.B EOWNERDEAD.
> +
> +.SH RETURN VALUE
> +On success,
> +.IR pthread_mutex_consistent()
> +return 0. Otherwise an error value is returned to indicate the error.
> +
> +.SH ERRORS
> +.TP
> +.B EINVAL
> +The mutex is either not robust or it is not in a inconsistent state.
> +
> +.SH SEE ALSO
> +.ad l
> +.nh
> +.BR pthread_mutexattr_init (3),
> +.BR pthread_mutex_lock (3),
> +.BR pthread_mutexattr_setrobust (3),
> +.BR pthread_mutexattr_getrobust (3),
> +.BR pthreads (7)
> diff --git a/man3/pthread_mutexattr_setrobust.3 b/man3/pthread_mutexattr_setrobust.3
> new file mode 100644
> index 0000000..3add496
> --- /dev/null
> +++ b/man3/pthread_mutexattr_setrobust.3
> @@ -0,0 +1,199 @@
> +.\" Copyright (c) 2017, Yubin Ruan <ablacktshirt@gmail.com>
> +.\"
> +.\" %%%LICENSE_START(VERBATIM)
> +.\" Permission is granted to make and distribute verbatim copies of this
> +.\" manual provided the copyright notice and this permission notice are
> +.\" preserved on all copies.
> +.\"
> +.\" Permission is granted to copy and distribute modified versions of this
> +.\" manual under the conditions for verbatim copying, provided that the
> +.\" entire resulting derived work is distributed under the terms of a
> +.\" permission notice identical to this one.
> +.\"
> +.\" Since the Linux kernel and libraries are constantly changing, this
> +.\" manual page may be incorrect or out-of-date.  The author(s) assume no
> +.\" responsibility for errors or omissions, or for damages resulting from
> +.\" the use of the information contained herein.  The author(s) may not
> +.\" have taken the same level of care in the production of this manual,
> +.\" which is licensed free of charge, as they might when working
> +.\" professionally.
> +.\"
> +.\" Formatted or processed versions of this manual, if unaccompanied by
> +.\" the source, must acknowledge the copyright and authors of this work.
> +.\" %%%LICENSE_END
> +.\"
> +.TH PTHREAD_MUTEXATTR_SETROBUST 3 2017-08-20 "Linux" "Linux Programmer's Manual"
> +.SH NAME
> +pthread_mutexattr_getrobust, pthread_mutexattr_setrobust,
> +pthread_mutexattr_getrobust_np, pthread_mutexattr_setrobust_np
> +\- get and set the robustness attribute of a mutex attribute object
> +.SH SYNOPSIS
> +.nf
> +.B #include <pthread.h>
> +.PP
> +.BI "int pthread_mutexattr_getrobust(const pthread_mutexattr_t *" attr, "int *" robustness ");"
> +.BI "int pthread_mutexattr_setrobust(const pthread_mutexattr_t *" attr, "int *" robustness ");"
> +.BI "int pthread_mutexattr_getrobust_np(const pthread_mutexattr_t *" attr, "int *" robustness ");"
> +.BI "int pthread_mutexattr_setrobust_np(const pthread_mutexattr_t *" attr, "int *" robustness ");"
> +.fi
> +.PP
> +Compile and link with \fI\-pthread\fP.
> +.SH DESCRIPTION
> +The
> +.BR pthread_mutexattr_getrobust()
> +and
> +.BR pthread_mutexattr_setrobust()
> +functions get and set the robustness attribute of a
> +initialized mutex attribute object, respectively.
> +The robustness attribute specifies the behavior of the mutex
> +when its owner dies without unlocking it. These two
> +functions are specified in POSIX. Glibc's NPTL has
> +.BR pthread_mutexattr_getrobust_np (3)
> +and
> +.BR pthread_mutexattr_setrobust_np (3)
> +respectively but they are just aliases, with the "np" standing for "Native Posix".
> +See
> +.BR NPTL (7).
> +.PP
> +Currently there are only two possible values for the
> +.IR robustness
> +attribute:
> +.TP
> +.BR PTHREAD_MUTEX_STALLED
> +is the default value for a mutex attribute object. If a mutex is initialized
> +with a
> +.BR PTHREAD_MUTEX_STALLED
> +attribute object and its owner dies without unlocking it, it is kept locked
> +afterwards and any future attempts to call
> +.IR pthread_mutex_lock (3)
> +on this mutex will block indefinitely.
> +.TP
> +.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. Usually after
> +.B EOWNERDEAD
> +is returned, the next owner should call
> +.IR pthread_mutex_consistent (3)
> +on the acquired mutex to make it consistent again before using it any further.
> +If the next owner unlock it using
> +.IR pthread_mutex_unlock (3)
> +before making it consistent, the mutex will be unusable permanently and any
> +subsequent attempts to lock it using
> +.IR pthread_mutex_lock (3)
> +will return
> +.B ENOTRECOVERABLE.
> +If the next owner terminates before calling
> +.IR pthread_mutex_consistent (3)
> +, furture
> +.IR pthread_mutex_lock (3)
> +on this mutex will still return
> +.B EOWNERDEAD.
> +
> +
> +Glibc defines
> +.B PTHREAD_MUTEX_STALLED_NP
> +and
> +.B PTHREAD_MUTEX_ROBUST_NP
> +as aliases of
> +.B PTHREAD_MUTEX_STALLED
> +and
> +.B PTHREAD_MUTEX_ROBUST
> +respectively.
> +
> +Note that the
> +.IR attr
> +argument of
> +.IR pthread_mutexattr_getrobust()
> +and
> +.IR pthread_mutexattr_setrobust()
> +should refer to a mutex attribute object that was initialized by
> +.IR pthread_mutexattr_init (3)
> +, otherwise the behavior is undefined.
> +.SH RETURN VALUE
> +On success, zero is returned by
> +.IR pthread_mutexattr_getrobust()
> +and the value pointed to by the
> +.IR robustness
> +parameter is set to the robustness attribute of
> +.IR attr.
> +Otherwise, an error number shall be returned. On success
> +.IR pthread_mutexattr_setrobust()
> +set the robustness attribute into the mutex attribute object
> +.IR attr
> +and return zero, otherwise a error number is returned to indicate the error.
> +.SS Glibc\-specificed features
> +In glibc's implementation,
> +.IR pthread_mutexattr_getrobust()
> +always return zero.
> +.SH ERRORS
> +.TP
> +.B EINVAL
> +A value other than
> +.B PTHREAD_MUTEX_STALLED
> +or
> +.B PTHREAD_MUTEX_ROBUST
> +is passed into
> +.IR pthread_mutexattr_setrobust().
> +
> +.SH EXAMPLE
> +.PP
> +The program demonstrate a simple usecase of the robustness attribute of a
> +pthread mutex attribute object. In this program, a thread holding the mutex
> +dies prematurely withough unlocking the mutex. Another thread acquires it
> +successfully and get EOWNERDEAD.
> +
> +.SS Program source
> +.EX
> +#include <stdio.h>
> +#include <unistd.h>
> +#include <pthread.h>
> +#include <errno.h>
> +
> +pthread_mutex_t lock;
> +
> +void *original_owner_thread(void *ptr)
> +{
> +    printf("[original owner] Setting lock...\\n");
> +    pthread_mutex_lock(&lock);
> +    printf("[original owner] Locked. Now exiting without unlocking.\\n");
> +    pthread_exit(NULL);
> +}
> +
> +int main(int argc, char *argv[])
> +{
> +    pthread_t lock_getter;
> +    pthread_mutexattr_t attr;
> +    pthread_mutexattr_init(&attr);   /* initialize the attribute object */
> +    pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST); /* set robustness */
> +
> +    pthread_mutex_init(&lock, &attr);   /* initialize the lock */
> +
> +    pthread_create(&lock_getter, NULL, original_owner_thread, NULL);
> +    sleep(2);   /* original_owner_thread should have exited now */
> +
> +    printf("Attempting to acquire the unlocked robust mutex.\\n");
> +    int ret_code = pthread_mutex_lock(&lock);
> +    if(EOWNERDEAD == ret_code) {
> +        printf("EOWNERDEAD returned. Make the mutex consistent now\\n");
> +        pthread_mutex_consistent(&lock);
> +    }
> +
> +    pthread_mutex_unlock(&lock);
> +
> +    return 0;
> +}
> +.EE
> +.SH SEE ALSO
> +.ad l
> +.nh
> +.BR pthread_mutex_init (3),
> +.BR pthread_mutex_consistent (3),
> +.BR pthread_mutex_lock (3)
> +.BR pthreads (7)
> --
> 2.7.4
>

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent()
       [not found]               ` <CAJYFCiN-g+GjD8StCLue60i6pOw7jZnRGn2=2znPFkFw_FDyqQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2017-09-11 20:34                 ` Michael Kerrisk (man-pages)
  0 siblings, 0 replies; 16+ messages in thread
From: Michael Kerrisk (man-pages) @ 2017-09-11 20:34 UTC (permalink / raw)
  To: Yubin Ruan; +Cc: linux-man, libc-alpha-9JcytcrH/bA+uJoB2kUjGw

Hi Yubin

On 11 September 2017 at 03:50, Yubin Ruan <ablacktshirt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> just re-sending this to see whether anybody is still interested.

Sorry for the slow response. I'm working on these pages. Give me a few days.

Cheers,

Michael

> Yubin
>
> 2017-08-27 5:05 GMT+08:00 Yubin Ruan <ablacktshirt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:
>> On Tue, Aug 22, 2017 at 10:19:48AM +0800, Yubin Ruan wrote:
>>> 2017-08-22 8:33 GMT+08:00 Michael Kerrisk (man-pages) <mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:
>>> > Hello Yubin
>>> >
>>> > On 08/20/2017 11:42 AM, Yubin Ruan wrote:
>>> >> Hi Michael,
>>> >> This patch series introduce three pthread functions for linux-man:
>>> >>     * pthread_mutexattr_setrobust()
>>> >>     * pthread_mutexattr_getrobust()
>>> >>     * pthread_mutex_consistent()
>>> >>
>>> >> I put pthread_mutexattr_setrobust() and pthread_mutexattr_getrobust()
>>> >> altogether since they are related.
>>> >>
>>> >> I obtain my information about these functions mainly from reading the
>>> >> glibc source code and do some testing. There might be some mistake,
>>> >> but they are mostly correct (95% or so).
>>> >>
>>> >> Following this email would be those man pages in plain text and nroff
>>> >> format. I have tested those nroff file on my Linux box using man, and
>>> >> it look pretty well. Please point out if there are any mistake in
>>> >> formatting.
>>> >
>>> > Thanks for the drafts. Could I ask you to review the man-pages(7)
>>> > page. There's a number of details described in that page
>>> > which should be fixed in the drafts. Also, please try rendering
>>> > your pages with "man -l <page>". This will show problems with
>>> > the example code. See the chown(2) man page example for
>>> > the required escape sequences for "\n" and the single quote.
>>>
>>> No problem. I would fix the formatting issues.
>>
>> Hi Michael,
>>
>> Sorry that it took so long.
>>
>> Following is the renew patch. I have corrected several formatting issues (i.e.,
>> \n , - , but I can't find anything wrong with ' ) and some typos. The only
>> issue I cannot solve is that the new `pthread_mutex_consistent(3)` page is too
>> short that it shows at the very bottom... please help with this.
>>
>> Also, please find attached three verification snippets.
>>
>>
>> Signed-off-by: Yubin Ruan <ablacktshirt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
>> ---
>>  man3/pthread_mutex_consistent.3    |  60 +++++++++++
>>  man3/pthread_mutexattr_setrobust.3 | 199 +++++++++++++++++++++++++++++++++++++
>>  2 files changed, 259 insertions(+)
>>  create mode 100644 man3/pthread_mutex_consistent.3
>>  create mode 100644 man3/pthread_mutexattr_setrobust.3
>>
>> diff --git a/man3/pthread_mutex_consistent.3 b/man3/pthread_mutex_consistent.3
>> new file mode 100644
>> index 0000000..6d5bc81
>> --- /dev/null
>> +++ b/man3/pthread_mutex_consistent.3
>> @@ -0,0 +1,60 @@
>> +.\" Copyright (c) 2017, Yubin Ruan <ablacktshirt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
>> +.\"
>> +.\" %%%LICENSE_START(VERBATIM)
>> +.\" Permission is granted to make and distribute verbatim copies of this
>> +.\" manual provided the copyright notice and this permission notice are
>> +.\" preserved on all copies.
>> +.\"
>> +.\" Permission is granted to copy and distribute modified versions of this
>> +.\" manual under the conditions for verbatim copying, provided that the
>> +.\" entire resulting derived work is distributed under the terms of a
>> +.\" permission notice identical to this one.
>> +.\"
>> +.\" Since the Linux kernel and libraries are constantly changing, this
>> +.\" manual page may be incorrect or out-of-date.  The author(s) assume no
>> +.\" responsibility for errors or omissions, or for damages resulting from
>> +.\" the use of the information contained herein.  The author(s) may not
>> +.\" have taken the same level of care in the production of this manual,
>> +.\" which is licensed free of charge, as they might when working
>> +.\" professionally.
>> +.\"
>> +.\" Formatted or processed versions of this manual, if unaccompanied by
>> +.\" the source, must acknowledge the copyright and authors of this work.
>> +.\" %%%LICENSE_END
>> +.\"
>> +.TH PTHREAD_MUTEXATTR_SETROBUST 3 2017-08-20 "Linux" "Linux Programmer's Manual"
>> +.SH NAME
>> +pthread_mutex_consistent \- make the robust mutex consistent
>> +.SH SYNOPSIS
>> +.nf
>> +.B #include <pthread.h>
>> +.PP
>> +.BI "int pthread_mute_consistent(pthread_mute_t *" mutex ");"
>> +.fi
>> +.PP
>> +Compile and link with \fI\-pthread\fP.
>> +.SH DESCRIPTION
>> +This function make a robust mutex consistent if it is in a inconsistent
>> +state. A mutex can be left in a inconsistent state if its owner terminate
>> +while holding the mutex, in which situation the next owner who acquire the
>> +mutex will succeed and be notified by the return value of
>> +.B EOWNERDEAD.
>> +
>> +.SH RETURN VALUE
>> +On success,
>> +.IR pthread_mutex_consistent()
>> +return 0. Otherwise an error value is returned to indicate the error.
>> +
>> +.SH ERRORS
>> +.TP
>> +.B EINVAL
>> +The mutex is either not robust or it is not in a inconsistent state.
>> +
>> +.SH SEE ALSO
>> +.ad l
>> +.nh
>> +.BR pthread_mutexattr_init (3),
>> +.BR pthread_mutex_lock (3),
>> +.BR pthread_mutexattr_setrobust (3),
>> +.BR pthread_mutexattr_getrobust (3),
>> +.BR pthreads (7)
>> diff --git a/man3/pthread_mutexattr_setrobust.3 b/man3/pthread_mutexattr_setrobust.3
>> new file mode 100644
>> index 0000000..3add496
>> --- /dev/null
>> +++ b/man3/pthread_mutexattr_setrobust.3
>> @@ -0,0 +1,199 @@
>> +.\" Copyright (c) 2017, Yubin Ruan <ablacktshirt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
>> +.\"
>> +.\" %%%LICENSE_START(VERBATIM)
>> +.\" Permission is granted to make and distribute verbatim copies of this
>> +.\" manual provided the copyright notice and this permission notice are
>> +.\" preserved on all copies.
>> +.\"
>> +.\" Permission is granted to copy and distribute modified versions of this
>> +.\" manual under the conditions for verbatim copying, provided that the
>> +.\" entire resulting derived work is distributed under the terms of a
>> +.\" permission notice identical to this one.
>> +.\"
>> +.\" Since the Linux kernel and libraries are constantly changing, this
>> +.\" manual page may be incorrect or out-of-date.  The author(s) assume no
>> +.\" responsibility for errors or omissions, or for damages resulting from
>> +.\" the use of the information contained herein.  The author(s) may not
>> +.\" have taken the same level of care in the production of this manual,
>> +.\" which is licensed free of charge, as they might when working
>> +.\" professionally.
>> +.\"
>> +.\" Formatted or processed versions of this manual, if unaccompanied by
>> +.\" the source, must acknowledge the copyright and authors of this work.
>> +.\" %%%LICENSE_END
>> +.\"
>> +.TH PTHREAD_MUTEXATTR_SETROBUST 3 2017-08-20 "Linux" "Linux Programmer's Manual"
>> +.SH NAME
>> +pthread_mutexattr_getrobust, pthread_mutexattr_setrobust,
>> +pthread_mutexattr_getrobust_np, pthread_mutexattr_setrobust_np
>> +\- get and set the robustness attribute of a mutex attribute object
>> +.SH SYNOPSIS
>> +.nf
>> +.B #include <pthread.h>
>> +.PP
>> +.BI "int pthread_mutexattr_getrobust(const pthread_mutexattr_t *" attr, "int *" robustness ");"
>> +.BI "int pthread_mutexattr_setrobust(const pthread_mutexattr_t *" attr, "int *" robustness ");"
>> +.BI "int pthread_mutexattr_getrobust_np(const pthread_mutexattr_t *" attr, "int *" robustness ");"
>> +.BI "int pthread_mutexattr_setrobust_np(const pthread_mutexattr_t *" attr, "int *" robustness ");"
>> +.fi
>> +.PP
>> +Compile and link with \fI\-pthread\fP.
>> +.SH DESCRIPTION
>> +The
>> +.BR pthread_mutexattr_getrobust()
>> +and
>> +.BR pthread_mutexattr_setrobust()
>> +functions get and set the robustness attribute of a
>> +initialized mutex attribute object, respectively.
>> +The robustness attribute specifies the behavior of the mutex
>> +when its owner dies without unlocking it. These two
>> +functions are specified in POSIX. Glibc's NPTL has
>> +.BR pthread_mutexattr_getrobust_np (3)
>> +and
>> +.BR pthread_mutexattr_setrobust_np (3)
>> +respectively but they are just aliases, with the "np" standing for "Native Posix".
>> +See
>> +.BR NPTL (7).
>> +.PP
>> +Currently there are only two possible values for the
>> +.IR robustness
>> +attribute:
>> +.TP
>> +.BR PTHREAD_MUTEX_STALLED
>> +is the default value for a mutex attribute object. If a mutex is initialized
>> +with a
>> +.BR PTHREAD_MUTEX_STALLED
>> +attribute object and its owner dies without unlocking it, it is kept locked
>> +afterwards and any future attempts to call
>> +.IR pthread_mutex_lock (3)
>> +on this mutex will block indefinitely.
>> +.TP
>> +.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. Usually after
>> +.B EOWNERDEAD
>> +is returned, the next owner should call
>> +.IR pthread_mutex_consistent (3)
>> +on the acquired mutex to make it consistent again before using it any further.
>> +If the next owner unlock it using
>> +.IR pthread_mutex_unlock (3)
>> +before making it consistent, the mutex will be unusable permanently and any
>> +subsequent attempts to lock it using
>> +.IR pthread_mutex_lock (3)
>> +will return
>> +.B ENOTRECOVERABLE.
>> +If the next owner terminates before calling
>> +.IR pthread_mutex_consistent (3)
>> +, furture
>> +.IR pthread_mutex_lock (3)
>> +on this mutex will still return
>> +.B EOWNERDEAD.
>> +
>> +
>> +Glibc defines
>> +.B PTHREAD_MUTEX_STALLED_NP
>> +and
>> +.B PTHREAD_MUTEX_ROBUST_NP
>> +as aliases of
>> +.B PTHREAD_MUTEX_STALLED
>> +and
>> +.B PTHREAD_MUTEX_ROBUST
>> +respectively.
>> +
>> +Note that the
>> +.IR attr
>> +argument of
>> +.IR pthread_mutexattr_getrobust()
>> +and
>> +.IR pthread_mutexattr_setrobust()
>> +should refer to a mutex attribute object that was initialized by
>> +.IR pthread_mutexattr_init (3)
>> +, otherwise the behavior is undefined.
>> +.SH RETURN VALUE
>> +On success, zero is returned by
>> +.IR pthread_mutexattr_getrobust()
>> +and the value pointed to by the
>> +.IR robustness
>> +parameter is set to the robustness attribute of
>> +.IR attr.
>> +Otherwise, an error number shall be returned. On success
>> +.IR pthread_mutexattr_setrobust()
>> +set the robustness attribute into the mutex attribute object
>> +.IR attr
>> +and return zero, otherwise a error number is returned to indicate the error.
>> +.SS Glibc\-specificed features
>> +In glibc's implementation,
>> +.IR pthread_mutexattr_getrobust()
>> +always return zero.
>> +.SH ERRORS
>> +.TP
>> +.B EINVAL
>> +A value other than
>> +.B PTHREAD_MUTEX_STALLED
>> +or
>> +.B PTHREAD_MUTEX_ROBUST
>> +is passed into
>> +.IR pthread_mutexattr_setrobust().
>> +
>> +.SH EXAMPLE
>> +.PP
>> +The program demonstrate a simple usecase of the robustness attribute of a
>> +pthread mutex attribute object. In this program, a thread holding the mutex
>> +dies prematurely withough unlocking the mutex. Another thread acquires it
>> +successfully and get EOWNERDEAD.
>> +
>> +.SS Program source
>> +.EX
>> +#include <stdio.h>
>> +#include <unistd.h>
>> +#include <pthread.h>
>> +#include <errno.h>
>> +
>> +pthread_mutex_t lock;
>> +
>> +void *original_owner_thread(void *ptr)
>> +{
>> +    printf("[original owner] Setting lock...\\n");
>> +    pthread_mutex_lock(&lock);
>> +    printf("[original owner] Locked. Now exiting without unlocking.\\n");
>> +    pthread_exit(NULL);
>> +}
>> +
>> +int main(int argc, char *argv[])
>> +{
>> +    pthread_t lock_getter;
>> +    pthread_mutexattr_t attr;
>> +    pthread_mutexattr_init(&attr);   /* initialize the attribute object */
>> +    pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST); /* set robustness */
>> +
>> +    pthread_mutex_init(&lock, &attr);   /* initialize the lock */
>> +
>> +    pthread_create(&lock_getter, NULL, original_owner_thread, NULL);
>> +    sleep(2);   /* original_owner_thread should have exited now */
>> +
>> +    printf("Attempting to acquire the unlocked robust mutex.\\n");
>> +    int ret_code = pthread_mutex_lock(&lock);
>> +    if(EOWNERDEAD == ret_code) {
>> +        printf("EOWNERDEAD returned. Make the mutex consistent now\\n");
>> +        pthread_mutex_consistent(&lock);
>> +    }
>> +
>> +    pthread_mutex_unlock(&lock);
>> +
>> +    return 0;
>> +}
>> +.EE
>> +.SH SEE ALSO
>> +.ad l
>> +.nh
>> +.BR pthread_mutex_init (3),
>> +.BR pthread_mutex_consistent (3),
>> +.BR pthread_mutex_lock (3)
>> +.BR pthreads (7)
>> --
>> 2.7.4
>>



-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent()
       [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
  2017-09-13 15:00               ` Michael Kerrisk (man-opages)
  1 sibling, 1 reply; 16+ messages in thread
From: Michael Kerrisk (man-pages) @ 2017-09-12 12:41 UTC (permalink / raw)
  To: Yubin Ruan
  Cc: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w,
	linux-man-u79uwXL29TY76Z2rM5mHXA,
	libc-alpha-9JcytcrH/bA+uJoB2kUjGw

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.

Cheers,

Michael

-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent()
       [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)
  1 sibling, 0 replies; 16+ messages in thread
From: Yubin Ruan @ 2017-09-13  4:09 UTC (permalink / raw)
  To: Michael Kerrisk (man-pages)
  Cc: linux-man-u79uwXL29TY76Z2rM5mHXA, libc-alpha-9JcytcrH/bA+uJoB2kUjGw

2017-09-13 16:34 GMT+08:00 Yubin Ruan <ablacktshirt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:
> 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);

Note that execve(/bin/cat) here is on purpose: it "suspends" the
parent so that the child will not be killed because parent dies, such
that the child have enough time to check whether pthread_mutex_lock(3)
will return EOWNERDEAD. So, after executing this program, wait 2
seconds before pressing your keyboard ;-)

Yubin

>     }
>     return 0;
> }
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent()
  2017-09-12 12:41               ` Michael Kerrisk (man-pages)
@ 2017-09-13  8:34                 ` Yubin Ruan
       [not found]                   ` <20170913083446.GA16265-BUpDV9vzqx74NUO8LpiZZi+1pv2Z1Xu3@public.gmane.org>
  0 siblings, 1 reply; 16+ messages in thread
From: Yubin Ruan @ 2017-09-13  8:34 UTC (permalink / raw)
  To: Michael Kerrisk (man-pages); +Cc: linux-man, libc-alpha

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;
}

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent()
       [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>
  1 sibling, 1 reply; 16+ messages in thread
From: Michael Kerrisk (man-pages) @ 2017-09-13 12:28 UTC (permalink / raw)
  To: Yubin Ruan; +Cc: linux-man, libc-alpha-9JcytcrH/bA+uJoB2kUjGw

Hello Yubin,

On 13 September 2017 at 10:34, Yubin Ruan <ablacktshirt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> 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...).

Thanks for the details and example program.

So, I see the kernel code that deals with this now: in
fs/exec.c::exec_mmap(), there is a call to mm_release() which in turn
calls exit_robust_list()

So, this detail isn't in POSIX. I think what I will do is move
discussion of that point into the NOTES.

> 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.

It also appears not to be true on Linux, at least in some short tests
I just did.[1] See my modified version of your program, below. Also
when looking at the kernel code, I can see no call to mm_release()
that would lead to this behavior.

Cheers,

Michael

[1] And I wonder if the Solaris documentation is even correct, since,
if we are talking about a shared anonymous mapping, the memory would
not be released until *all* processes have unmapped the memory

/************ verify-execve.c *****************/
#define _POSIX_C_SOURCE 200809L
#define _GNU_SOURCE
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#define MEMSIZE 4096

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

int main(int argc, char *argv[]) {
    struct shm *shm = NULL;
    int ret_code = 0;
    pthread_mutex_t *mutexp = NULL;
    pthread_mutexattr_t attr;
    pid_t pid = 0;

    shm = mmap(NULL, MEMSIZE, PROT_READ | PROT_WRITE,
               MAP_ANONYMOUS | MAP_SHARED,-1, 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);

    pid = fork();
    if (0 == pid) {
        sleep(1);
        printf("Child about to call pthread_mutex-Lock()\n");
        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 {
        ret_code = pthread_mutex_lock(mutexp);
        if (0 == ret_code) {
            printf("parent successfully acquired the lock\n");
        } else {
            ERROR_ON(pthread_mutex_lock);
            return -1;
        }

        printf("Parent sleeping 3 seconds\n");
        sleep(3);
        if (munmap(shm, MEMSIZE) == -1)
            ERROR_ON(munmap);
        printf("Parent has unmapped the memory\n");
        sleep(1);
        printf("Parent about to exit\n");
        exit(0);
/*
        printf("parent going to execve(/bin/true)\n");
        execl("/bin/true", "true", (char *) NULL);
*/
    }
    return 0;
}
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent()
       [not found]             ` <20170826210528.GA32472-BUpDV9vzqx74NUO8LpiZZi+1pv2Z1Xu3@public.gmane.org>
  2017-09-12 12:41               ` Michael Kerrisk (man-pages)
@ 2017-09-13 15:00               ` Michael Kerrisk (man-opages)
       [not found]                 ` <1159fa2e-006e-d3dd-7d1b-0e055cad42b5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
  1 sibling, 1 reply; 16+ messages in thread
From: Michael Kerrisk (man-opages) @ 2017-09-13 15:00 UTC (permalink / raw)
  To: Yubin Ruan
  Cc: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w,
	linux-man-u79uwXL29TY76Z2rM5mHXA,
	libc-alpha-9JcytcrH/bA+uJoB2kUjGw

Hi Yubin

> Hi Michael,
> 
> Sorry that it took so long.
> 
> Following is the renew patch. I have corrected several formatting issues (i.e.,
> \n , - , but I can't find anything wrong with ' ) and some typos. The only
> issue I cannot solve is that the new `pthread_mutex_consistent(3)` page is too
> short that it shows at the very bottom... please help with this.
> 
> Also, please find attached three verification snippets.

I've done some heavy editing, and added quite a few pieces to these 
pages, and pushed the results to Git. Go take a look and let me know
of anything you think needs fixing. Thanks for these pages!

Cheers,

Michael
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent()
       [not found]                     ` <20170915094414.GA18043-BUpDV9vzqx74NUO8LpiZZi+1pv2Z1Xu3@public.gmane.org>
@ 2017-09-15  7:53                       ` Michael Kerrisk (man-pages)
  0 siblings, 0 replies; 16+ messages in thread
From: Michael Kerrisk (man-pages) @ 2017-09-15  7:53 UTC (permalink / raw)
  To: Yubin Ruan
  Cc: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w,
	linux-man-u79uwXL29TY76Z2rM5mHXA,
	libc-alpha-9JcytcrH/bA+uJoB2kUjGw

On 09/15/2017 11:45 AM, Yubin Ruan wrote:
> On Wed, Sep 13, 2017 at 05:00:11PM +0200, Michael Kerrisk (man-opages) wrote:
>> Hi Yubin
>>
>>> Hi Michael,
>>>
>>> Sorry that it took so long.
>>>
>>> Following is the renew patch. I have corrected several formatting issues (i.e.,
>>> \n , - , but I can't find anything wrong with ' ) and some typos. The only
>>> issue I cannot solve is that the new `pthread_mutex_consistent(3)` page is too
>>> short that it shows at the very bottom... please help with this.
>>>
>>> Also, please find attached three verification snippets.
>>
>> I've done some heavy editing, and added quite a few pieces to these pages,
>> and pushed the results to Git. Go take a look and let me know
>> of anything you think needs fixing. Thanks for these pages!
> 
> Good job. No objection, except a minor change and some code formatting change.
> See the diff below.

Thanks, Yubin. Applied.

Cheers,

Michael

> diff --git a/man3/pthread_mutexattr_setrobust.3 b/man3/pthread_mutexattr_setrobust.3
> index e9e8e76..66b4bed 100644
> --- a/man3/pthread_mutexattr_setrobust.3
> +++ b/man3/pthread_mutexattr_setrobust.3
> @@ -86,7 +86,7 @@ on the mutex will block indefinitely.
>  .TP
>  .B PTHREAD_MUTEX_ROBUST
>  If a mutex is initialized with the
> -.BR PTHREAD_MUTEX_ROBUST ,
> +.BR PTHREAD_MUTEX_ROBUST
>  attribute and its owner dies without unlocking it,
>  any future attempts to call
>  .BR pthread_mutex_lock (3)
> @@ -255,22 +255,21 @@ main(int argc, char *argv[])
>      s = pthread_mutex_lock(&mtx);
>      if (s == EOWNERDEAD) {
>          printf("[main thread] pthread_mutex_lock() returned EOWNERDEAD\\n");
> -	printf("[main thread] Now make the mutex consistent\\n");
> +        printf("[main thread] Now make the mutex consistent\\n");
>          s = pthread_mutex_consistent(&mtx);
>          if (s != 0)
> -	    handle_error_en(s, "pthread_mutex_consistent");
> -	printf("[main thread] Mutex is now consistent; unlocking\\n");
> +            handle_error_en(s, "pthread_mutex_consistent");
> +        printf("[main thread] Mutex is now consistent; unlocking\\n");
>          s = pthread_mutex_unlock(&mtx);
>          if (s != 0)
> -	    handle_error_en(s, "pthread_mutex_unlock");
> -
> +            handle_error_en(s, "pthread_mutex_unlock");
>          exit(EXIT_SUCCESS);
>      } else if (s == 0) {
> -	printf("[main thread] pthread_mutex_lock() unexpectedly succeeded\\n");
> -	exit(EXIT_FAILURE);
> +        printf("[main thread] pthread_mutex_lock() unexpectedly succeeded\\n");
> +        exit(EXIT_FAILURE);
>      } else {
> -	printf("[main thread] pthread_mutex_lock() unexpectedly failed\\n");
> -	handle_error_en(s, "pthread_mutex_lock");
> +        printf("[main thread] pthread_mutex_lock() unexpectedly failed\\n");
> +        handle_error_en(s, "pthread_mutex_lock");
>      }
>  }
> 
> 


-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent()
       [not found]                       ` <CAKgNAkh4iAkz8oL70S3dzztF_oZy+fXohS4OZ2nyySAaAMGX3w-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2017-09-15  8:29                         ` Yubin Ruan
  0 siblings, 0 replies; 16+ messages in thread
From: Yubin Ruan @ 2017-09-15  8:29 UTC (permalink / raw)
  To: Michael Kerrisk (man-pages); +Cc: linux-man, libc-alpha-9JcytcrH/bA+uJoB2kUjGw

On Wed, Sep 13, 2017 at 02:28:10PM +0200, Michael Kerrisk (man-pages) wrote:
> Hello Yubin,
> 
> On 13 September 2017 at 10:34, Yubin Ruan <ablacktshirt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > 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...).
> 
> Thanks for the details and example program.
> 
> So, I see the kernel code that deals with this now: in
> fs/exec.c::exec_mmap(), there is a call to mm_release() which in turn
> calls exit_robust_list()
> 
> So, this detail isn't in POSIX. I think what I will do is move
> discussion of that point into the NOTES.
> 
> > 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.
> 
> It also appears not to be true on Linux, at least in some short tests
> I just did.[1] See my modified version of your program, below. Also
> when looking at the kernel code, I can see no call to mm_release()
> that would lead to this behavior.

[snip]
>
> 
> [1] And I wonder if the Solaris documentation is even correct, since,
> if we are talking about a shared anonymous mapping, the memory would
> not be released until *all* processes have unmapped the memory

Yes you are right. The lock will not be unlocked automatically. Instead, it
will deadlock if the child try to lock it. See the modified program
below (your snippet contains some logical problems, so I modified it a little
bit)

Yubin
 
/************ verify-execve.c *****************/
#define _POSIX_C_SOURCE 200809L
#define _GNU_SOURCE
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#define MEMSIZE 4096

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

int main(int argc, char *argv[]) {
    struct shm *shm = NULL;
    int ret_code = 0;
    pthread_mutex_t *mutexp = NULL;
    pthread_mutexattr_t attr;
    int status = -1;
    pid_t pid = 0;

    shm = mmap(NULL, MEMSIZE, PROT_READ | PROT_WRITE,
               MAP_ANONYMOUS | MAP_SHARED,-1, 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);

    pid = fork();
    if (0 == pid) {
        sleep(3);
        printf("Child about to call pthread_mutex-Lock()\n");
        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 {
        ret_code = pthread_mutex_lock(mutexp);
        if (0 == ret_code) {
            printf("parent successfully acquired the lock\n");
        } else {
            ERROR_ON(pthread_mutex_lock);
            return -1;
        }

        if (munmap(shm, MEMSIZE) == -1)
            ERROR_ON(munmap);
        printf("Parent has unmapped the memory, waiting for its child\n");
        wait(&status);
        printf("Parent about to exit\n");
        exit(0);
    }
    return 0;
}
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent()
       [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>
  0 siblings, 1 reply; 16+ messages in thread
From: Yubin Ruan @ 2017-09-15  9:45 UTC (permalink / raw)
  To: Michael Kerrisk (man-opages)
  Cc: linux-man-u79uwXL29TY76Z2rM5mHXA, libc-alpha-9JcytcrH/bA+uJoB2kUjGw

On Wed, Sep 13, 2017 at 05:00:11PM +0200, Michael Kerrisk (man-opages) wrote:
> Hi Yubin
> 
> >Hi Michael,
> >
> >Sorry that it took so long.
> >
> >Following is the renew patch. I have corrected several formatting issues (i.e.,
> >\n , - , but I can't find anything wrong with ' ) and some typos. The only
> >issue I cannot solve is that the new `pthread_mutex_consistent(3)` page is too
> >short that it shows at the very bottom... please help with this.
> >
> >Also, please find attached three verification snippets.
> 
> I've done some heavy editing, and added quite a few pieces to these pages,
> and pushed the results to Git. Go take a look and let me know
> of anything you think needs fixing. Thanks for these pages!

Good job. No objection, except a minor change and some code formatting change.
See the diff below.

Yubin

diff --git a/man3/pthread_mutexattr_setrobust.3 b/man3/pthread_mutexattr_setrobust.3
index e9e8e76..66b4bed 100644
--- a/man3/pthread_mutexattr_setrobust.3
+++ b/man3/pthread_mutexattr_setrobust.3
@@ -86,7 +86,7 @@ on the mutex will block indefinitely.
 .TP
 .B PTHREAD_MUTEX_ROBUST
 If a mutex is initialized with the
-.BR PTHREAD_MUTEX_ROBUST ,
+.BR PTHREAD_MUTEX_ROBUST
 attribute and its owner dies without unlocking it,
 any future attempts to call
 .BR pthread_mutex_lock (3)
@@ -255,22 +255,21 @@ main(int argc, char *argv[])
     s = pthread_mutex_lock(&mtx);
     if (s == EOWNERDEAD) {
         printf("[main thread] pthread_mutex_lock() returned EOWNERDEAD\\n");
-	printf("[main thread] Now make the mutex consistent\\n");
+        printf("[main thread] Now make the mutex consistent\\n");
         s = pthread_mutex_consistent(&mtx);
         if (s != 0)
-	    handle_error_en(s, "pthread_mutex_consistent");
-	printf("[main thread] Mutex is now consistent; unlocking\\n");
+            handle_error_en(s, "pthread_mutex_consistent");
+        printf("[main thread] Mutex is now consistent; unlocking\\n");
         s = pthread_mutex_unlock(&mtx);
         if (s != 0)
-	    handle_error_en(s, "pthread_mutex_unlock");

^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2017-09-15  9:45 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
     [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)

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.