30 Commits

Author SHA1 Message Date
David Herrmann
3bcce95be3 pty: add kmscon_pty_signal()
Allow to send arbitrary signals to the foreground process group of the
pty. Linux supports the TIOCSIG ioctl so we actually do not need to
implement this on our own, yeah!

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-03-27 13:35:41 +02:00
David Herrmann
84b87c320b pty: avoid calling callbacks twice
We shouldn't call the close-cb twice. Therefore, correctly free the FD on
first error, however, keep the signal-callback to get nice log-messages.
Only when the user calls *_close() we eventually stop everything.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-03-25 19:14:30 +02:00
David Herrmann
3935a77439 pty: fix using right fd
Use right fd when spawning child process.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-03-25 18:50:30 +02:00
David Herrmann
5cdf94711b pty: random fixes
Fix some random coding-style issues and adjust to new eloop-rules.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-03-25 18:37:45 +02:00
David Herrmann
e0546adcaa pty: correctly terminate when child_setup fails
We currently return "ret" when child setup fails, however, we should
rather call exit(). Also avoid cleaning up as this is impossible here
anyway.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-03-25 18:16:05 +02:00
David Herrmann
0392fb8c49 pty: fix wrong fd check
FDs may be 0 so check for >= and not > for fds.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-03-25 18:10:27 +02:00
David Herrmann
6bcc73666a pty: fix race when starting child
We should do fork() as the last operation when starting the child.
Currently, if our eloop-add() fails, we have a started child but return
failure to the caller. Therefore, the child will stay alive but we do not
use it.

We now perform all startup correctly before fork()'ing so we are always
safe when starting the child.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-03-25 18:06:33 +02:00
David Herrmann
9e87b853ab use pthread_sigmask instead of sigprocmask
pthread is already in our vmem due to our dependencies so link to it
explicitly and use pthread_sigmask to avoid buggy sigprocmask in
multi-threaded applications.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-03-25 17:53:16 +02:00
David Herrmann
b1cb8c918c pty: use reentrant ptsname_r
Avoid static storage in pty subsystem and use the ptsname_r extension.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-03-25 16:18:03 +02:00
David Herrmann
8773331331 pty: introduce --term
Allow setting TERM to different values than the default with a
command-line switch.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-03-24 18:15:50 +01:00
David Herrmann
ec2dfaa244 pty: add --login option
Allow to specify a separate login-program that is executed instead of the
default. All arguments specified after --login are considered argv[] of
the new process and not parsed by the library.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-03-24 18:10:55 +01:00
David Herrmann
d98dba57d3 pty: fix log statements
Remove new-lines and add LOG_SUBSYSTEM.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-03-24 17:44:42 +01:00
David Herrmann
4b9bc46226 Fix some signed/unsigned warnings
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-03-24 17:04:42 +01:00
David Herrmann
4790d9f7ab eloop: move prefix to "ev_" instead of "kmscon_"
The long "kmscon_" prefix is horrible so move everything to "ev_" now.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-03-20 19:08:29 +01:00
Ran Benita
d8cf293c94 build: implicitly include config.h
config.h is meant to be around in all files, as it contains stuff like
_GNU_SOURCE, NDEBUG and HAVE_*.

Make these definitions available without needing to #include config.h
every single time.

Signed-off-by: Ran Benita <ran234@gmail.com>
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-03-20 19:00:16 +01:00
David Herrmann
7a31fa98d6 pty: avoid 4KB buffers on the stack
Allocate the IO-buffer dynamically on the heap for every pty object to
avoid 4KB objects on the stack. This may not be a problem now but we
might get stack overflows later if we continue to use such huge arrays
on the stack.

This doesn't affect the runtime performance as the buffer is still
allocated only once on pty-creation.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-01-30 22:01:32 +01:00
Ran Benita
9d4cd66631 pty: increase input buffer size
Increase the buffer size to match the kernel's. This should guarantee
(practically) that we read() everything queued up for us.

This speeds us up considerably when running something like
$ find /
Such a process spends most of its time blocking on write() waiting for
us. By increasing the buffer size we avoid repeated
read pty -> draw screen -> read pty
cycles, which take most of our time.

To time it, before:
real  0m22.588s
user  0m00.000s
sys   0m00.020s

After:
real  0m00.680s
user  0m00.023s
sys   0m00.000s

Signed-off-by: Ran Benita <ran234@gmail.com>
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-01-30 20:51:27 +01:00
David Herrmann
f63eaffc95 pty: remove unneeded variable in setup_child
We can use "ret" instead of "saved_errno" here.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-01-28 17:43:26 +01:00
David Herrmann
d8d0e24a22 pty: move fork_pty_child to setup_child
The name fork_pty_child was misleading. It doesn't fork at all but
instead sets up the child's environment.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-01-28 17:39:20 +01:00
David Herrmann
31fc374065 pty: avoid FIONREAD ioctl
This ioctl is really not needed and performs needles kernel context
switches. We can simply read into our buffer without checking how many
data is available.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-01-28 17:23:49 +01:00
David Herrmann
229b4bfde2 pty: buffer data between pty and child
If the child returns EWOULDBLOCK on write we need to save the input in a
buffer to avoid loosing data. We need to work in non-blocking mode to
avoid UI hangs so we simply use the new ring-buffer object to store
data.

This also changes the callback behavior. The pty is no longer closed
implicitely so the owner must call pty_close now even if the close
callback is called. This avoids circular callbacks.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-01-28 17:17:20 +01:00
David Herrmann
ecb7ddbc29 pty: merge input and close callbacks
There is no reason to keep two callbacks as the caller always registers
both. Hence, we can use a shared callback. Reading length 0 means closed
like reading from an fd.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-01-28 14:03:52 +01:00
David Herrmann
da0623c260 terminal, pty: pass eloop reference on obj creation
There is no reason to pass the eloop reference late at terminal/pty
open. If we pass it early on object creation we will always have a valid
reference and can remove several code paths.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-01-28 13:56:13 +01:00
David Herrmann
9b4c6c5542 pty: move output_cb to input_cb
The output_cb handles output from the pty but it actually makes more
sense to call it input_cb as we are handling input to our console here.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-01-28 13:45:06 +01:00
David Herrmann
cda62d3e60 pty: move pty_input to pty_write
pty_write actually makes more sense as we are writing to the other end
and not reading input.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-01-28 13:44:25 +01:00
David Herrmann
c3d2ea3eb2 pty: fix style issue
Fix small indentation style issue.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-01-28 13:43:09 +01:00
David Herrmann
57bb28b1b4 pty: merge fork_pty and pty_spawn
pty_spawn was just a short wrapper so we can merge both into one
function.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-01-28 13:22:44 +01:00
Ran Benita
d480ec1030 pty: unblock all signals before exec'ing the child
When we register signals in the eloop we also block them in our signal
mask. The signal mask is inherited by the child. Therefore, if the child
does not reset its mask (e.g. bash and most normal processes), it will
not receive any of the signals that we handle. So for example C-c
(SIGINT) does nothing in the child process.

We now unblock all signals before we exec the child.

It's also worth noting that if we _ignore_ a signal -
sigaction(SIG_IGN) - this is also inherited and we must reset it to
default. However, we do not ignore signals so this is unneeded.

Here is some more discussion on signalfd and this problem:
https://lwn.net/Articles/415684/

Signed-off-by: Ran Benita <ran234@gmail.com>
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-01-28 12:54:22 +01:00
David Herrmann
bd09f38a4d log: move log_warning to log_warn
log_warn is much shorter and we already use log_err instead of log_error
so this is more consistent now.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-01-24 15:29:55 +01:00
Ran Benita
c63a5f2b95 Add pseudo terminal support
This commit adds a new pty object.

The pty object takes care of all pseudo terminal handling, reading and
writing. It can be opened and closed, and notify through callbacks when
input arrives or the child process exits/dies. It can also receive input
and pass it along to the child process.

There is not yet any real VTE processing, so we display raw escape
codes and so on. However, this should provide immediate feedback for
any further vte development, as we start to act like a real terminal
emulator.

Signed-off-by: Ran Benita <ran234@gmail.com>
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-01-23 14:02:43 +01:00