dump: avoid extra jumps dump_one_task

It looks better and the next patches adds more checks
before memory allocations.

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
dump: use a virtual pid in names of zombie's core files

All per task images contains a virtual pid.

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
mm: mark a vma as stack, if a value of sp is in it

/proc/PID/maps can contains not up to date information about a stack vma.
A kernel marks a VMA as stack, if thread_struct->usersp is in it,
but usersp is updated, when a process calls a syscall.

This problem is occured, when we try to dump/restore a process in a loop.
When a restorer resumes a process, a restorer vma will be marked as stack.

A thread stack should not be marked as stack, because its vma is mapped
w/o MAP_GROWSDOWN.

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Acked-by: Cyrill Gorcunov <gorcunov@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
zdtm: Add pty01 test

Two slaves on same pty.

Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
zdtm: Add pty02 test

The test is tricky -- it opens master in task 1
and dup it in task 2, then close in task 1 and
do C/R.

Thus we need to setup SID of master terminal
somehow in children, which usually fails in
currnt tty restore implementation.

Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
zdtm: Add pty03 test

Non-opened control terminal

* Set a control terminal
* Close a file descriptor on it
* Dump/restore
* Open the control terminal (/dev/tty)
* Check, that it connected to the master end

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
zdtm: Add pty04 test

Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
zdtm: Add tty05 test

For testing locked/exclusive ptys

Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
app-test: check screen application

Add test for screen c/r.

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
Add separate lists for master/slave fd on restore

When we will be restoring ttys we need that restore
procedure for master peers will be yielded earlier
than for slave peers due to ttys specifics. With this
commit we introduce @tty_masters and @tty_slaves
lists which will allow us to order tty file restore
procesure.

Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
tty: Add checkpoint/restore for unix terminals v4

Usually the PTYs represent a pair of links -- master peer and slave
peer. Master peer must be opened before slave. Internally, when kernel
creates master peer it also generates a slave interface in a form of
/dev/pts/N, where N is that named pty "index". Master/slave connection
unambiguously identified by this index.

Still, one master can carry multiple slaves -- for example a user opens
one master via /dev/ptmx and appropriate /dev/pts/N in sequence.
The result will be the following

master
`- slave 1
`- slave 2

both slave will have same master index but different file descriptors.
Still inside the kernel pty parameters are same for both slaves. Thus
only one slave parameters should be restored, there is no need to carry
all parameters for every slave peer we've found.

Not yet addressed problems:

- Also there is a second hardness with slave peers -- at moment of
  restore the master end might be already closed for any reason so
  to resolve such problem we need to open a fake master peer with
  proper index and hook a slave on it, then we close master peer.

- Need to figure out how to deal with ttys which have some
  data in buffers not yet flushed, at moment this data will
  be simply lost during c/r

- Need to restore control terminals

- Need to fetch tty flags such as exclusive/packet-mode

[ avagin@:
   - contol terminals restoration code complete rework
     (not addressed in this patch)
   - overall code redesign and simplification
]

v4:
 - drop redundant pid from dump_chrdev
 - make sure optional fown is passed on regular ptys
 - add a comments about zeroifying termios
 - get rid of redundant empty line in files.c

Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
CC: Andrey Vagin <avagin@openvz.org>
tty: Add show_tty method

Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
check: Add pty checking code

Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
service-fd: Eliminate redundant getrlimit call

We will need service fd testing in our file engine,
thus we need to escape calling getrlimit which
degradates performance.

For this sake we simly cache rlim value and reuse it
where needed.

Also is_service_fd() helper is added to be able to
verify fd, which we will need in tty code.

Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
tty: Restore controlling terminal

Because setting up a controlling terminal is just
call for ioctl over interface (/dev/pts/N) under
proper context (where proper context means a task
with sid matched to one the terminal has), we do
some tricks

 - we dump controlling tty with unique id and very
   few parameters (that's why a number of parameters
   in tty.proto made optional)

 - at restore time we find a task which has the sid
   we need and assign ctl-tty-id there obtained in
   previous step

 - then we generate fake fdinfo entry and bind it to
   ctl-tty-id, thus when file engine finds this fd
   it'll know that something special is done inside
   open_fd handler and simply close this fd once
   open_fd finished

 - in our open_fd handled we simply yield ioctl
   on /dev/pts/N and that's all

It's important to note that since /dev/pts/N is a slave
peer an appropriate master peer will be already created
by file engine.

Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>