From nobody Mon Nov 14 11:23:06 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4N9n3t37lGz4dMMv; Mon, 14 Nov 2022 11:23:06 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4N9n3t2TzHz3hJM; Mon, 14 Nov 2022 11:23:06 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1668424986; 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; bh=InZM20NW0d92QU1x9pm8yO/SuulE4+oOXN6Ln14sp3g=; b=RumqHsWmBbBDwf4VlsfGdpkyORmoPnlW03JynmpSRzLSGr3nU7E0B2BYTeYpyV8ueoQDln QulmoWJd8Hlf66sT3oiRC6mN1Z/GK+fhDHFOLN1n1WS6sJIfLHLT/ROQGd5p3M59Q2DQol P4BW0LQJZhH91/p66K0i6EePhffLqZDKXIY1G41h8JguFXtQ1iKMXjNYdBvVll0+6RjQCr q7dVbyQ6Yokx3dKGuOdNEFxY9h1soVqFUJy7Y5VCPyOgcrgJjb8Xayw3h7w8Pw8q9Zt9Dy RgYkLOdUn0bVJI1Hx9ecyHu1IsX3FIXoG61bK4xmSwh+ipNGZWT6V5eB07PRzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1668424986; 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; bh=InZM20NW0d92QU1x9pm8yO/SuulE4+oOXN6Ln14sp3g=; b=y4wygQiVS4Zxa99Aq5jjzOfba4N20PU8YGCJVQvSvmxWU/VxqTMimI08744UbMiuG44m+s 0Bgsvhuqkr+bwqU78g9DDflUVdY9fGsaYtc5Qioa+WjW4sajsgGP9xRc6y3Sq4Hm1cfDD2 5yWAwIABn+eheGn4ZRjFdGFlccU4HbO/EnYODnSiqO7ySJ0VXQnGmfHjLka2DtX1853Z8p TevbeBnj79dj4rGYP7GESvndTxM01anmEjsqHoQPaax60M0Vp5pyHIti7lpG9zL+L09AO7 QLe/bvIndmhF06syNBhgO0oKN3hYaT4C0UauZhHre1hEhd2NvrZU1QdmKjm3KA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1668424986; a=rsa-sha256; cv=none; b=OzXwBqRDecq5BYww1PIXW6cg2d8GPhl6QPpm5PtwH6c3avK2bxHEvQoqcCyy/w38wmHRHS SEgGTr2OxHFMYXz/7Y+vHnPOW+m7t6nMf8UlwD6mYY450bZkcUIzScvh526dXw+Uw4Itwu QhdoGTK0/sSsbMGr8jL927Ja8TI/kGCTpJPIvwU6JRs+3vwr1viWRhFeBMcmBireOSQVGG 3mTl4ctNr/jHbwnBpUWGt9P4oJ8T4Fhv4Tbm7ykH40FLmZFFBKXM8KTnqwRmiVF2oqI9Vs aWJOUn33A/rfUlebWSiZ4QYqi0aJhmrNjdPAkIdXOZ66I7OX9XJFZkRyJbE7Bg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4N9n3t1YKFzgCL; Mon, 14 Nov 2022 11:23:06 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 2AEBN6Ih040576; Mon, 14 Nov 2022 11:23:06 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2AEBN6ul040575; Mon, 14 Nov 2022 11:23:06 GMT (envelope-from git) Date: Mon, 14 Nov 2022 11:23:06 GMT Message-Id: <202211141123.2AEBN6ul040575@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: =?utf-8?Q?Dag-Erling=20Sm=C3=B8rgrav?= Subject: git: d488e8d0372d - stable/13 - getty: code cleanup, part 1 List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: d488e8d0372d5c79fb2cb1aa6c41a59fbfb62479 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=d488e8d0372d5c79fb2cb1aa6c41a59fbfb62479 commit d488e8d0372d5c79fb2cb1aa6c41a59fbfb62479 Author: Dag-Erling Smørgrav AuthorDate: 2022-11-04 13:23:33 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2022-11-14 11:22:42 +0000 getty: code cleanup, part 1 * Avoid unnecessary use of `unsigned char *` * Use explicit casts when assigning `unsigned char *` to `char *` or vice versa * Drop unused global variables (and fix memory leak in `gettable()`) * Use `snprintf()` instead of `strcpy()` + `strcat()` * Drop spurious braces in switch Sponsored by: Klara, Inc. Obtained from: Apple OSS Distributions (in part) Differential Revision: https://reviews.freebsd.org/D37263 (cherry picked from commit 8ad7a14ab49b93240676e15f404354775be931f4) getty: code cleanup, part 2 * Clean up whitespace * Reindent Sponsored by: Klara, Inc. (cherry picked from commit f285f41498ebe0bfc1dbe47d67af12ec2d131521) getty: Avoid NULL deref if stdin is not a tty. Sponsored by: Klara, Inc. Obtained from: Apple OSS Distributions Differential Revision: https://reviews.freebsd.org/D37265 (cherry picked from commit eb4d86d529e2523a19fd7454976923319954a49d) --- libexec/getty/chat.c | 32 +++++------ libexec/getty/extern.h | 2 +- libexec/getty/init.c | 6 +- libexec/getty/main.c | 149 +++++++++++++++++++++++++------------------------ libexec/getty/subr.c | 24 ++++---- 5 files changed, 109 insertions(+), 104 deletions(-) diff --git a/libexec/getty/chat.c b/libexec/getty/chat.c index 0f2def37b028..0a348d6b2ebb 100644 --- a/libexec/getty/chat.c +++ b/libexec/getty/chat.c @@ -59,10 +59,10 @@ static volatile int alarmed = 0; static void chat_alrm(int); static int chat_unalarm(void); -static int getdigit(unsigned char **, int, int); +static int getdigit(char **, int, int); static char **read_chat(char **); static char *cleanchr(char **, unsigned char); -static const char *cleanstr(const unsigned char *, int); +static const char *cleanstr(const char *, int); static const char *result(int); static int chat_expect(const char *); static int chat_send(char const *); @@ -104,7 +104,7 @@ chat_unalarm(void) */ static int -getdigit(unsigned char **ptr, int base, int max) +getdigit(char **ptr, int base, int max) { int i, val = 0; char * q; @@ -149,10 +149,10 @@ read_chat(char **chatstr) p != NULL; p = strtok(NULL, ws)) { - unsigned char *q, *r; + char *q, *r; /* Read escapes */ - for (q = r = (unsigned char *)p; *r; ++q) + for (q = r = p; *r; ++q) { if (*q == '\\') { @@ -248,7 +248,7 @@ cleanchr(char **buf, unsigned char ch) l = 2; ch &= 0x7f; } else - l = 0; + l = 0; if (ch < 32) { tmp[l++] = '^'; @@ -271,9 +271,9 @@ cleanchr(char **buf, unsigned char ch) */ static const char * -cleanstr(const unsigned char *s, int l) +cleanstr(const char *s, int l) { - static unsigned char * tmp = NULL; + static char * tmp = NULL; static int tmplen = 0; if (tmplen < l * 4 + 1) @@ -344,7 +344,7 @@ chat_expect(const char *str) if (chat_debug & CHATDEBUG_RECEIVE) syslog(LOG_DEBUG, "chat_recv '%s' m=%d", - cleanchr(NULL, ch), i); + cleanchr(NULL, ch), i); if (ch == str[i]) got[i++] = ch; @@ -365,9 +365,9 @@ chat_expect(const char *str) } } alarm(0); - chat_unalarm(); - alarmed = 0; - free(got); + chat_unalarm(); + alarmed = 0; + free(got); } } @@ -399,13 +399,13 @@ chat_send(char const *str) unsigned char ch = (unsigned char)*str++; if (alarmed) - r = 3; + r = 3; else if (ch == PAUSE_CH) usleep(500000); /* 1/2 second */ else { usleep(10000); /* be kind to modem */ if (write(STDOUT_FILENO, &ch, 1) != 1) - r = alarmed ? 3 : 2; + r = alarmed ? 3 : 2; } } alarm(0); @@ -414,7 +414,7 @@ chat_send(char const *str) } if (chat_debug & CHATDEBUG_SEND) - syslog(LOG_DEBUG, "chat_send %s", result(r)); + syslog(LOG_DEBUG, "chat_send %s", result(r)); return r; } @@ -481,7 +481,7 @@ getty_chat(char *scrstr, int timeout, int debug) } if (chat_debug & CHATDEBUG_MISC) - syslog(LOG_DEBUG, "getty_chat %s", result(r)); + syslog(LOG_DEBUG, "getty_chat %s", result(r)); } return r; diff --git a/libexec/getty/extern.h b/libexec/getty/extern.h index fbee272d109c..6872acb4f6f7 100644 --- a/libexec/getty/extern.h +++ b/libexec/getty/extern.h @@ -48,7 +48,7 @@ const char *autobaud(void); int delaybits(void); void edithost(const char *); void gendefaults(void); -void gettable(const char *, char *); +void gettable(const char *); void makeenv(char *[]); const char *portselector(void); void set_ttydefaults(int); diff --git a/libexec/getty/init.c b/libexec/getty/init.c index ec911ca7223f..79b9601a2be1 100644 --- a/libexec/getty/init.c +++ b/libexec/getty/init.c @@ -34,7 +34,7 @@ static char sccsid[] = "@(#)from: init.c 8.1 (Berkeley) 6/4/93"; #endif static const char rcsid[] = - "$FreeBSD$"; + "$FreeBSD$"; #endif /* not lint */ /* @@ -53,7 +53,7 @@ static char nullstr[] = ""; static char loginprg[] = _PATH_LOGIN; static char datefmt[] = "%+"; -#define M(a) (&omode.c_cc[a]) +#define M(a) (char *)(&omode.c_cc[a]) struct gettystrs gettystrs[] = { { "nx", NULL, NULL }, /* next table */ @@ -123,7 +123,7 @@ struct gettynums gettynums[] = { { "dc", 0, 0, 0 }, /* debug chat script value */ { NULL, 0, 0, 0 } }; - + struct gettyflags gettyflags[] = { { "ht", 0, 0, 0, 0 }, /* has tabs */ diff --git a/libexec/getty/main.c b/libexec/getty/main.c index 743a0508c561..fad3811adcf6 100644 --- a/libexec/getty/main.c +++ b/libexec/getty/main.c @@ -58,6 +58,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -100,10 +101,7 @@ static char name[MAXLOGNAME*3]; static char ttyn[32]; #define OBUFSIZ 128 -#define TABBUFSIZ 512 -static char defent[TABBUFSIZ]; -static char tabent[TABBUFSIZ]; static const char *tname; static char *env[128]; @@ -191,7 +189,7 @@ main(int argc, char *argv[]) gethostname(hostname, sizeof(hostname) - 1); hostname[sizeof(hostname) - 1] = '\0'; if (hostname[0] == '\0') - strcpy(hostname, "Amnesiac"); + snprintf(hostname, sizeof(hostname), "Amnesiac"); /* * Limit running time to deal with broken or dead lines. @@ -201,7 +199,7 @@ main(int argc, char *argv[]) limit.rlim_cur = GETTY_TIMEOUT; (void)setrlimit(RLIMIT_CPU, &limit); - gettable("default", defent); + gettable("default"); gendefaults(); tname = "default"; if (argc > 1) @@ -214,83 +212,87 @@ main(int argc, char *argv[]) * that the file descriptors are already set up for us. * J. Gettys - MIT Project Athena. */ - if (argc <= 2 || strcmp(argv[2], "-") == 0) - strcpy(ttyn, ttyname(STDIN_FILENO)); - else { - strcpy(ttyn, _PATH_DEV); - strlcat(ttyn, argv[2], sizeof(ttyn)); - if (strcmp(argv[0], "+") != 0) { - chown(ttyn, 0, 0); - chmod(ttyn, 0600); - revoke(ttyn); - - /* - * Do the first scan through gettytab. - * Terminal mode parameters will be wrong until - * defttymode() called, but they're irrelevant for - * the initial setup of the terminal device. - */ - dogettytab(); - - /* - * Init or answer modem sequence has been specified. - */ - if (IC || AC) { - if (!opentty(ttyn, O_RDWR|O_NONBLOCK)) - exit(1); - defttymode(); - setttymode(1); + if (argc <= 2 || strcmp(argv[2], "-") == 0) { + char *n = ttyname(STDIN_FILENO); + if (n == NULL) { + syslog(LOG_ERR, "ttyname: %m"); + exit(1); } + snprintf(ttyn, sizeof(ttyn), "%s", n); + } else { + snprintf(ttyn, sizeof(ttyn), "%s%s", _PATH_DEV, argv[2]); + if (strcmp(argv[0], "+") != 0) { + chown(ttyn, 0, 0); + chmod(ttyn, 0600); + revoke(ttyn); + + /* + * Do the first scan through gettytab. + * Terminal mode parameters will be wrong until + * defttymode() called, but they're irrelevant for + * the initial setup of the terminal device. + */ + dogettytab(); - if (IC) { - if (getty_chat(IC, CT, DC) > 0) { - syslog(LOG_ERR, "modem init problem on %s", ttyn); - (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode); - exit(1); + /* + * Init or answer modem sequence has been specified. + */ + if (IC || AC) { + if (!opentty(ttyn, O_RDWR|O_NONBLOCK)) + exit(1); + defttymode(); + setttymode(1); } - } - - if (AC) { - fd_set rfds; - struct timeval to; - int i; - FD_ZERO(&rfds); - FD_SET(0, &rfds); - to.tv_sec = RT; - to.tv_usec = 0; - i = select(32, &rfds, NULL, NULL, RT ? &to : NULL); - if (i < 0) { - syslog(LOG_ERR, "select %s: %m", ttyn); - } else if (i == 0) { - syslog(LOG_NOTICE, "recycle tty %s", ttyn); - (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode); - exit(0); /* recycle for init */ + if (IC) { + if (getty_chat(IC, CT, DC) > 0) { + syslog(LOG_ERR, "modem init problem on %s", ttyn); + (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode); + exit(1); + } } - i = getty_chat(AC, CT, DC); - if (i > 0) { - syslog(LOG_ERR, "modem answer problem on %s", ttyn); - (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode); - exit(1); + + if (AC) { + fd_set rfds; + struct timeval to; + int i; + + FD_ZERO(&rfds); + FD_SET(0, &rfds); + to.tv_sec = RT; + to.tv_usec = 0; + i = select(32, &rfds, NULL, NULL, RT ? &to : NULL); + if (i < 0) { + syslog(LOG_ERR, "select %s: %m", ttyn); + } else if (i == 0) { + syslog(LOG_NOTICE, "recycle tty %s", ttyn); + (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode); + exit(0); /* recycle for init */ + } + i = getty_chat(AC, CT, DC); + if (i > 0) { + syslog(LOG_ERR, "modem answer problem on %s", ttyn); + (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode); + exit(1); + } + } else { /* maybe blocking open */ + if (!opentty(ttyn, O_RDWR | (NC ? O_NONBLOCK : 0 ))) + exit(1); } - } else { /* maybe blocking open */ - if (!opentty(ttyn, O_RDWR | (NC ? O_NONBLOCK : 0 ))) - exit(1); } - } } defttymode(); for (;;) { /* - * if a delay was specified then sleep for that + * if a delay was specified then sleep for that * number of seconds before writing the initial prompt */ if (first_sleep && DE) { - sleep(DE); - /* remove any noise */ - (void)tcflush(STDIN_FILENO, TCIOFLUSH); + sleep(DE); + /* remove any noise */ + (void)tcflush(STDIN_FILENO, TCIOFLUSH); } first_sleep = 0; @@ -318,7 +320,7 @@ main(int argc, char *argv[]) char * cp; while ((cp = get_line(fd)) != NULL) { - putf(cp); + putf(cp); } close(fd); } @@ -439,7 +441,7 @@ opentty(const char *tty, int flags) return 0; sleep(60); } - if (login_tty(i) < 0) { + if (login_tty(i) < 0) { if (daemon(0,0) < 0) { syslog(LOG_ERR,"daemon: %m"); close(i); @@ -555,7 +557,7 @@ getname(void) See RFC1662. Derived from code from Michael Hancock, and Erik 'PPP' Olson, - */ + */ if (PP && (cs == PPP_FRAME)) { ppp_state = 1; @@ -564,7 +566,7 @@ getname(void) } else if (ppp_state == 2 && cs == PPP_ESCAPE) { ppp_state = 3; } else if ((ppp_state == 2 && cs == PPP_CONTROL) - || (ppp_state == 3 && cs == PPP_CONTROL_ESCAPED)) { + || (ppp_state == 3 && cs == PPP_CONTROL_ESCAPED)) { ppp_state = 4; } else if (ppp_state == 4 && cs == PPP_LCP_HI) { ppp_state = 5; @@ -761,7 +763,7 @@ putf(const char *cp) puts(editedhost); break; - case 'd': { + case 'd': t = (time_t)0; (void)time(&t); if (Lo) @@ -785,7 +787,6 @@ putf(const char *cp) case 'v': puts(kerninfo.version); break; - } case '%': putchr('%'); @@ -801,9 +802,9 @@ putf(const char *cp) static void dogettytab(void) { - + /* Read the database entry. */ - gettable(tname, tabent); + gettable(tname); /* * Avoid inheriting the parity values from the default entry diff --git a/libexec/getty/subr.c b/libexec/getty/subr.c index 9958d193aa60..68682df6d5bd 100644 --- a/libexec/getty/subr.c +++ b/libexec/getty/subr.c @@ -34,7 +34,7 @@ static char sccsid[] = "@(#)from: subr.c 8.1 (Berkeley) 6/4/93"; #endif static const char rcsid[] = - "$FreeBSD$"; + "$FreeBSD$"; #endif /* not lint */ /* @@ -60,8 +60,9 @@ static const char rcsid[] = * Get a table entry. */ void -gettable(const char *name, char *buf) +gettable(const char *name) { + char *buf = NULL; struct gettystrs *sp; struct gettynums *np; struct gettyflags *fp; @@ -155,6 +156,7 @@ gettable(const char *name, char *buf) fp->value = 1 ^ fp->invrt; } } + free(buf); } void @@ -186,8 +188,8 @@ setdefaults(void) for (sp = gettystrs; sp->field; sp++) if (!sp->value) - sp->value = !sp->defalt ? sp->defalt - : strdup(sp->defalt); + sp->value = !sp->defalt ? + sp->defalt : strdup(sp->defalt); for (np = gettynums; np->field; np++) if (!np->set) np->value = np->defalt; @@ -202,13 +204,15 @@ charnames[] = { &SU, &DS, &RP, &FL, &WE, &LN, 0 }; +#define CV(a) (char *)(&tmode.c_cc[a]) + static char * charvars[] = { - &tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR], - &tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP], - &tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP], - &tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD], - &tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], 0 + CV(VERASE), CV(VKILL), CV(VINTR), + CV(VQUIT), CV(VSTART), CV(VSTOP), + CV(VEOF), CV(VEOL), CV(VSUSP), + CV(VDSUSP), CV(VREPRINT), CV(VDISCARD), + CV(VWERASE), CV(VLNEXT), 0 }; void @@ -505,7 +509,7 @@ edithost(const char *pattern) * In case of any errors, or if the pattern did not match, pass * the original hostname as is. */ - copyasis: +copyasis: strlcpy(editedhost, HN, sizeof(editedhost)); }