From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 94728 invoked by alias); 13 Sep 2017 01:39:26 -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 94717 invoked by uid 89); 13 Sep 2017 01:39:25 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=0.5 required=5.0 tests=BAYES_00,DATE_IN_FUTURE_06_12,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RCVD_IN_SORBS_SPAM,SPF_PASS autolearn=no version=3.3.2 spammy=H*M:internal, Hx-languages-length:3198 X-HELO: mail-pf0-f196.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=J+hkWKphBP7jPABMR5wUelYRhGYD2bYugKIn1BtCXEA=; b=SqrPbpsLxZdlGdx0ehZVG1I0w7Vl0Za9ofFy9xapDNi1PpPHtfR86u4xduB02RdJfq cx4ZN5kZQ2mCTAI/R5P/JgCAFs7MmTwYD/g6RNaWYGywvu4KZYGY+Jm/qBN0LzdsAh7b PXfmkUhRdqREjXi25SU7EWkuvbMGcismYJ0Z9bEozAQhIRwLbMWSsiPGAdidtftofRSH RcDkZaOBptxzZrLiHdbBnEVgg4lCjmv0ZVG23P05PQe1pZKZ1XGr6aebZXPbIAiYCPsO v71GBu5WcX43qdZ5gQ2mJUuNjtNI0juJ0KUJf+AByzs6+VeAnz4D4e4BapwAES4NLHCW uBgw== X-Gm-Message-State: AHPjjUiyDlyimFej1b+K4x/dOvHoHrjHV5ziABDZr6QFL9Qtyjeojadb vXSTr3f72vp+Hg== X-Google-Smtp-Source: ADKCNb7vWOdS9GLX62S2WGOcFNxrjDNhNvosQ57UZYlD3xZIjHnCtD41s+kR8RuE5OrYu0COBNefNg== X-Received: by 10.99.116.90 with SMTP id e26mr16427675pgn.290.1505266762747; Tue, 12 Sep 2017 18:39:22 -0700 (PDT) Date: Wed, 13 Sep 2017 01:39:00 -0000 From: Yubin Ruan To: "Michael Kerrisk (man-pages)" Cc: linux-man@vger.kernel.org, libc-alpha@sourceware.org Subject: Re: [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent() Message-ID: <20170913083446.GA16265@HP.internal.baidu.com> References: <36ab9ec0-b496-c007-c12f-065fd618e7fd@gmail.com> <20170826210528.GA32472@HP.internal.baidu.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) X-SW-Source: 2017-09/txt/msg00533.txt.bz2 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); } return 0; }