]> granicus.if.org Git - sudo/blob - src/regress/ttyname/check_ttyname.c
Add SPDX-License-Identifier to files.
[sudo] / src / regress / ttyname / check_ttyname.c
1 /*
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (c) 2013-2016 Todd C. Miller <Todd.Miller@sudo.ws>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18
19 #include <config.h>
20
21 #include <sys/types.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #ifdef HAVE_STRING_H
25 # include <string.h>
26 #endif /* HAVE_STRING_H */
27 #ifdef HAVE_STRINGS_H
28 # include <strings.h>
29 #endif /* HAVE_STRINGS_H */
30 #include <unistd.h>
31 #include <limits.h>
32 #include <errno.h>
33
34 #include "sudo_compat.h"
35 #include "sudo_fatal.h"
36 #include "sudo_util.h"
37 #include "sudo_debug.h"
38
39 __dso_public int main(int argc, char *argv[]);
40
41 int sudo_debug_instance = SUDO_DEBUG_INSTANCE_INITIALIZER;
42 extern char *get_process_ttyname(char *name, size_t namelen);
43
44 int
45 main(int argc, char *argv[])
46 {
47     char *tty_libc = NULL, *tty_sudo = NULL;
48     char pathbuf[PATH_MAX];
49     int ret = 1;
50
51     initprogname(argc > 0 ? argv[0] : "check_ttyname");
52
53     /* Lookup tty name using kernel info if possible. */
54     if (get_process_ttyname(pathbuf, sizeof(pathbuf)) != NULL)
55         tty_sudo = pathbuf;
56
57 #if defined(HAVE_KINFO_PROC2_NETBSD) || \
58     defined(HAVE_KINFO_PROC_OPENBSD) || \
59     defined(HAVE_KINFO_PROC_FREEBSD) || \
60     defined(HAVE_KINFO_PROC_44BSD) || \
61     defined(HAVE__TTYNAME_DEV) || defined(HAVE_STRUCT_PSINFO_PR_TTYDEV) || \
62     defined(HAVE_PSTAT_GETPROC) || defined(__linux__)
63
64     /* Lookup tty name attached to stdin via libc. */
65     tty_libc = ttyname(STDIN_FILENO);
66 #endif
67
68     /* Compare libc and kernel ttys. */
69     if (tty_libc != NULL && tty_sudo != NULL) {
70         if (strcmp(tty_libc, tty_sudo) == 0)
71             ret = 0;
72     } else if (tty_libc == NULL && tty_sudo == NULL) {
73         ret = 0;
74     }
75
76     if (ret == 0) {
77         printf("%s: OK (%s)\n", getprogname(), tty_sudo ? tty_sudo : "none");
78     } else if (tty_libc == NULL) {
79         printf("%s: SKIP (%s)\n", getprogname(), tty_sudo ? tty_sudo : "none");
80         ret = 0;
81     } else {
82         printf("%s: FAIL %s (sudo) vs. %s (libc)\n", getprogname(),
83             tty_sudo ? tty_sudo : "none", tty_libc ? tty_libc : "none");
84     }
85
86     return ret;
87 }