From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 1E3B53858C98 for ; Fri, 17 Nov 2023 11:18:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1E3B53858C98 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1E3B53858C98 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700219930; cv=none; b=rvri9CRgRJqvW0V4ii/YS17sdQf5Fks41iujy7CEHblE55e85/EO8fqcUaNxzwRhUUuMehuqWjXZToGVADLhTjAVeprz3XpeVshgAAZWW9TWNTHsro5rdWYAaIuJfUCNS/VLtzR3GGpGtU+bWhXwYSdOQMzp6ZOTEesUby7J2e0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700219930; c=relaxed/simple; bh=71cgKv7O20ar55M/3gjqbpd84bj9jncmvcnADr6J6lc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Rg0/fJeauCJA8Cc6V1u8Yf05g73C1b73lup2/RMii+Xx+78+SJqPzN6fO9UZENOXWNTF36iO2Rqpp5ifyxumjxWuseS3ZW/dnGngGY38iDcSC9u/yO7XBc3ED72LYqoLhjNv5vx2MzAKZHodDwwJwr8rXTLz4VIP7uNg3Ce2M7g= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700219928; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1ettGmkh/krZKvHrwZs1j5rftxhwzQFQnf65eTMlyLc=; b=O7K5JDXs96MC8Ha3tcE9ZbVnep18gI8r3GBeRPAY/2IoQmJ4npAI0bPD/TNItmbua3h+4w Z0lQEr7f5KrHSheJRcwZma5ocNAS2HhYta1p/QooHrEhMf+Em3cBLFGXS2pr+ToMU2TCTQ aeNMassacLZTX/DznEMzqeJnax3hJGY= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-37-UmRCOnFUPUyXqRpQZM_8eg-1; Fri, 17 Nov 2023 06:18:47 -0500 X-MC-Unique: UmRCOnFUPUyXqRpQZM_8eg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D9A2C1C04353 for ; Fri, 17 Nov 2023 11:18:46 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.242.6]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E464B2026D4C; Fri, 17 Nov 2023 11:18:45 +0000 (UTC) From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= To: gdb-patches@sourceware.org Cc: ahajkova@redhat.com Subject: [PATCH 2/6] gdb/ser-pipe.c: Duplicate the file descriptors Date: Fri, 17 Nov 2023 12:18:36 +0100 Message-ID: <20231117111840.2040709-3-ahajkova@redhat.com> In-Reply-To: <20231117111840.2040709-1-ahajkova@redhat.com> References: <20231117111840.2040709-1-ahajkova@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Duplicate the numbers of STDOUT/STDIN/STDERR file descriptors GDB is connected to. Preserved numbers of the file descriptors could be then sent to the GDBserver. If GDBserver is run locally and will accept he numbers of the file descriptors, it can start the inferior connected to the same STDIN/OUT/ERR, GDB is connected to. --- gdb/ser-pipe.c | 25 +++++++++++++++++++++++++ gdb/serial.c | 4 ++++ gdb/serial.h | 4 ++++ 3 files changed, 33 insertions(+) diff --git a/gdb/ser-pipe.c b/gdb/ser-pipe.c index 47ccd33cece..84253cf2e2c 100644 --- a/gdb/ser-pipe.c +++ b/gdb/ser-pipe.c @@ -77,6 +77,16 @@ pipe_open (struct serial *scb, const char *name) return -1; } + /* Preserve STDIN/STDOUT/STDERR so they won't be closed on + exec later, after we fork. */ + int saved_stdin = dup (STDIN_FILENO); + int saved_stdout = dup (STDOUT_FILENO); + int saved_stderr = dup (STDERR_FILENO); + + scb->fds[0] = saved_stdin; + scb->fds[1] = saved_stdout; + scb->fds[2] = saved_stderr; + /* Create the child process to run the command in. Note that the apparent call to vfork() below *might* actually be a call to fork() due to the fact that autoconf will ``#define vfork fork'' @@ -90,6 +100,12 @@ pipe_open (struct serial *scb, const char *name) close (pdes[1]); close (err_pdes[0]); close (err_pdes[1]); + close (saved_stdout); + close (saved_stdin); + close (saved_stderr); + scb->fds[0] = -1; + scb->fds[1] = -1; + scb->fds[2] = -1; return -1; } @@ -128,6 +144,10 @@ pipe_open (struct serial *scb, const char *name) close (err_pdes[1]); } + mark_fd_no_cloexec (saved_stdout); + mark_fd_no_cloexec (saved_stdin); + mark_fd_no_cloexec (saved_stderr); + close_most_fds (); const char *shellfile = get_shell (); @@ -139,6 +159,11 @@ pipe_open (struct serial *scb, const char *name) close (pdes[1]); if (err_pdes[1] != -1) close (err_pdes[1]); + + close (saved_stdout); + close (saved_stdin); + close (saved_stderr); + /* :end chunk */ state = XNEW (struct pipe_state); state->pid = pid; diff --git a/gdb/serial.c b/gdb/serial.c index 8a8bab46ead..a2bbe9a972d 100644 --- a/gdb/serial.c +++ b/gdb/serial.c @@ -182,6 +182,10 @@ new_serial (const struct serial_ops *ops) scb->ops = ops; + scb->fds[0] = -1; + scb->fds[1] = -1; + scb->fds[2] = -1; + scb->bufp = scb->buf; scb->error_fd = -1; scb->refcnt = 1; diff --git a/gdb/serial.h b/gdb/serial.h index 3b861200302..113f7360a55 100644 --- a/gdb/serial.h +++ b/gdb/serial.h @@ -254,6 +254,10 @@ struct serial int async_state; /* Async internal state. */ void *async_context; /* Async event thread's context */ serial_event_ftype *async_handler;/* Async event handler */ + + /* File descriptors for preserved STDIN/STDOUT/STDERR + to be sent to GDBserver when run locally. */ + int fds[3]; }; struct serial_ops -- 2.41.0