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 B4A7A3858C74 for ; Fri, 19 Jan 2024 11:57:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B4A7A3858C74 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 B4A7A3858C74 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=1705665430; cv=none; b=MMyLbcXxLSFoqWW6NVzeaMhA33K2UwVbXaNXQqc7qam3d3sjqfhjA/4ADYVl78oAsfbGewEfXqbBfdunG80FBjXXTdCag5jEqxQ43mNBMhR78ck6I0H3AVM7lBxlnvCC/lJkpa0wOZv7yrgidr39O/mSfYNSkl9WNJoPXsgoDUc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705665430; c=relaxed/simple; bh=R0ikvpl73RJk0tWjTJEJyPazsbp4xImmBys4m8gEIWU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=HZ+f1crzYiicvswoMP2JvnuZrQeuZvZzIq41fxSS22G8Lzz6BSCy/nNO75ylBTQlaF989S1ieisFkRMLq8iFvtDjwBSx68/5RIUD0iTL0el1XXM73M+pKSjV4Z6livZLa2NNeq8ztegDw8JvgZw0cfTFH3gKWS7rwM5WB0YIyFE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705665428; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tvVM/e4EmuKcIK7aGvXZLvvHy7K4N8Y20LwPyBVlQfo=; b=Od623woLnIYj1hmcnuXHRqLc9DAybo28AI6t+t6qMOwACY2ycwxen8DxMJXzeRwKKNtQqI +kb+PK8VxuAXpcqY3TpVQMRio1Pgj7ByzYInt/MsKU6QLTtWHNcbrHMSV0BqU1D3GB0MaN cvWZP4KS9W4QKs22TgLXQpKR1l4OUl8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-601-rPuYjfiHPoScNwRMV0Yv7Q-1; Fri, 19 Jan 2024 06:57:06 -0500 X-MC-Unique: rPuYjfiHPoScNwRMV0Yv7Q-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (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 B853983B82C for ; Fri, 19 Jan 2024 11:57:06 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.242.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1C0474011C41 for ; Fri, 19 Jan 2024 11:57:05 +0000 (UTC) From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= To: gdb-patches@sourceware.org Subject: [PATCH v2 2/6] gdb/ser-pipe.c: Duplicate the file descriptors Date: Fri, 19 Jan 2024 12:56:26 +0100 Message-ID: <20240119115659.491195-4-ahajkova@redhat.com> In-Reply-To: <20240119115659.491195-1-ahajkova@redhat.com> References: <20240119115659.491195-1-ahajkova@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 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=-11.5 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 the 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 | 40 ++++++++++++++++++++++++++++++++++++++++ gdb/serial.c | 4 ++++ gdb/serial.h | 4 ++++ 3 files changed, 48 insertions(+) diff --git a/gdb/ser-pipe.c b/gdb/ser-pipe.c index 842b656eb5a..849926c179b 100644 --- a/gdb/ser-pipe.c +++ b/gdb/ser-pipe.c @@ -77,6 +77,32 @@ pipe_open (struct serial *scb, const char *name) perror_with_name (_("could not open socket pair"), save); } + /* 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); + if (saved_stdin == -1 || saved_stdout == -1 || saved_stderr == -1) + { + /* If any FD failed to dup() then we can't used the default-fd mechanism, + so close any files that succeeded. */ + if (saved_stdin == -1) + close (saved_stdin); + if (saved_stdout == -1) + close (saved_stdout); + if (saved_stderr == -1) + close (saved_stderr); + saved_stdin = saved_stdout = saved_stderr = -1; + } + + mark_fd_no_cloexec (saved_stdout); + mark_fd_no_cloexec (saved_stdin); + mark_fd_no_cloexec (saved_stderr); + + 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'' @@ -91,6 +117,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; perror_with_name (_("could not vfork"), save); } @@ -140,6 +172,14 @@ pipe_open (struct serial *scb, const char *name) close (pdes[1]); if (err_pdes[1] != -1) close (err_pdes[1]); + + unmark_fd_no_cloexec(saved_stdout); + unmark_fd_no_cloexec(saved_stdin); + unmark_fd_no_cloexec(saved_stderr); + 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 734a580ed02..8fe7bcfe4b4 100644 --- a/gdb/serial.c +++ b/gdb/serial.c @@ -180,6 +180,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 69507e69295..bfec99ad7c6 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.43.0