From patchwork Tue Nov 12 16:20:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Adhemerval Zanella (Code Review)" X-Patchwork-Id: 1193676 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-106942-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gnutoolchain-gerrit.osci.io Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="RWRSIq8l"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47CCfG5nTbz9sPL for ; Wed, 13 Nov 2019 03:20:54 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:date:from:to:cc:subject:in-reply-to:references :reply-to:mime-version:content-transfer-encoding:content-type :message-id; q=dns; s=default; b=I1O3opCX3he7Ju66mwIbmmjO7kCeGwE b9WxqdCZc/tC3Of7wlAQamZu9oSdFXNgOyLlE9RPscFkAU5MauZCUUWCgKQWPYI5 cF4ivQUiENwMf+F00uBKBq9zY/jijan+k/KQA5zVtDDIRopwcPUO+9sADQCQfUnU 7nTekxUfh1IM= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:date:from:to:cc:subject:in-reply-to:references :reply-to:mime-version:content-transfer-encoding:content-type :message-id; s=default; bh=mvUe+7nEQ1t/Pjk5Sv+4hVcC/jg=; b=RWRSI q8lNW82MGp1ePWkSiHnn2od3s9jgnUxNkqMpOcqeLSX6kaetWQlRKjZt/Zv5b5Rx 4wttIRO7RJCM6k2tBc7A677j/iOOuePsRmBmd5InE6bDVMUlAM4qpiKV3cUS/H/d kdMXslyovZ4Zk0GnlioefWwBOQ8i2dOIn/FoIQ= Received: (qmail 97277 invoked by alias); 12 Nov 2019 16:20:18 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 97224 invoked by uid 89); 12 Nov 2019 16:20:18 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3 autolearn=ham version=3.3.1 spammy= X-HELO: mx1.osci.io X-Gerrit-PatchSet: 2 Date: Tue, 12 Nov 2019 11:20:12 -0500 From: "Sourceware to Gerrit sync (Code Review)" To: Florian Weimer , libc-alpha@sourceware.org Cc: Carlos O'Donell Auto-Submitted: auto-generated X-Gerrit-MessageType: merged Subject: [pushed] login: Introduce matches_last_entry to utmp processing X-Gerrit-Change-Id: Ib8a85002f7f87ee41590846d16d7e52bdb82f5a5 X-Gerrit-Change-Number: 614 X-Gerrit-ChangeURL: X-Gerrit-Commit: 76a7c103eb9060f9e3ba01d073ae4621a17d8b46 In-Reply-To: References: Reply-To: noreply@gnutoolchain-gerrit.osci.io, fweimer@redhat.com, libc-alpha@sourceware.org, carlos@redhat.com MIME-Version: 1.0 Content-Disposition: inline User-Agent: Gerrit/3.0.3-76-gf8b6da0ab5 Message-Id: <20191112162012.E216F20AF6@gnutoolchain-gerrit.osci.io> Sourceware to Gerrit sync has submitted this change. Change URL: https://gnutoolchain-gerrit.osci.io/r/c/glibc/+/614 ...................................................................... login: Introduce matches_last_entry to utmp processing This simplifies internal_getut_nolock and fixes a regression, introduced in commit be6b16d975683e6cca57852cd4cfe715b2a9d8b1 ("login: Acquire write lock early in pututline [BZ #24882]") in pututxline because __utmp_equal can only compare process-related utmp entries. Fixes: be6b16d975683e6cca57852cd4cfe715b2a9d8b1 Change-Id: Ib8a85002f7f87ee41590846d16d7e52bdb82f5a5 --- M login/utmp_file.c 1 file changed, 31 insertions(+), 49 deletions(-) Approvals: Carlos O'Donell: Looks good to me, approved diff --git a/login/utmp_file.c b/login/utmp_file.c index 917c4c5..c41d17b 100644 --- a/login/utmp_file.c +++ b/login/utmp_file.c @@ -43,6 +43,25 @@ /* Cache for the last read entry. */ static struct utmp last_entry; +/* Returns true if *ENTRY matches last_entry, based on + data->ut_type. */ +static bool +matches_last_entry (const struct utmp *data) +{ + if (file_offset <= 0) + /* Nothing has been read. last_entry is stale and cannot match. */ + return false; + + if (data->ut_type == RUN_LVL + || data->ut_type == BOOT_TIME + || data->ut_type == OLD_TIME + || data->ut_type == NEW_TIME) + /* For some entry types, only a type match is required. */ + return data->ut_type == last_entry.ut_type; + else + /* For the process-related entries, a full match is needed. */ + return __utmp_equal (&last_entry, data); +} /* Locking timeout. */ #ifndef TIMEOUT @@ -133,9 +152,6 @@ __lseek64 (file_fd, 0, SEEK_SET); file_offset = 0; - /* Make sure the entry won't match. */ - last_entry.ut_type = -1; - return 1; } @@ -191,48 +207,20 @@ static int internal_getut_nolock (const struct utmp *id) { - if (id->ut_type == RUN_LVL || id->ut_type == BOOT_TIME - || id->ut_type == OLD_TIME || id->ut_type == NEW_TIME) + while (1) { - /* Search for next entry with type RUN_LVL, BOOT_TIME, - OLD_TIME, or NEW_TIME. */ - - while (1) + /* Read the next entry. */ + if (__read_nocancel (file_fd, &last_entry, sizeof (struct utmp)) + != sizeof (struct utmp)) { - /* Read the next entry. */ - if (__read_nocancel (file_fd, &last_entry, sizeof (struct utmp)) - != sizeof (struct utmp)) - { - __set_errno (ESRCH); - file_offset = -1l; - return -1; - } - file_offset += sizeof (struct utmp); - - if (id->ut_type == last_entry.ut_type) - break; + __set_errno (ESRCH); + file_offset = -1l; + return -1; } - } - else - { - /* Search for the next entry with the specified ID and with type - INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS. */ + file_offset += sizeof (struct utmp); - while (1) - { - /* Read the next entry. */ - if (__read_nocancel (file_fd, &last_entry, sizeof (struct utmp)) - != sizeof (struct utmp)) - { - __set_errno (ESRCH); - file_offset = -1l; - return -1; - } - file_offset += sizeof (struct utmp); - - if (__utmp_equal (&last_entry, id)) - break; - } + if (matches_last_entry (id)) + break; } return 0; @@ -365,13 +353,7 @@ /* Find the correct place to insert the data. */ bool found = false; - if (file_offset > 0 - && ((last_entry.ut_type == data->ut_type - && (last_entry.ut_type == RUN_LVL - || last_entry.ut_type == BOOT_TIME - || last_entry.ut_type == OLD_TIME - || last_entry.ut_type == NEW_TIME)) - || __utmp_equal (&last_entry, data))) + if (matches_last_entry (data)) { if (__lseek64 (file_fd, file_offset, SEEK_SET) < 0) { @@ -389,7 +371,7 @@ found = false; } else - found = __utmp_equal (&last_entry, data); + found = matches_last_entry (data); } if (!found)