Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp964044rda; Sun, 22 Oct 2023 19:12:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHpLStM1u04U+Rc8L89bnTpL5aUPwpYtyQDn0t6dXXbMOiYvLDG2iaCmWN/TEzsCLP0Jc1U X-Received: by 2002:a05:6808:f0e:b0:3b2:e5b5:b48a with SMTP id m14-20020a0568080f0e00b003b2e5b5b48amr10406630oiw.11.1698027127967; Sun, 22 Oct 2023 19:12:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698027127; cv=none; d=google.com; s=arc-20160816; b=B/Uw/a/QNDFavuM8IFoOKKi53XlIkg0BK2K0i9Jdtwfy950TVnslgZBousMOQ0JCZy 6zI3I99BSNkzqCbz9zaZ50QN8Egd0Z+EQBoZMcpMJ8Ob3g2xl7z5Ni4XY2sE6y5OzQ0Q FHWHl0SL9TZ+aBvKRDlQe3fAUO8ern9a6OPnvVlL7UDG6BHxi30L48INVndjVZ1NPtET LUo72iViZZv31NDknDsCe7GMdWVdQ/LB3oPfBuiVcF9fYkcAm8VrnyCMmJZGgAtzc1Ie ST3o9DLhjHFQNaQHfSEafdnl5+P9Yf5GqQKwaIZf/rQ9YzrqBHw/wzffd3f07mo4Dtrn Dmxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-signature; bh=k+tJrpWVDJJ7KETSa91IT4Yko+pzXy4M/FqN16u/imA=; fh=7qEnEX/LkcfVW+x9QL9Kl5KXgIktTChUzdd7gz02GxQ=; b=j/elidzkiG2VqOCw4PgmaULGll0LmN6rxFHq3qjREe//s5o5pS5pxdEkE5+IH/LRPC 0YdyfFlri+2o33T2ztF7fJLYP4mWM6WzQkoPV0aoP/F+mNUAiWBlIaNSSu97+doD/TJ5 PmwnuTz96xHwKdta/ZdbTnCiRNteduK7kI8oKoyaLx4cYLAQokcNwXBv++gkE2Blc7WQ zm98+UQcQ+rln3+2lyOfalYJ6mUEHHNY5zdkq1lfVI4a598yeACm9dyjGFXmO9gUuYx3 L0bFmYY48qejXGbTiAPv6TRGV8rX6SOyehBJ6f107gfaQEvUDRTL4Zk38v1cWttCMMZd xaVA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=s5LsyowA; dkim=neutral (no key) header.i=@suse.de; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id x185-20020a6386c2000000b005644a9be955si5916092pgd.179.2023.10.22.19.12.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Oct 2023 19:12:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=s5LsyowA; dkim=neutral (no key) header.i=@suse.de; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 35C9B805FD16; Sun, 22 Oct 2023 19:12:02 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229470AbjJWCMB (ORCPT + 99 others); Sun, 22 Oct 2023 22:12:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233057AbjJWCMA (ORCPT ); Sun, 22 Oct 2023 22:12:00 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B9EF8E for ; Sun, 22 Oct 2023 19:11:57 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id DF85E1FE05; Mon, 23 Oct 2023 02:11:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1698027115; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=k+tJrpWVDJJ7KETSa91IT4Yko+pzXy4M/FqN16u/imA=; b=s5LsyowArzqA8jhivPkChCZo47Ziif6yhlsjuYRwbsSaSV1PQx9dworgojmvuu1oHSrsK/ rDKhoDv4kHEcmaCGsY7lzNxDF05YzdUY6HPgHaV5zn8efU3psM5+SqAwAZNZmCUmCqSgyC U+BwDJR8bsIvaWrfgZmmiJa329kMjOc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1698027115; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=k+tJrpWVDJJ7KETSa91IT4Yko+pzXy4M/FqN16u/imA=; b=9LY8056gLdOPZckx/ThPj7qOa7rcXyTMj9p6Iv9QcEvVDKisdvBCchyE162cxWlTab9cSX 57mZlbYlpeHL7lBA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 6C925132FD; Mon, 23 Oct 2023 02:11:54 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id QpwwCWrWNWV2bwAAMHmgww (envelope-from ); Mon, 23 Oct 2023 02:11:54 +0000 From: NeilBrown To: Steve Dickson Cc: linux-nfs@vger.kernel.org, Trond Myklebust Subject: [PATCH 4/6] Move fork_workers() and wait_for_workers() in cache.c Date: Mon, 23 Oct 2023 12:58:34 +1100 Message-ID: <20231023021052.5258-5-neilb@suse.de> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231023021052.5258-1-neilb@suse.de> References: <20231023021052.5258-1-neilb@suse.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Authentication-Results: smtp-out2.suse.de; none X-Spam-Level: X-Spam-Score: -2.10 X-Spamd-Result: default: False [-2.10 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[3]; TO_DN_SOME(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; R_MISSING_CHARSET(2.50)[]; BROKEN_CONTENT_TYPE(1.50)[]; NEURAL_HAM_LONG(-3.00)[-1.000]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Sun, 22 Oct 2023 19:12:02 -0700 (PDT) Both mountd and exported have fork_workers() and wait_for_workers() which are nearly identical. Move this code into cache.c (adding a cache_ prefix to the function names) and leave the minor differences in the two callers. Also remove duplicate declarations from mountd.h. Signed-off-by: NeilBrown --- support/export/cache.c | 75 ++++++++++++++++++++++++++++++++- support/export/export.h | 2 + utils/exportd/exportd.c | 90 ++++++---------------------------------- utils/mountd/mountd.c | 91 ++++++----------------------------------- utils/mountd/mountd.h | 9 ---- 5 files changed, 99 insertions(+), 168 deletions(-) diff --git a/support/export/cache.c b/support/export/cache.c index 5307f6c8d872..1874156af5e5 100644 --- a/support/export/cache.c +++ b/support/export/cache.c @@ -1,10 +1,9 @@ - /* * Handle communication with knfsd internal cache * * We open /proc/net/rpc/{auth.unix.ip,nfsd.export,nfsd.fh}/channel * and listen for requests (using my_svc_run) - * + * */ #ifdef HAVE_CONFIG_H @@ -16,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -1775,3 +1775,74 @@ cache_get_filehandle(nfs_export *exp, int len, char *p) fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE); return &fh; } + +/* Wait for all worker child processes to exit and reap them */ +void +cache_wait_for_workers(char *prog) +{ + int status; + pid_t pid; + + for (;;) { + + pid = waitpid(0, &status, 0); + + if (pid < 0) { + if (errno == ECHILD) + return; /* no more children */ + xlog(L_FATAL, "%s: can't wait: %s\n", prog, + strerror(errno)); + } + + /* Note: because we SIG_IGN'd SIGCHLD earlier, this + * does not happen on 2.6 kernels, and waitpid() blocks + * until all the children are dead then returns with + * -ECHILD. But, we don't need to do anything on the + * death of individual workers, so we don't care. */ + xlog(L_NOTICE, "%s: reaped child %d, status %d\n", + prog, (int)pid, status); + } +} + +/* Fork num_threads worker children and wait for them */ +int +cache_fork_workers(char *prog, int num_threads) +{ + int i; + pid_t pid; + + if (num_threads <= 1) + return 1; + + xlog(L_NOTICE, "%s: starting %d threads\n", prog, num_threads); + + for (i = 0 ; i < num_threads ; i++) { + pid = fork(); + if (pid < 0) { + xlog(L_FATAL, "%s: cannot fork: %s\n", prog, + strerror(errno)); + } + if (pid == 0) { + /* worker child */ + + /* Re-enable the default action on SIGTERM et al + * so that workers die naturally when sent them. + * Only the parent unregisters with pmap and + * hence needs to do special SIGTERM handling. */ + struct sigaction sa; + sa.sa_handler = SIG_DFL; + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + sigaction(SIGHUP, &sa, NULL); + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); + + /* fall into my_svc_run in caller */ + return 1; + } + } + + /* in parent */ + cache_wait_for_workers(prog); + return 0; +} diff --git a/support/export/export.h b/support/export/export.h index 8d5a0d3004ef..ce561f9fbd3e 100644 --- a/support/export/export.h +++ b/support/export/export.h @@ -29,6 +29,8 @@ int v4clients_process(fd_set *fdset); struct nfs_fh_len * cache_get_filehandle(nfs_export *exp, int len, char *p); int cache_export(nfs_export *exp, char *path); +int cache_fork_workers(char *prog, int num_threads); +void cache_wait_for_workers(char *prog); bool ipaddr_client_matches(nfs_export *exp, struct addrinfo *ai); bool namelist_client_matches(nfs_export *exp, char *dom); diff --git a/utils/exportd/exportd.c b/utils/exportd/exportd.c index 6f866445efc2..d07a885c6763 100644 --- a/utils/exportd/exportd.c +++ b/utils/exportd/exportd.c @@ -16,7 +16,6 @@ #include #include #include -#include #include "nfslib.h" #include "conffile.h" @@ -54,90 +53,19 @@ static char shortopts[] = "d:fghs:t:liT:"; */ inline static void set_signals(void); -/* Wait for all worker child processes to exit and reap them */ -static void -wait_for_workers (void) -{ - int status; - pid_t pid; - - for (;;) { - - pid = waitpid(0, &status, 0); - - if (pid < 0) { - if (errno == ECHILD) - return; /* no more children */ - xlog(L_FATAL, "mountd: can't wait: %s\n", - strerror(errno)); - } - - /* Note: because we SIG_IGN'd SIGCHLD earlier, this - * does not happen on 2.6 kernels, and waitpid() blocks - * until all the children are dead then returns with - * -ECHILD. But, we don't need to do anything on the - * death of individual workers, so we don't care. */ - xlog(L_NOTICE, "mountd: reaped child %d, status %d\n", - (int)pid, status); - } -} - inline void cleanup_lockfiles (void) { unlink(etab.lockfn); } -/* Fork num_threads worker children and wait for them */ static void -fork_workers(void) -{ - int i; - pid_t pid; - - xlog(L_NOTICE, "mountd: starting %d threads\n", num_threads); - - for (i = 0 ; i < num_threads ; i++) { - pid = fork(); - if (pid < 0) { - xlog(L_FATAL, "mountd: cannot fork: %s\n", - strerror(errno)); - } - if (pid == 0) { - /* worker child */ - - /* Re-enable the default action on SIGTERM et al - * so that workers die naturally when sent them. - * Only the parent unregisters with pmap and - * hence needs to do special SIGTERM handling. */ - struct sigaction sa; - sa.sa_handler = SIG_DFL; - sa.sa_flags = 0; - sigemptyset(&sa.sa_mask); - sigaction(SIGHUP, &sa, NULL); - sigaction(SIGINT, &sa, NULL); - sigaction(SIGTERM, &sa, NULL); - - /* fall into my_svc_run in caller */ - return; - } - } - - /* in parent */ - wait_for_workers(); - cleanup_lockfiles(); - free_state_path_names(&etab); - xlog(L_NOTICE, "exportd: no more workers, exiting\n"); - exit(0); -} - -static void killer (int sig) { if (num_threads > 1) { /* play Kronos and eat our children */ kill(0, SIGTERM); - wait_for_workers(); + cache_wait_for_workers("exportd"); } cleanup_lockfiles(); free_state_path_names(&etab); @@ -145,6 +73,7 @@ killer (int sig) exit(0); } + static void sig_hup (int UNUSED(sig)) { @@ -152,8 +81,9 @@ sig_hup (int UNUSED(sig)) xlog (L_NOTICE, "Received SIGHUP... Ignoring.\n"); return; } -inline static void -set_signals(void) + +inline static void +set_signals(void) { struct sigaction sa; @@ -295,9 +225,13 @@ main(int argc, char **argv) */ cache_open(); - if (num_threads > 1) - fork_workers(); - + if (cache_fork_workers(progname, num_threads) == 0) { + /* We forked, waited, and now need to clean up */ + cleanup_lockfiles(); + free_state_path_names(&etab); + xlog(L_NOTICE, "%s: no more workers, exiting\n", progname); + exit(0); + } v4clients_init(); diff --git a/utils/mountd/mountd.c b/utils/mountd/mountd.c index f9c62cded66c..dbd5546df61c 100644 --- a/utils/mountd/mountd.c +++ b/utils/mountd/mountd.c @@ -21,7 +21,6 @@ #include #include #include -#include #include "conffile.h" #include "xmalloc.h" @@ -119,90 +118,17 @@ cleanup_lockfiles (void) unlink(rmtab.lockfn); } -/* Wait for all worker child processes to exit and reap them */ -static void -wait_for_workers (void) -{ - int status; - pid_t pid; - - for (;;) { - - pid = waitpid(0, &status, 0); - - if (pid < 0) { - if (errno == ECHILD) - return; /* no more children */ - xlog(L_FATAL, "mountd: can't wait: %s\n", - strerror(errno)); - } - - /* Note: because we SIG_IGN'd SIGCHLD earlier, this - * does not happen on 2.6 kernels, and waitpid() blocks - * until all the children are dead then returns with - * -ECHILD. But, we don't need to do anything on the - * death of individual workers, so we don't care. */ - xlog(L_NOTICE, "mountd: reaped child %d, status %d\n", - (int)pid, status); - } -} - -/* Fork num_threads worker children and wait for them */ -static void -fork_workers(void) -{ - int i; - pid_t pid; - - xlog(L_NOTICE, "mountd: starting %d threads\n", num_threads); - - for (i = 0 ; i < num_threads ; i++) { - pid = fork(); - if (pid < 0) { - xlog(L_FATAL, "mountd: cannot fork: %s\n", - strerror(errno)); - } - if (pid == 0) { - /* worker child */ - - /* Re-enable the default action on SIGTERM et al - * so that workers die naturally when sent them. - * Only the parent unregisters with pmap and - * hence needs to do special SIGTERM handling. */ - struct sigaction sa; - sa.sa_handler = SIG_DFL; - sa.sa_flags = 0; - sigemptyset(&sa.sa_mask); - sigaction(SIGHUP, &sa, NULL); - sigaction(SIGINT, &sa, NULL); - sigaction(SIGTERM, &sa, NULL); - - /* fall into my_svc_run in caller */ - return; - } - } - - /* in parent */ - wait_for_workers(); - unregister_services(); - cleanup_lockfiles(); - free_state_path_names(&etab); - free_state_path_names(&rmtab); - xlog(L_NOTICE, "mountd: no more workers, exiting\n"); - exit(0); -} - /* * Signal handler. */ -static void +static void killer (int sig) { unregister_services(); if (num_threads > 1) { /* play Kronos and eat our children */ kill(0, SIGTERM); - wait_for_workers(); + cache_wait_for_workers("mountd"); } cleanup_lockfiles(); free_state_path_names(&etab); @@ -220,7 +146,7 @@ sig_hup (int UNUSED(sig)) } bool_t -mount_null_1_svc(struct svc_req *rqstp, void *UNUSED(argp), +mount_null_1_svc(struct svc_req *rqstp, void *UNUSED(argp), void *UNUSED(resp)) { struct sockaddr *sap = nfs_getrpccaller(rqstp->rq_xprt); @@ -922,8 +848,15 @@ main(int argc, char **argv) */ cache_open(); - if (num_threads > 1) - fork_workers(); + if (cache_fork_workers("mountd", num_threads) == 0) { + /* We forked, waited, and now need to clean up */ + unregister_services(); + cleanup_lockfiles(); + free_state_path_names(&etab); + free_state_path_names(&rmtab); + xlog(L_NOTICE, "mountd: no more workers, exiting\n"); + exit(0); + } nfsd_path_init(); v4clients_init(); diff --git a/utils/mountd/mountd.h b/utils/mountd/mountd.h index d30775313f66..bd5c9576d8ad 100644 --- a/utils/mountd/mountd.h +++ b/utils/mountd/mountd.h @@ -51,13 +51,4 @@ void mountlist_del(char *host, const char *path); void mountlist_del_all(const struct sockaddr *sap); mountlist mountlist_list(void); -void cache_open(void); -struct nfs_fh_len * - cache_get_filehandle(nfs_export *exp, int len, char *p); -int cache_export(nfs_export *exp, char *path); - -bool ipaddr_client_matches(nfs_export *exp, struct addrinfo *ai); -bool namelist_client_matches(nfs_export *exp, char *dom); -bool client_matches(nfs_export *exp, char *dom, struct addrinfo *ai); - #endif /* MOUNTD_H */ -- 2.42.0