All of lore.kernel.org
 help / color / mirror / Atom feed
From: marcandre.lureau@redhat.com
To: qemu-devel@nongnu.org
Cc: "Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>
Subject: [PATCH 05/32] char: move qemu_openpty_raw from util/ to char/
Date: Wed, 23 Mar 2022 19:57:16 +0400	[thread overview]
Message-ID: <20220323155743.1585078-6-marcandre.lureau@redhat.com> (raw)
In-Reply-To: <20220323155743.1585078-1-marcandre.lureau@redhat.com>

From: Marc-André Lureau <marcandre.lureau@redhat.com>

It is only needed by char-pty.

Fix the code style while at it.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 include/qemu-common.h |   2 -
 chardev/char-pty.c    | 111 +++++++++++++++++++++++++++++++++
 util/qemu-openpty.c   | 139 ------------------------------------------
 chardev/meson.build   |   4 +-
 util/meson.build      |   1 -
 5 files changed, 113 insertions(+), 144 deletions(-)
 delete mode 100644 util/qemu-openpty.c

diff --git a/include/qemu-common.h b/include/qemu-common.h
index f0fe07cd74c3..f9b3f85b8124 100644
--- a/include/qemu-common.h
+++ b/include/qemu-common.h
@@ -31,8 +31,6 @@ ssize_t qemu_write_full(int fd, const void *buf, size_t count)
 
 #ifndef _WIN32
 int qemu_pipe(int pipefd[2]);
-/* like openpty() but also makes it raw; return master fd */
-int qemu_openpty_raw(int *aslave, char *pty_name);
 #endif
 
 void cpu_exec_init_all(void);
diff --git a/chardev/char-pty.c b/chardev/char-pty.c
index a2d1e7c985bc..d73d8b23903d 100644
--- a/chardev/char-pty.c
+++ b/chardev/char-pty.c
@@ -197,6 +197,117 @@ static void char_pty_finalize(Object *obj)
     qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
 }
 
+#if defined HAVE_PTY_H
+# include <pty.h>
+#elif defined CONFIG_BSD
+# include <termios.h>
+# if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
+#  include <libutil.h>
+# else
+#  include <util.h>
+# endif
+#elif defined CONFIG_SOLARIS
+# include <termios.h>
+# include <stropts.h>
+#else
+# include <termios.h>
+#endif
+
+#ifdef __sun__
+
+#if !defined(HAVE_OPENPTY)
+/* Once illumos has openpty(), this is going to be removed. */
+static int openpty(int *amaster, int *aslave, char *name,
+                   struct termios *termp, struct winsize *winp)
+{
+    const char *slave;
+    int mfd = -1, sfd = -1;
+
+    *amaster = *aslave = -1;
+
+    mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
+    if (mfd < 0) {
+        goto err;
+    }
+
+    if (grantpt(mfd) == -1 || unlockpt(mfd) == -1) {
+        goto err;
+    }
+
+    if ((slave = ptsname(mfd)) == NULL) {
+        goto err;
+    }
+
+    if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1) {
+        goto err;
+    }
+
+    if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
+        (termp != NULL && tcgetattr(sfd, termp) < 0)) {
+        goto err;
+    }
+
+    *amaster = mfd;
+    *aslave = sfd;
+
+    if (winp) {
+        ioctl(sfd, TIOCSWINSZ, winp);
+    }
+
+    return 0;
+
+err:
+    if (sfd != -1) {
+        close(sfd);
+    }
+    close(mfd);
+    return -1;
+}
+#endif
+
+static void cfmakeraw (struct termios *termios_p)
+{
+    termios_p->c_iflag &=
+        ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
+    termios_p->c_oflag &= ~OPOST;
+    termios_p->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
+    termios_p->c_cflag &= ~(CSIZE | PARENB);
+    termios_p->c_cflag |= CS8;
+
+    termios_p->c_cc[VMIN] = 0;
+    termios_p->c_cc[VTIME] = 0;
+}
+#endif
+
+/* like openpty() but also makes it raw; return master fd */
+static int qemu_openpty_raw(int *aslave, char *pty_name)
+{
+    int amaster;
+    struct termios tty;
+#if defined(__OpenBSD__) || defined(__DragonFly__)
+    char pty_buf[PATH_MAX];
+#define q_ptsname(x) pty_buf
+#else
+    char *pty_buf = NULL;
+#define q_ptsname(x) ptsname(x)
+#endif
+
+    if (openpty(&amaster, aslave, pty_buf, NULL, NULL) < 0) {
+        return -1;
+    }
+
+    /* Set raw attributes on the pty. */
+    tcgetattr(*aslave, &tty);
+    cfmakeraw(&tty);
+    tcsetattr(*aslave, TCSAFLUSH, &tty);
+
+    if (pty_name) {
+        strcpy(pty_name, q_ptsname(amaster));
+    }
+
+    return amaster;
+}
+
 static void char_pty_open(Chardev *chr,
                           ChardevBackend *backend,
                           bool *be_opened,
diff --git a/util/qemu-openpty.c b/util/qemu-openpty.c
deleted file mode 100644
index 427f43a76973..000000000000
--- a/util/qemu-openpty.c
+++ /dev/null
@@ -1,139 +0,0 @@
-/*
- * qemu-openpty.c
- *
- * Copyright (c) 2003-2008 Fabrice Bellard
- * Copyright (c) 2010 Red Hat, Inc.
- *
- * Wrapper function qemu_openpty() implementation.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-/*
- * This is not part of oslib-posix.c because this function
- * uses openpty() which often in -lutil, and if we add this
- * dependency to oslib-posix.o, every app will have to be
- * linked with -lutil.
- */
-
-#include "qemu/osdep.h"
-#include "qemu-common.h"
-
-#if defined HAVE_PTY_H
-# include <pty.h>
-#elif defined CONFIG_BSD
-# include <termios.h>
-# if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
-#  include <libutil.h>
-# else
-#  include <util.h>
-# endif
-#elif defined CONFIG_SOLARIS
-# include <termios.h>
-# include <stropts.h>
-#else
-# include <termios.h>
-#endif
-
-#ifdef __sun__
-
-#if !defined(HAVE_OPENPTY)
-/* Once illumos has openpty(), this is going to be removed. */
-static int openpty(int *amaster, int *aslave, char *name,
-                   struct termios *termp, struct winsize *winp)
-{
-        const char *slave;
-        int mfd = -1, sfd = -1;
-
-        *amaster = *aslave = -1;
-
-        mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
-        if (mfd < 0)
-                goto err;
-
-        if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
-                goto err;
-
-        if ((slave = ptsname(mfd)) == NULL)
-                goto err;
-
-        if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
-                goto err;
-
-        if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
-            (termp != NULL && tcgetattr(sfd, termp) < 0))
-                goto err;
-
-        *amaster = mfd;
-        *aslave = sfd;
-
-        if (winp)
-                ioctl(sfd, TIOCSWINSZ, winp);
-
-        return 0;
-
-err:
-        if (sfd != -1)
-                close(sfd);
-        close(mfd);
-        return -1;
-}
-#endif
-
-static void cfmakeraw (struct termios *termios_p)
-{
-        termios_p->c_iflag &=
-                ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
-        termios_p->c_oflag &= ~OPOST;
-        termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
-        termios_p->c_cflag &= ~(CSIZE|PARENB);
-        termios_p->c_cflag |= CS8;
-
-        termios_p->c_cc[VMIN] = 0;
-        termios_p->c_cc[VTIME] = 0;
-}
-#endif
-
-int qemu_openpty_raw(int *aslave, char *pty_name)
-{
-    int amaster;
-    struct termios tty;
-#if defined(__OpenBSD__) || defined(__DragonFly__)
-    char pty_buf[PATH_MAX];
-#define q_ptsname(x) pty_buf
-#else
-    char *pty_buf = NULL;
-#define q_ptsname(x) ptsname(x)
-#endif
-
-    if (openpty(&amaster, aslave, pty_buf, NULL, NULL) < 0) {
-        return -1;
-    }
-
-    /* Set raw attributes on the pty. */
-    tcgetattr(*aslave, &tty);
-    cfmakeraw(&tty);
-    tcsetattr(*aslave, TCSAFLUSH, &tty);
-
-    if (pty_name) {
-        strcpy(pty_name, q_ptsname(amaster));
-    }
-
-    return amaster;
-}
diff --git a/chardev/meson.build b/chardev/meson.build
index 325ba2bdb97d..664f77b8879a 100644
--- a/chardev/meson.build
+++ b/chardev/meson.build
@@ -12,11 +12,11 @@ chardev_ss.add(files(
   'char-udp.c',
   'char.c',
 ))
-chardev_ss.add(when: 'CONFIG_POSIX', if_true: files(
+chardev_ss.add(when: 'CONFIG_POSIX', if_true: [files(
   'char-fd.c',
   'char-parallel.c',
   'char-pty.c',
-))
+), util])
 chardev_ss.add(when: 'CONFIG_WIN32', if_true: files(
   'char-console.c',
   'char-win-stdio.c',
diff --git a/util/meson.build b/util/meson.build
index bb0b010662d7..f8f0643318c3 100644
--- a/util/meson.build
+++ b/util/meson.build
@@ -16,7 +16,6 @@ if targetos == 'freebsd'
   freebsd_dep = util
 endif
 util_ss.add(when: 'CONFIG_POSIX', if_true: [files('oslib-posix.c'), freebsd_dep])
-util_ss.add(when: 'CONFIG_POSIX', if_true: [files('qemu-openpty.c'), util])
 util_ss.add(when: 'CONFIG_POSIX', if_true: files('qemu-thread-posix.c'))
 util_ss.add(when: 'CONFIG_POSIX', if_true: files('memfd.c'))
 util_ss.add(when: 'CONFIG_WIN32', if_true: files('aio-win32.c'))
-- 
2.35.1.273.ge6ebfd0e8cbb



  parent reply	other threads:[~2022-03-23 16:08 UTC|newest]

Thread overview: 79+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-23 15:57 [PATCH 00/32] Misc cleanups marcandre.lureau
2022-03-23 15:57 ` [PATCH 01/32] meson: use chardev_ss dependencies marcandre.lureau
2022-03-23 15:57 ` [PATCH 02/32] meson: add util dependency for oslib-posix on freebsd marcandre.lureau
2022-03-23 15:57 ` [PATCH 03/32] meson: remove unneeded py3 marcandre.lureau
2022-03-23 15:57 ` [PATCH 04/32] meson: remove test-qdev-global-props dependency on testqapi marcandre.lureau
2022-03-23 15:57 ` marcandre.lureau [this message]
2022-03-23 15:57 ` [PATCH 06/32] Replace config-time define HOST_WORDS_BIGENDIAN marcandre.lureau
2022-03-23 15:57   ` marcandre.lureau
2022-03-23 17:10   ` Richard Henderson
2022-03-23 17:16   ` Richard Henderson
2022-03-23 15:57 ` [PATCH 07/32] Replace TARGET_WORDS_BIGENDIAN marcandre.lureau
2022-03-23 15:57   ` marcandre.lureau
2022-03-23 17:12   ` Richard Henderson
2022-03-23 15:57 ` [PATCH 08/32] osdep: poison {HOST,TARGET}_WORDS_BIGENDIAN marcandre.lureau
2022-03-23 17:14   ` Richard Henderson
2022-03-23 15:57 ` [PATCH 09/32] include/qapi: add g_autoptr support for qobject types marcandre.lureau
2022-03-23 18:11   ` Richard Henderson
2022-03-24 16:00   ` Markus Armbruster
2022-03-23 15:57 ` [PATCH 10/32] tests: replace free_all() usage with g_auto marcandre.lureau
2022-03-23 18:13   ` Richard Henderson
2022-03-23 15:57 ` [PATCH 11/32] Replace qemu_real_host_page variables with inlined functions marcandre.lureau
2022-03-23 15:57   ` marcandre.lureau
2022-03-25  8:34   ` Juan Quintela
2022-03-25  8:34     ` Juan Quintela
2022-03-23 15:57 ` [PATCH 12/32] qga: replace deprecated g_get_current_time() marcandre.lureau
2022-04-06 14:53   ` Damien Hedde
2022-04-07  5:46   ` Markus Armbruster
2022-04-07 11:19     ` Marc-André Lureau
2022-03-23 15:57 ` [PATCH 13/32] error-report: replace deprecated g_get_current_time() with glib >= 2.62 marcandre.lureau
2022-04-06  9:07   ` Markus Armbruster
2022-04-06  9:35     ` Marc-André Lureau
2022-04-06  9:40       ` Marc-André Lureau
2022-04-06 10:38         ` Markus Armbruster
2022-03-23 15:57 ` [PATCH 14/32] util: rename qemu-error.c to match its header name marcandre.lureau
2022-03-23 18:23   ` Richard Henderson
2022-04-07  5:49   ` Markus Armbruster
2022-03-23 15:57 ` [PATCH 15/32] error-report: use error_printf() for program prefix marcandre.lureau
2022-04-07  5:50   ` Markus Armbruster
2022-03-23 15:57 ` [PATCH 16/32] include: move TFR to osdep.h marcandre.lureau
2022-03-23 15:57 ` [PATCH 17/32] include: move qemu_write_full() declaration " marcandre.lureau
2022-03-23 15:57 ` [PATCH 18/32] include: move qemu_pipe() " marcandre.lureau
2022-03-23 15:57 ` [PATCH 19/32] include: move coroutine IO functions to coroutine.h marcandre.lureau
2022-03-23 15:57 ` [PATCH 20/32] include: move dump_in_progress() to runstate.h marcandre.lureau
2022-04-06  8:45   ` Markus Armbruster
2022-03-23 15:57 ` [PATCH 21/32] include: move C/util-related declarations to cutils.h marcandre.lureau
2022-03-23 15:57 ` [PATCH 22/32] include: move cpu_exec* declarations to cpu-common.h marcandre.lureau
2022-03-23 15:57 ` [PATCH 23/32] include: move target page bits declaration to page-vary.h marcandre.lureau
2022-03-23 18:29   ` Richard Henderson
2022-03-23 15:57 ` [PATCH 24/32] include: move progress API to qemu-progress.h marcandre.lureau
2022-03-23 18:30   ` Richard Henderson
2022-03-23 15:57 ` [PATCH 25/32] include: move qemu_get_vm_name() to sysemu.h marcandre.lureau
2022-03-23 15:57 ` [PATCH 26/32] include: move os_*() to os-foo.h marcandre.lureau
2022-03-23 18:31   ` Richard Henderson
2022-03-23 21:11   ` Philippe Mathieu-Daudé
2022-03-23 15:57 ` [PATCH 27/32] include: move page_size_init() to include/hw/core/cpu.h marcandre.lureau
2022-03-23 15:57 ` [PATCH 28/32] Move CPU softfloat unions to cpu-float.h marcandre.lureau
2022-03-23 15:57   ` marcandre.lureau
2022-03-25  8:16   ` Juan Quintela
2022-03-25  8:16     ` Juan Quintela
2022-03-23 15:57 ` [PATCH 29/32] Move fcntl_setfl() to oslib-posix marcandre.lureau
2022-03-23 18:34   ` Richard Henderson
2022-03-23 15:57 ` [PATCH 30/32] qga: remove explicit environ argument from exec/spawn marcandre.lureau
2022-03-23 18:36   ` Richard Henderson
2022-03-23 15:57 ` [PATCH 31/32] RFC: Simplify softmmu/main.c marcandre.lureau
2022-03-23 20:56   ` Akihiko Odaki
2022-03-24  7:51   ` Paolo Bonzini
2022-04-20  7:57     ` Marc-André Lureau
2022-04-20 10:48       ` Akihiko Odaki
2022-03-23 15:57 ` [PATCH 32/32] Remove qemu-common.h include from most units marcandre.lureau
2022-03-23 15:57   ` marcandre.lureau
2022-04-05 15:02   ` Ani Sinha
2022-04-05 15:02     ` Ani Sinha
2022-04-06 10:45     ` Markus Armbruster
2022-04-06 10:45       ` Markus Armbruster
2022-04-06 11:14       ` Peter Maydell
2022-04-06 11:14         ` Peter Maydell
2022-04-05 15:24   ` Warner Losh
2022-04-05 15:24     ` Warner Losh
2022-03-24  9:26 ` [PATCH 00/32] Misc cleanups Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220323155743.1585078-6-marcandre.lureau@redhat.com \
    --to=marcandre.lureau@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.