From patchwork Fri Jan 10 14:09:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilya Leoshkevich X-Patchwork-Id: 13934723 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 01B2FE77188 for ; Fri, 10 Jan 2025 14:15:35 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tWFlA-0004DE-Gx; Fri, 10 Jan 2025 09:13:12 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tWFl6-0004Ax-Jc for qemu-devel@nongnu.org; Fri, 10 Jan 2025 09:13:08 -0500 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tWFl4-0004Ek-QN for qemu-devel@nongnu.org; Fri, 10 Jan 2025 09:13:08 -0500 Received: from pps.filterd (m0356516.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 50ACHjjR020129; Fri, 10 Jan 2025 14:13:01 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=oBBwNUe0FHAARyM/9 yvbQbES/7VdqDS8Q9rkdH1xSfE=; b=GjZP5uxis5TgYp5XyqxiXuNndk83+wQ2+ NF78XXEYkJzo6n8o/gdyqeuCUQbgiZPnjUuVZM7s53iKSE5+akTzuPLNusWBVLSZ 15Ewo2ObNB3gWdUoKCwaKAdSu7odpJOQGpj4jldoq1bWMEpZJHXMKJzQY0EN/XJY Iyu9H+YP8NdNQokbrMnGCDDuRLkuR8Vh4THWZtEEJrnzfMtwGdHrXWAvwC9LMYOk n/EK0nLbOHW5hQBvsYBYdGXq3o7hLImdqiqdSiPykeJUy0MAfof/tgWyZ30qepmk +w7b8PvG2RztE7d1K2XYvTWtyJG1O23dp+JBjftYKCJqQshMwM1/g== Received: from ppma11.dal12v.mail.ibm.com (db.9e.1632.ip4.static.sl-reverse.com [50.22.158.219]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 442r9atyye-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 10 Jan 2025 14:13:01 +0000 (GMT) Received: from pps.filterd (ppma11.dal12v.mail.ibm.com [127.0.0.1]) by ppma11.dal12v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 50AAqWNp026183; Fri, 10 Jan 2025 14:13:00 GMT Received: from smtprelay03.fra02v.mail.ibm.com ([9.218.2.224]) by ppma11.dal12v.mail.ibm.com (PPS) with ESMTPS id 43yj12j96v-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 10 Jan 2025 14:13:00 +0000 Received: from smtpav03.fra02v.mail.ibm.com (smtpav03.fra02v.mail.ibm.com [10.20.54.102]) by smtprelay03.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 50AECwSS54591816 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 10 Jan 2025 14:12:58 GMT Received: from smtpav03.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 90CF22004F; Fri, 10 Jan 2025 14:12:58 +0000 (GMT) Received: from smtpav03.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D252720040; Fri, 10 Jan 2025 14:12:57 +0000 (GMT) Received: from heavy.ibm.com (unknown [9.179.24.22]) by smtpav03.fra02v.mail.ibm.com (Postfix) with ESMTP; Fri, 10 Jan 2025 14:12:57 +0000 (GMT) From: Ilya Leoshkevich To: Warner Losh , Riku Voipio , Laurent Vivier , Paolo Bonzini , Richard Henderson , =?utf-8?q?Alex_Benn=C3=A9?= =?utf-8?q?e?= Cc: Kyle Evans , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , qemu-devel@nongnu.org, =?utf-8?q?Daniel_P_=2E_Berrang=C3=A9?= , Ilya Leoshkevich Subject: [PATCH v5 2/8] gdbstub: Try unlinking the unix socket before binding Date: Fri, 10 Jan 2025 15:09:34 +0100 Message-ID: <20250110141255.2328-3-iii@linux.ibm.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250110141255.2328-1-iii@linux.ibm.com> References: <20250110141255.2328-1-iii@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: UEdwANMgB4UeKCi8bfVeG1vq6R7uTp1Y X-Proofpoint-ORIG-GUID: UEdwANMgB4UeKCi8bfVeG1vq6R7uTp1Y X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-15_01,2024-10-11_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 spamscore=0 lowpriorityscore=0 bulkscore=0 malwarescore=0 adultscore=0 suspectscore=0 mlxlogscore=915 impostorscore=0 priorityscore=1501 mlxscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2411120000 definitions=main-2501100106 Received-SPF: pass client-ip=148.163.158.5; envelope-from=iii@linux.ibm.com; helo=mx0b-001b2d01.pphosted.com X-Spam_score_int: -26 X-Spam_score: -2.7 X-Spam_bar: -- X-Spam_report: (-2.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org In case an emulated process execve()s another emulated process, bind() will fail, because the socket already exists. So try deleting it. Use the existing unix_listen() function which does this. Link qemu-user with qemu-sockets.c and add the monitor_get_fd() stub. Note that it is not possible to handle this in do_execv(): deleting gdbserver_user_state.socket_path before safe_execve() is not correct, because the latter may fail, and afterwards we may lose control. Signed-off-by: Ilya Leoshkevich --- gdbstub/user.c | 24 +++++------------------- stubs/meson.build | 2 ++ stubs/monitor-fd.c | 9 +++++++++ util/meson.build | 2 ++ 4 files changed, 18 insertions(+), 19 deletions(-) create mode 100644 stubs/monitor-fd.c diff --git a/gdbstub/user.c b/gdbstub/user.c index ef52f249ce9..2c500eb1e23 100644 --- a/gdbstub/user.c +++ b/gdbstub/user.c @@ -10,6 +10,7 @@ */ #include "qemu/osdep.h" +#include "qapi/error.h" #include "qemu/bitops.h" #include "qemu/cutils.h" #include "qemu/sockets.h" @@ -317,9 +318,9 @@ static bool gdb_accept_socket(int gdb_fd) static int gdbserver_open_socket(const char *path) { g_autoptr(GString) buf = g_string_new(""); - struct sockaddr_un sockaddr = {}; char *pid_placeholder; - int fd, ret; + Error *err = NULL; + int fd; pid_placeholder = strstr(path, "%d"); if (pid_placeholder != NULL) { @@ -329,24 +330,9 @@ static int gdbserver_open_socket(const char *path) path = buf->str; } - fd = socket(AF_UNIX, SOCK_STREAM, 0); + fd = unix_listen(path, &err); if (fd < 0) { - perror("create socket"); - return -1; - } - - sockaddr.sun_family = AF_UNIX; - pstrcpy(sockaddr.sun_path, sizeof(sockaddr.sun_path) - 1, path); - ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); - if (ret < 0) { - perror("bind socket"); - close(fd); - return -1; - } - ret = listen(fd, 1); - if (ret < 0) { - perror("listen socket"); - close(fd); + warn_report_err(err); return -1; } diff --git a/stubs/meson.build b/stubs/meson.build index e91614a874d..17168d03eed 100644 --- a/stubs/meson.build +++ b/stubs/meson.build @@ -61,6 +61,8 @@ if have_user if not (have_system or have_tools) stub_ss.add(files('qdev.c')) endif + + stub_ss.add(files('monitor-fd.c')) endif if have_system diff --git a/stubs/monitor-fd.c b/stubs/monitor-fd.c new file mode 100644 index 00000000000..9bb67498850 --- /dev/null +++ b/stubs/monitor-fd.c @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "qemu/osdep.h" +#include "monitor/monitor.h" + +int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp) +{ + abort(); +} diff --git a/util/meson.build b/util/meson.build index 5d8bef98912..780b5977a89 100644 --- a/util/meson.build +++ b/util/meson.build @@ -84,6 +84,8 @@ if have_block or have_ga util_ss.add(files('qemu-coroutine.c', 'qemu-coroutine-lock.c', 'qemu-coroutine-io.c')) util_ss.add(files(f'coroutine-@coroutine_backend@.c')) util_ss.add(files('thread-pool.c', 'qemu-timer.c')) +endif +if have_block or have_ga or have_user util_ss.add(files('qemu-sockets.c')) endif if have_block