From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.0 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EEEF1C433DB for ; Tue, 2 Mar 2021 14:35:11 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 6C08564F23 for ; Tue, 2 Mar 2021 14:35:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6C08564F23 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:48820 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lH67C-0006Nh-7N for qemu-devel@archiver.kernel.org; Tue, 02 Mar 2021 09:35:10 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:36778) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lH66L-0005rc-Qq for qemu-devel@nongnu.org; Tue, 02 Mar 2021 09:34:17 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:49047) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lH66I-0001uw-Fn for qemu-devel@nongnu.org; Tue, 02 Mar 2021 09:34:17 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614695652; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=OpCjEKCiUq0UfjElv5Meixzc3hIIhh/LgX0u9UvRcfM=; b=KJQyxlfrBkyu7AP7m2PPIsds0Sah+18B/0c23AzP4Ofgo+4MbYYhGhBLl870/o8BatqSQU nuY89s2lyEhlyqddQiqOP9OQApdihybGFEyUi0XBpPH6pTEY74LASozO7152RVW5dkic1y xLFpOCN1y7UB2EVn2FGcQl56L++1IGw= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-528-PYBF1RXMMymwPQnvEU5kxA-1; Tue, 02 Mar 2021 09:34:06 -0500 X-MC-Unique: PYBF1RXMMymwPQnvEU5kxA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D79F21E563; Tue, 2 Mar 2021 14:34:05 +0000 (UTC) Received: from localhost (ovpn-112-87.ams2.redhat.com [10.36.112.87]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8353519C93; Tue, 2 Mar 2021 14:34:02 +0000 (UTC) Date: Tue, 2 Mar 2021 14:34:01 +0000 From: "Richard W.M. Jones" To: Stefan Hajnoczi Subject: Re: [PATCH v3] qemu-storage-daemon: add --pidfile option Message-ID: <20210302143401.GD26415@redhat.com> References: <20210302142746.170535-1-stefanha@redhat.com> MIME-Version: 1.0 In-Reply-To: <20210302142746.170535-1-stefanha@redhat.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=rjones@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Received-SPF: pass client-ip=63.128.21.124; envelope-from=rjones@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , afrosi@redhat.com, Daniel Berrange , qemu-devel@nongnu.org, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" On Tue, Mar 02, 2021 at 02:27:46PM +0000, Stefan Hajnoczi wrote: > Daemons often have a --pidfile option where the pid is written to a file > so that scripts can stop the daemon by sending a signal. > > The pid file also acts as a lock to prevent multiple instances of the > daemon from launching for a given pid file. > > QEMU, qemu-nbd, qemu-ga, virtiofsd, and qemu-pr-helper all support the > --pidfile option. Add it to qemu-storage-daemon too. > > Reported-by: Richard W.M. Jones > Signed-off-by: Stefan Hajnoczi > --- > v3: > * Add --pidfile help output [Kevin] > > docs/tools/qemu-storage-daemon.rst | 14 +++++++++++ > storage-daemon/qemu-storage-daemon.c | 36 ++++++++++++++++++++++++++++ > 2 files changed, 50 insertions(+) > > diff --git a/docs/tools/qemu-storage-daemon.rst b/docs/tools/qemu-storage-daemon.rst > index f63627eaf6..3d9704d835 100644 > --- a/docs/tools/qemu-storage-daemon.rst > +++ b/docs/tools/qemu-storage-daemon.rst > @@ -118,6 +118,20 @@ Standard options: > List object properties with ``,help``. See the :manpage:`qemu(1)` > manual page for a description of the object properties. > > +.. option:: --pidfile PATH > + > + is the path to a file where the daemon writes its pid. This allows scripts to > + stop the daemon by sending a signal:: > + > + $ kill -SIGTERM $( + > + A file lock is applied to the file so only one instance of the daemon can run > + with a given pid file path. The daemon unlinks its pid file when terminating. > + > + The pid file is written after chardevs, exports, and NBD servers have been > + created but before accepting connections. The daemon has started successfully > + when the pid file is written and clients may begin connecting. > + > Examples > -------- > Launch the daemon with QMP monitor socket ``qmp.sock`` so clients can execute > diff --git a/storage-daemon/qemu-storage-daemon.c b/storage-daemon/qemu-storage-daemon.c > index 9021a46b3a..4672145df5 100644 > --- a/storage-daemon/qemu-storage-daemon.c > +++ b/storage-daemon/qemu-storage-daemon.c > @@ -59,6 +59,7 @@ > #include "sysemu/runstate.h" > #include "trace/control.h" > > +static const char *pid_file; > static volatile bool exit_requested = false; > > void qemu_system_killed(int signal, pid_t pid) > @@ -115,6 +116,8 @@ static void help(void) > " See the qemu(1) man page for documentation of the\n" > " objects that can be added.\n" > "\n" > +" --pidfile write process ID to a file after startup\n" > +"\n" > QEMU_HELP_BOTTOM "\n", > error_get_progname()); > } > @@ -126,6 +129,7 @@ enum { > OPTION_MONITOR, > OPTION_NBD_SERVER, > OPTION_OBJECT, > + OPTION_PIDFILE, > }; > > extern QemuOptsList qemu_chardev_opts; > @@ -164,6 +168,7 @@ static void process_options(int argc, char *argv[]) > {"monitor", required_argument, NULL, OPTION_MONITOR}, > {"nbd-server", required_argument, NULL, OPTION_NBD_SERVER}, > {"object", required_argument, NULL, OPTION_OBJECT}, > + {"pidfile", required_argument, NULL, OPTION_PIDFILE}, > {"trace", required_argument, NULL, 'T'}, > {"version", no_argument, NULL, 'V'}, > {0, 0, 0, 0} > @@ -275,6 +280,9 @@ static void process_options(int argc, char *argv[]) > qobject_unref(args); > break; > } > + case OPTION_PIDFILE: > + pid_file = optarg; > + break; > default: > g_assert_not_reached(); > } > @@ -285,6 +293,27 @@ static void process_options(int argc, char *argv[]) > } > } > > +static void pid_file_cleanup(void) > +{ > + unlink(pid_file); > +} > + > +static void pid_file_init(void) > +{ > + Error *err = NULL; > + > + if (!pid_file) { > + return; > + } > + > + if (!qemu_write_pidfile(pid_file, &err)) { > + error_reportf_err(err, "cannot create PID file: "); > + exit(EXIT_FAILURE); > + } > + > + atexit(pid_file_cleanup); > +} > + > int main(int argc, char *argv[]) > { > #ifdef CONFIG_POSIX > @@ -312,6 +341,13 @@ int main(int argc, char *argv[]) > qemu_init_main_loop(&error_fatal); > process_options(argc, argv); > > + /* > + * Write the pid file after creating chardevs, exports, and NBD servers but > + * before accepting connections. This ordering is documented. Do not change > + * it. > + */ > + pid_file_init(); > + > while (!exit_requested) { > main_loop_wait(false); > } > -- > 2.29.2 > Reviewed-by: Richard W.M. Jones Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW