Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tYD7A-005taI-Cv for pgsql-hackers@arkaria.postgresql.org; Wed, 15 Jan 2025 23:48:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tYD77-001lFe-U0 for pgsql-hackers@arkaria.postgresql.org; Wed, 15 Jan 2025 23:47:58 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tYD77-001lFU-Fa for pgsql-hackers@lists.postgresql.org; Wed, 15 Jan 2025 23:47:58 +0000 Received: from mail-yb1-xb34.google.com ([2607:f8b0:4864:20::b34]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tYD76-000c2p-1F for pgsql-hackers@lists.postgresql.org; Wed, 15 Jan 2025 23:47:57 +0000 Received: by mail-yb1-xb34.google.com with SMTP id 3f1490d57ef6-e53a91756e5so673884276.1 for ; Wed, 15 Jan 2025 15:47:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736984875; x=1737589675; darn=lists.postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=85NOL16xFf+tvmMIG3bCj9ucuT7AC+eMDOEKDeuMVmQ=; b=XkS3JQ7B6Vfy1eWeR2CpkzZzRcEL3ziFBegeCeIZtKKprkzWSgJ1ai7ieG35V1g9b1 7RxUpXS4ULPmAjY5r5lEXzMmeK08SgiYY/nETXAofYQaqZ6pA+UgDRcwXbE/CmLF1Go5 Eq+kIod26Nx8kt9sGNLjfrJ2o1ddSf9WmCkUaCTmbs+AnwM8ETGalSoBq4ggMg4ITXya zqjK4VUf4kmO29MsNMSES2zcsEl6OwmCZOt7LqQeMyMG4IFzlh11+nP5vlvvWWu9aDGl T5daq4K+bKiIHGvCbNh4Le/eq85nMnOfKtHu0vrSHqchpLonocp8PcdT6D9rF3JEzNWS Ca6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736984875; x=1737589675; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=85NOL16xFf+tvmMIG3bCj9ucuT7AC+eMDOEKDeuMVmQ=; b=VbTXU57JPLI4CVn37nMDCY87vQBWq6saocyU9rkJqooENhujH9XeP7EX//rHkfSwKJ ee+eJpK/D9TIrBLjc/9IGXJhKMTEtv2bzC7i1lvP5mCVZ9S8OVu8kp/i+SQdZ7sWYjc5 oAwzjFHIwDqiiY7MLeuzbyrV0Xr5Htez89H3q8AenN2BYS5RyU9N2qxNN5p5QvuN3uSR +dxH3QuSTg13EZSPOv2lmi4EbMDSXMY6yhamyxXUerT9WSAF5XP/UoRE/gksY76KVeAg Jliqi9oPTl5xIpqF1WJyUo1tw+8OJUBZyS7MN/UuEQYxYJox3mGG5HK/UFgrUafbm4QB l3+w== X-Forwarded-Encrypted: i=1; AJvYcCV1t0DNLKLgwgrJcsWihLXkwekATZwE9lxwjokrLK6HKEC62WQFzV95OYtfycaIKblIKIJ9HL+5Ww3in77G@lists.postgresql.org X-Gm-Message-State: AOJu0YyL1rRZsJwwrXKyeI7bBFbgoElfIzhBc92PNtJ07Yf5Dl730u02 2percS7QoRrkOyj3cnqnwqqHHSZ3Cbou2QLTc5o/9uTLEAdmFF15 X-Gm-Gg: ASbGncvFn7IQROYAk/+Ar+SUfZU7/SH3PcDimXwX9h+GeklTmShGtnZR2f7FU1bKitU L5mZwIDx6/yvAjm3hhkiy2X7GP4q5ycaRiMIitFgkGxFAmZHDy2842Dy9nLTvQgiEIo91AAyYiG COEDciCV2mhF9VcrIa/gMUIHuGIeecAItfzywD+1EQT+6zaw/3YBdEu5wrpyNIP1+pkZhppRlGo qrohMKpMtW2zrUq+jXxPM8+T6xXIC5Sg4UVCRK5Z5oIGjw47vAw0DrMzDd4K92Z8pMAc/galET9 8q5p1LWq4pzcDmZK4IpsJcRkfA== X-Google-Smtp-Source: AGHT+IEUxhQUJUkp8XuYBqEWV5fAyVzKSkefo56wT9qksQAWZN3qIl6rBNMT/aoMoR+jIaeRDglwhQ== X-Received: by 2002:a25:1e84:0:b0:e39:8e5f:adab with SMTP id 3f1490d57ef6-e54ee1f4cbbmr20189201276.39.1736984874604; Wed, 15 Jan 2025 15:47:54 -0800 (PST) Received: from nathan ([2600:382:8802:50f8:f982:be79:97b7:1142]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-e55a595d0d9sm2475537276.14.2025.01.15.15.47.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 15:47:53 -0800 (PST) Date: Wed, 15 Jan 2025 17:47:51 -0600 From: Nathan Bossart To: Thomas Munro Cc: Tom Lane , Fujii Masao , Andy Fan , pgsql-hackers@lists.postgresql.org Subject: Re: convert libpgport's pqsignal() to a void function Message-ID: References: <1164438.1736910166@sss.pgh.pa.us> <1171432.1736914085@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="EYHoee8f6wFuErPc" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --EYHoee8f6wFuErPc Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Thu, Jan 16, 2025 at 11:07:41AM +1300, Thomas Munro wrote: > +1 for your idea of not defining them at all outside the backend, it's > just confusing noise. I tried that, but these extra signals are needed even in the frontend for pgkill(), etc. My next thought was to simply ignore signal() errors for the extra signals, but I don't think that's very nice because it just masks broken code. Finally, I settled on modifying initdb's setup_signals() to use WIN32 checks instead of checking for the signals themselves. This is how it's done elsewhere, and this is apparently all that's needed to make cfbot's Windows run happy. I've also attached a 0002 that removes the redefinitions of SIG_ERR and friends. That looks good on cfbot, but we'll see what the buildfarm has to say... -- nathan --EYHoee8f6wFuErPc Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="v4-0001-Convert-libpgport-s-pqsignal-to-a-void-function.patch" From 15613d15ea907fa1aceee84d3a2e8b3708f95b4f Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Wed, 15 Jan 2025 17:30:23 -0600 Subject: [PATCH v4 1/2] Convert libpgport's pqsignal() to a void function. The protections added by commit 3b00fdba9f introduced race conditions to this function that can lead to bogus return values. Since nobody seems to inspect the return value, this is of little consequence, but it would have been nice to convert it to a void function to avoid any possibility of a bogus return value. Unfortunately, doing so would have required also modifying legacy-pqsignal.c's version of the function, which would've required an SONAME bump. Or so I thought... Thanks to commit 9a45a89c38, legacy-pqsignal.c now has its own dedicated extern for pqsignal(), which decouples it enough that we can follow through with changing libpgport's pqsignal() to a void function. This commit also adds a bit of error checking in the form of assertions for the return value of sigaction()/signal(). Since a failure most likely indicates a coding error, and nobody has ever bothered to check pqsignal()'s return value, it's probably not worth doing anything fancier. While at it, modify initdb's setup_signals() to check for WIN32 instead of the signals themselves. win32_port.h defines many extra signals, so the previous checks were insufficient to avoid calling pqsignal() with invalid signals on Windows. It'd be nice to avoid defining these extra signals in the frontend altogether, but they are used for pgkill(), etc., and we already use WIN32 checks for invalid signals in various places. --- src/bin/initdb/initdb.c | 19 +++++-------------- src/include/port.h | 2 +- src/port/pqsignal.c | 34 ++++++---------------------------- 3 files changed, 12 insertions(+), 43 deletions(-) diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index 101c780012..ea4b66b3bf 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -2874,27 +2874,18 @@ setup_text_search(void) void setup_signals(void) { - /* some of these are not valid on Windows */ -#ifdef SIGHUP - pqsignal(SIGHUP, trapsig); -#endif -#ifdef SIGINT pqsignal(SIGINT, trapsig); -#endif -#ifdef SIGQUIT - pqsignal(SIGQUIT, trapsig); -#endif -#ifdef SIGTERM pqsignal(SIGTERM, trapsig); -#endif + + /* the following are not valid on Windows */ +#ifndef WIN32 + pqsignal(SIGHUP, trapsig); + pqsignal(SIGQUIT, trapsig); /* Ignore SIGPIPE when writing to backend, so we can clean up */ -#ifdef SIGPIPE pqsignal(SIGPIPE, SIG_IGN); -#endif /* Prevent SIGSYS so we can probe for kernel calls that might not work */ -#ifdef SIGSYS pqsignal(SIGSYS, SIG_IGN); #endif } diff --git a/src/include/port.h b/src/include/port.h index f0e28ce5c5..4e9e565787 100644 --- a/src/include/port.h +++ b/src/include/port.h @@ -520,7 +520,7 @@ extern int pg_mkdir_p(char *path, int omode); #define pqsignal pqsignal_be #endif typedef void (*pqsigfunc) (SIGNAL_ARGS); -extern pqsigfunc pqsignal(int signo, pqsigfunc func); +extern void pqsignal(int signo, pqsigfunc func); /* port/quotes.c */ extern char *escape_single_quotes_ascii(const char *src); diff --git a/src/port/pqsignal.c b/src/port/pqsignal.c index 1169de6b81..5dd8b76bae 100644 --- a/src/port/pqsignal.c +++ b/src/port/pqsignal.c @@ -112,31 +112,15 @@ wrapper_handler(SIGNAL_ARGS) /* * Set up a signal handler, with SA_RESTART, for signal "signo" * - * Returns the previous handler. - * - * NB: If called within a signal handler, race conditions may lead to bogus - * return values. You should either avoid calling this within signal handlers - * or ignore the return value. - * - * XXX: Since no in-tree callers use the return value, and there is little - * reason to do so, it would be nice if we could convert this to a void - * function instead of providing potentially-bogus return values. - * Unfortunately, that requires modifying the pqsignal() in legacy-pqsignal.c, - * which in turn requires an SONAME bump, which is probably not worth it. - * * Note: the actual name of this function is either pqsignal_fe when * compiled with -DFRONTEND, or pqsignal_be when compiled without that. * This is to avoid a name collision with libpq's legacy-pqsignal.c. */ -pqsigfunc +void pqsignal(int signo, pqsigfunc func) { - pqsigfunc orig_func = pqsignal_handlers[signo]; /* assumed atomic */ #if !(defined(WIN32) && defined(FRONTEND)) - struct sigaction act, - oact; -#else - pqsigfunc ret; + struct sigaction act; #endif Assert(signo < PG_NSIG); @@ -155,17 +139,11 @@ pqsignal(int signo, pqsigfunc func) if (signo == SIGCHLD) act.sa_flags |= SA_NOCLDSTOP; #endif - if (sigaction(signo, &act, &oact) < 0) - return SIG_ERR; - else if (oact.sa_handler == wrapper_handler) - return orig_func; - else - return oact.sa_handler; + if (sigaction(signo, &act, NULL) < 0) + Assert(false); /* probably indicates coding error */ #else /* Forward to Windows native signal system. */ - if ((ret = signal(signo, func)) == wrapper_handler) - return orig_func; - else - return ret; + if (signal(signo, func) == SIG_ERR) + Assert(false); /* probably indicates coding error */ #endif } -- 2.39.5 (Apple Git-154) --EYHoee8f6wFuErPc Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="v4-0002-Remove-redeclarations-of-signal-functions-on-Wind.patch" From e99d35bb7ee3370a5a8688be320edd1df3dd0cbc Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Wed, 15 Jan 2025 17:36:09 -0600 Subject: [PATCH v4 2/2] Remove redeclarations of signal functions on Windows. --- src/include/port/win32_port.h | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/include/port/win32_port.h b/src/include/port/win32_port.h index 7900272e8d..ff7028bdc8 100644 --- a/src/include/port/win32_port.h +++ b/src/include/port/win32_port.h @@ -154,14 +154,6 @@ #define sigmask(sig) ( 1 << ((sig)-1) ) -/* Signal function return values */ -#undef SIG_DFL -#undef SIG_ERR -#undef SIG_IGN -#define SIG_DFL ((pqsigfunc)0) -#define SIG_ERR ((pqsigfunc)-1) -#define SIG_IGN ((pqsigfunc)1) - /* Some extra signals */ #define SIGHUP 1 #define SIGQUIT 3 -- 2.39.5 (Apple Git-154) --EYHoee8f6wFuErPc--