From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 82768 invoked by alias); 13 Sep 2017 04:09:37 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Received: (qmail 82756 invoked by uid 89); 13 Sep 2017 04:09:37 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.6 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RCVD_IN_SORBS_SPAM,SPF_PASS autolearn=no version=3.3.2 spammy=H*RU:74.125.82.65, Hx-spam-relays-external:74.125.82.65, pressing X-HELO: mail-wm0-f65.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=NT24jIopApjruftQoyExykYFd/AltvQElJkgZECweQE=; b=VSJ1XE+qLAx8h2e7yIpEeSDnb1lhOVCIwuu9TLyhFmfkuK0w/djWbUlDei2jHIjQbl BL3L8Rn08kO37EheccWQAuHXJ3TRLFOeaYvg0HAw61cZIGTCIQXfEseQVMNSXgBrQhh1 pb3JZmiaI+XZCC7IuGbiER40jgty8hZJwrBeZ+r3yz4MFyx1t0mxScv1uXHJLTen8+i5 qHcbbHJMENQFBNZqa09FDQWnne/HRnBSl/7MzUsJArFvqfdhTeqFx6NYGJDqOh3yGIkL DNJdqgv24S39+NGL+2XBp9qIGnlZjXtc41y2oxI+faM7G5opAO/ee8Tn1v7UCcPw6Od9 m/+g== X-Gm-Message-State: AHPjjUjEBEzR3F/vrr3L6vg1jtoDxYM69NlgyuoArLTIsMvCc2rsRwD6 RdMTffwLPxAjYXoxegtEBkzEurFwK3O8jNlglQ== X-Google-Smtp-Source: ADKCNb6aCwrFBWfYbVLoq/sRstXo8gzmDvrqMjyHmtNrs5w7QfXGRKuW9/E3DvVsjkHkgVtPctAagsah5QUIxeCjcW8= X-Received: by 10.80.137.70 with SMTP id f6mr8571403edf.194.1505275773698; Tue, 12 Sep 2017 21:09:33 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <20170913083446.GA16265@HP.internal.baidu.com> References: <36ab9ec0-b496-c007-c12f-065fd618e7fd@gmail.com> <20170826210528.GA32472@HP.internal.baidu.com> <20170913083446.GA16265@HP.internal.baidu.com> From: Yubin Ruan Date: Wed, 13 Sep 2017 04:09:00 -0000 Message-ID: Subject: Re: [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent() To: "Michael Kerrisk (man-pages)" Cc: linux-man@vger.kernel.org, libc-alpha@sourceware.org Content-Type: text/plain; charset="UTF-8" X-SW-Source: 2017-09/txt/msg00534.txt.bz2 2017-09-13 16:34 GMT+08:00 Yubin Ruan : > 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 > #include > #include > #include > #include > #include > #include > #include > #include > #include > > #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; > }