summaryrefslogtreecommitdiff
path: root/extras/ezio/tty.c
blob: ce788f2854476aef1f7828733c6c0710c9dbbcfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* Copyright (c) 2008, 2009 Nicira Networks, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, Nicira Networks gives permission
 * to link the code of its release of vswitchd with the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the same
 * license as the "OpenSSL" library), and distribute the linked
 * executables.  You must obey the GNU General Public License in all
 * respects for all of the code used other than "OpenSSL".  If you modify
 * this file, you may extend this exception to your version of the file,
 * but you are not obligated to do so.  If you do not wish to do so,
 * delete this exception statement from your version.
 *
 */

#include <config.h>
#include "extras/ezio/tty.h"
#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <stropts.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
#include "fatal-signal.h"
#include "socket-util.h"
#include "util.h"

#define THIS_MODULE VLM_tty
#include "vlog.h"

/* Get major() and minor() macros. */
#if MAJOR_IN_MKDEV
#  include <sys/mkdev.h>
#elif MAJOR_IN_SYSMACROS
#  include <sys/sysmacros.h>
#else
#  include <sys/types.h>
#  ifndef major
#    define major(dev) (((dev) >> 8) & 0xff)
#    define minor(dev) ((dev) & 0xff)
#  endif
#endif

static int
fcntl_lock(int fd)
{
    struct flock l;
    memset(&l, 0, sizeof l);
    l.l_type = F_WRLCK;
    l.l_whence = SEEK_SET;
    l.l_start = 0;
    l.l_len = 0;
    return fcntl(fd, F_SETLK, &l) == -1 ? errno : 0;
}

static int
remove_lockfile(const char *name)
{
    char buffer[BUFSIZ];
    ssize_t n;
    pid_t pid;
    int fd;

    /* Remove existing lockfile. */
    fd = open(name, O_RDWR);
    if (fd < 0) {
        if (errno == ENOENT) {
            return 0;
        } else {
            VLOG_ERR("%s: open: %s", name, strerror(errno));
            return errno;
        }
    }

    /* Read lockfile. */
    n = read(fd, buffer, sizeof buffer - 1);
    if (n < 0) {
        int error = errno;
        VLOG_ERR("%s: read: %s", name, strerror(error));
        close(fd);
        return error;
    }
    buffer[n] = '\0';
    if (n == 4 && memchr(buffer, '\0', n)) {
        int32_t x;
        memcpy(&x, buffer, sizeof x);
        pid = x;
    } else if (n >= 0) {
        pid = strtol(buffer, NULL, 10);
    }
    if (pid <= 0) {
        close(fd);
        VLOG_WARN("%s: format not recognized, treating as locked.", name);
        return EACCES;
    }

    /* Is lockfile fresh? */
    if (strstr(buffer, "fcntl")) {
        int retval = fcntl_lock(fd);
        if (retval) {
            close(fd);
            VLOG_ERR("%s: device is locked (via fcntl): %s",
                     name, strerror(retval));
            return retval;
        } else {
            VLOG_WARN("%s: removing stale lockfile (checked via fcntl)", name);
        }
    } else {
        if (!(kill(pid, 0) < 0 && errno == ESRCH)) {
            close(fd);
            VLOG_ERR("%s: device is locked (without fcntl)", name);
            return EACCES;
        } else {
            VLOG_WARN("%s: removing stale lockfile (without fcntl)", name);
        }
    }
    close(fd);

    /* Remove stale lockfile. */
    if (unlink(name)) {
        VLOG_ERR("%s: unlink: %s", name, strerror(errno));
        return errno;
    }
    return 0;
}

static int
create_lockfile(const char *name)
{
    const char *username;
    char buffer[BUFSIZ];
    struct passwd *pwd;
    mode_t old_umask;
    uid_t uid;
    int fd;

    /* Create file. */
    old_umask = umask(022);
    fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
    if (fd < 0) {
        int error = errno;
        VLOG_ERR("%s: create: %s", name, strerror(error));
        umask(old_umask);
        return error;
    }
    umask(old_umask);

    /* Lock file. */
    if (fcntl_lock(fd)) {
        int error = errno;
        close(fd);
        VLOG_ERR("%s: cannot lock: %s", name, strerror(error));
        return error;
    }

    /* Write to file. */
    uid = getuid();
    pwd = getpwuid(uid);
    username = pwd ? pwd->pw_name : "unknown";
    snprintf(buffer, sizeof buffer, "%10ld %s %.20s fcntl\n",
             (long int) getpid(), program_name, username);
    if (write(fd, buffer, strlen(buffer)) != strlen(buffer)) {
        int error = errno;
        VLOG_ERR("%s: write: %s", name, strerror(error));
        close(fd);
        unlink(name);
        return error;
    }

    /* We intentionally do not close 'fd', to avoid releasing the fcntl lock.
     * The asssumption here is that we never unlock a tty. */
    fatal_signal_add_file_to_unlink(name);

    return 0;
}

static int
do_lock(char *name)
{
    int retval = remove_lockfile(name);
    if (!retval) {
        retval = create_lockfile(name);
    }
    free(name);
    return retval;
}

int
tty_lock(const char *dev_name)
{
    struct stat s;
    char *name;
    int retval;

    /* Check that the lockfile directory exists. */
    if (stat(TTY_LOCK_DIR, &s)) {
        VLOG_ERR("%s: stat: %s", TTY_LOCK_DIR, strerror(errno));
        return errno;
    }

    /* First lock by device number. */
    if (stat(dev_name, &s)) {
        VLOG_ERR("%s: stat: %s", dev_name, strerror(errno));
        return errno;
    }
    retval = do_lock(xasprintf("%s/LK.%03d.%03d.%03d", TTY_LOCK_DIR,
                               major(s.st_dev),
                               major(s.st_rdev), minor(s.st_rdev)));
    if (retval) {
        return retval;
    }

    /* Then lock by device name. */
    if (!strncmp(dev_name, "/dev/", 5)) {
        char *cp;

        name = xasprintf("%s/%s", TTY_LOCK_DIR, dev_name + 5);
        for (cp = name + strlen(dev_name) + 1; *cp; cp++) {
            if (*cp == '/') {
                *cp = '_';
            }
        }
    } else {
        char *slash = strrchr(dev_name, '/');
        name = xasprintf ("%s/%s", TTY_LOCK_DIR, slash ? slash + 1 : dev_name);
    }
    return do_lock(name);
}

struct saved_termios {
    int fd;
    struct termios tios;
};

static void
restore_termios(void *s_)
{
    struct saved_termios *s = s_;
    tcsetattr(s->fd, TCSAFLUSH, &s->tios);
}

int
tty_set_raw_mode(int fd, speed_t speed)
{
    if (isatty(fd)) {
        struct termios tios;
        struct saved_termios *s;

        if (tcgetattr(fd, &tios) < 0) {
            return errno;
        }

        s = xmalloc(sizeof *s);
        s->fd = dup(fd);
        if (s->fd < 0) {
            int error = errno;
            VLOG_WARN("dup failed: %s", strerror(error));
            free(s);
            return errno;
        }
        s->tios = tios;
        fatal_signal_add_hook(restore_termios, s, true);

        tios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
                          | INLCR | IGNCR | ICRNL | IXON);
        tios.c_oflag &= ~OPOST;
        tios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
        tios.c_cflag &= ~(CSIZE | PARENB);
        tios.c_cflag |= CS8;
        if (speed != B0) {
            cfsetispeed(&tios, speed);
            cfsetospeed(&tios, speed);
        }
        if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
            return errno;
        }
    }
    return set_nonblocking(fd);
}

int
tty_open_master_pty(void)
{
    int retval;
    int fd;

    fd = posix_openpt(O_RDWR | O_NOCTTY);
    if (fd < 0) {
        int error = errno;
        VLOG_WARN("posix_openpt failed: %s", strerror(error));
        close(fd);
        return -error;
    }

    if (grantpt(fd) < 0) {
        int error = errno;
        VLOG_WARN("grantpt failed: %s", strerror(error));
        close(fd);
        return -error;
    }

    if (unlockpt(fd) < 0) {
        int error = errno;
        VLOG_WARN("unlockpt failed: %s", strerror(error));
        close(fd);
        return -error;
    }

    retval = set_nonblocking(fd);
    if (retval) {
        VLOG_WARN("set_nonblocking failed: %s", strerror(retval));
        close(fd);
        return retval;
    }

    return fd;
}

int
tty_fork_child(int master_fd, char *argv[])
{
    int retval = fork();
    if (!retval) {
        char *slave_name;
        int slave_fd;
        int fd;

        /* Running in child process. */
        fatal_signal_fork();

        /* Open pty slave as controlling terminal. */
        setsid();
        slave_name = ptsname(master_fd);
        if (slave_name == NULL) {
            ovs_fatal(errno, "ptsname");
        }
        slave_fd = open(slave_name, O_RDWR);
        if (isastream(slave_fd)
            && (ioctl(slave_fd, I_PUSH, "ptem") < 0
                || ioctl(slave_fd, I_PUSH, "ldterm") < 0)) {
            ovs_fatal(errno, "STREAMS ioctl");
        }

        /* Make pty slave stdin, stdout. */
        if (dup2(slave_fd, STDIN_FILENO) < 0
            || dup2(slave_fd, STDOUT_FILENO) < 0
            || dup2(slave_fd, STDERR_FILENO) < 0) {
            ovs_fatal(errno, "dup2");
        }

        /* Close other file descriptors. */
        for (fd = 3; fd < 20; fd++) {
            close(fd);
        }

        /* Set terminal type. */
        setenv("TERM", "ezio3", true);

        /* Invoke subprocess. */
        execvp(argv[0], argv);
        ovs_fatal(errno, "execvp");
    } else if (retval > 0) {
        /* Running in parent process. */
        return 0;
    } else {
        /* Fork failed. */
        VLOG_WARN("fork failed: %s", strerror(errno));
        return errno;
    }
}

int
tty_set_window_size(int fd UNUSED, int rows UNUSED, int columns UNUSED)
{
#ifdef TIOCGWINSZ
    struct winsize win;
    win.ws_row = rows;
    win.ws_col = columns;
    win.ws_xpixel = 0;
    win.ws_ypixel = 0;
    if (ioctl(fd, TIOCSWINSZ, &win) == -1) {
        return errno;
    }
#else
#error
#endif
    return 0;
}