From: Felix Janda <felix.janda@posteo.de>
To: musl@lists.openwall.com
Subject: Re: Add login_tty
Date: Sat, 1 Nov 2014 23:27:29 +0100 [thread overview]
Message-ID: <20141101222729.GB5949@euler> (raw)
In-Reply-To: <20141101214503.GK22465@brightrain.aerifal.cx>
[-- Attachment #1: Type: text/plain, Size: 238 bytes --]
Rich Felker wrote:
[..]
> In that case maybe your patch is okay as-is, aside from needing to be
> factored into two changes -- one for removing useless code and the
> other for separating-out login_tty.
Both of them are attached.
Felix
[-- Attachment #2: 0001-Remove-unnecessary-allocation-of-fd-0-1-2-in-forkpty.patch --]
[-- Type: text/x-patch, Size: 1385 bytes --]
From 8b60d5f69bf3b199e4050f3b1f24b76657765ea0 Mon Sep 17 00:00:00 2001
From: Felix Janda <felix.janda@posteo.de>
Date: Sat, 1 Nov 2014 23:08:24 +0100
Subject: [PATCH 1/2] Remove unnecessary allocation of fd 0/1/2 in forkpty()
Since there are no error paths in the kernel's dup2() implementation
which might be evaded by first allocating fd 0/1/2, remove the
corresponding code.
---
src/misc/forkpty.c | 15 ++-------------
1 file changed, 2 insertions(+), 13 deletions(-)
diff --git a/src/misc/forkpty.c b/src/misc/forkpty.c
index 07f8d01..4a1ea9b 100644
--- a/src/misc/forkpty.c
+++ b/src/misc/forkpty.c
@@ -5,21 +5,12 @@
int forkpty(int *m, char *name, const struct termios *tio, const struct winsize *ws)
{
- int s, t, i, istmp[3]={0};
+ int s;
pid_t pid;
if (openpty(m, &s, name, tio, ws) < 0) return -1;
- /* Ensure before forking that we don't exceed fd limit */
- for (i=0; i<3; i++) {
- if (fcntl(i, F_GETFL) < 0) {
- t = fcntl(s, F_DUPFD, i);
- if (t<0) break;
- else if (t!=i) close(t);
- else istmp[i] = 1;
- }
- }
- pid = i==3 ? fork() : -1;
+ pid = fork();
if (!pid) {
close(*m);
setsid();
@@ -30,8 +21,6 @@ int forkpty(int *m, char *name, const struct termios *tio, const struct winsize
if (s>2) close(s);
return 0;
}
- for (i=0; i<3; i++)
- if (istmp[i]) close(i);
close(s);
if (pid < 0) close(*m);
return pid;
--
2.0.4
[-- Attachment #3: 0002-Split-off-login_tty-form-forkpty.patch --]
[-- Type: text/x-patch, Size: 1773 bytes --]
From 00f65eed60d4e805bb03bf638289d45409b93f60 Mon Sep 17 00:00:00 2001
From: Felix Janda <felix.janda@posteo.de>
Date: Sat, 1 Nov 2014 23:20:27 +0100
Subject: [PATCH 2/2] Split off login_tty form forkpty
---
include/utmp.h | 2 ++
src/misc/forkpty.c | 10 ++--------
src/misc/login_tty.c | 14 ++++++++++++++
3 files changed, 18 insertions(+), 8 deletions(-)
create mode 100644 src/misc/login_tty.c
diff --git a/include/utmp.h b/include/utmp.h
index e9ba23e..d3dcee7 100644
--- a/include/utmp.h
+++ b/include/utmp.h
@@ -43,6 +43,8 @@ void updwtmp(const char *, const struct utmp *);
#define UTMP_FILENAME _PATH_UTMP
#define WTMP_FILENAME _PATH_WTMP
+int login_tty(int);
+
#ifdef __cplusplus
}
#endif
diff --git a/src/misc/forkpty.c b/src/misc/forkpty.c
index 4a1ea9b..007b369 100644
--- a/src/misc/forkpty.c
+++ b/src/misc/forkpty.c
@@ -1,7 +1,6 @@
#include <pty.h>
+#include <utmp.h>
#include <unistd.h>
-#include <sys/ioctl.h>
-#include <fcntl.h>
int forkpty(int *m, char *name, const struct termios *tio, const struct winsize *ws)
{
@@ -13,12 +12,7 @@ int forkpty(int *m, char *name, const struct termios *tio, const struct winsize
pid = fork();
if (!pid) {
close(*m);
- setsid();
- ioctl(s, TIOCSCTTY, (char *)0);
- dup2(s, 0);
- dup2(s, 1);
- dup2(s, 2);
- if (s>2) close(s);
+ login_tty(s);
return 0;
}
close(s);
diff --git a/src/misc/login_tty.c b/src/misc/login_tty.c
new file mode 100644
index 0000000..f0be0a0
--- /dev/null
+++ b/src/misc/login_tty.c
@@ -0,0 +1,14 @@
+#include <utmp.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+
+int login_tty(int fd)
+{
+ setsid();
+ if (ioctl(fd, TIOCSCTTY, (char *)0)) return -1;
+ dup2(fd, 0);
+ dup2(fd, 1);
+ dup2(fd, 2);
+ if (fd>2) close(fd);
+ return 0;
+}
--
2.0.4
next prev parent reply other threads:[~2014-11-01 22:27 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-08-25 18:57 Felix Janda
2014-08-25 22:43 ` Rich Felker
2014-08-26 16:56 ` Felix Janda
2014-08-29 18:44 ` Felix Janda
2014-09-04 21:22 ` Szabolcs Nagy
2014-09-04 21:33 ` Rich Felker
2014-09-04 22:31 ` Justin Cormack
2014-09-05 17:23 ` Felix Janda
2014-09-05 17:29 ` Rich Felker
2014-09-05 18:52 ` Felix Janda
2014-10-31 16:19 ` Rich Felker
2014-11-01 21:15 ` Felix Janda
2014-11-01 21:45 ` Rich Felker
2014-11-01 22:07 ` Szabolcs Nagy
2014-11-01 22:27 ` Felix Janda [this message]
2014-11-01 22:43 ` Rich Felker
2014-11-01 22:56 ` Felix Janda
2014-11-02 0:09 ` Rich Felker
2014-11-02 14:19 ` Felix Janda
2014-11-02 16:28 ` Rich Felker
2014-11-02 18:56 ` Felix Janda
2014-11-02 22:28 ` Rich Felker
2014-11-03 18:29 ` Felix Janda
2014-12-21 0:58 ` Rich Felker
2014-12-21 1:15 ` Rich Felker
2014-12-21 1:38 ` Rich Felker
2014-12-21 2:59 ` Rich Felker
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=20141101222729.GB5949@euler \
--to=felix.janda@posteo.de \
--cc=musl@lists.openwall.com \
/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.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).