Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Pager: Routines to create a "more" running out of a particular file |
| 3 | * descriptor. |
| 4 | * |
| 5 | * Copyright 1987, 1988 by MIT Student Information Processing Board |
| 6 | * |
Theodore Ts'o | 06cefee | 1999-10-23 01:16:22 +0000 | [diff] [blame] | 7 | * Permission to use, copy, modify, and distribute this software and |
| 8 | * its documentation for any purpose is hereby granted, provided that |
| 9 | * the names of M.I.T. and the M.I.T. S.I.P.B. not be used in |
| 10 | * advertising or publicity pertaining to distribution of the software |
| 11 | * without specific, written prior permission. M.I.T. and the |
| 12 | * M.I.T. S.I.P.B. make no representations about the suitability of |
| 13 | * this software for any purpose. It is provided "as is" without |
| 14 | * express or implied warranty. |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 17 | #if HAVE_SECURE_GETENV |
| 18 | #define _GNU_SOURCE |
| 19 | #endif |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 20 | #ifdef HAVE_UNISTD_H |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | #endif |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 23 | #ifdef HAVE_ERRNO_H |
| 24 | #include <errno.h> |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 25 | #else |
| 26 | extern int errno; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 27 | #endif |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 28 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 29 | #include "ss_internal.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/file.h> |
| 33 | #include <signal.h> |
Theodore Ts'o | 762c7c6 | 2005-04-06 14:44:16 -0400 | [diff] [blame] | 34 | #ifdef HAVE_SYS_PRCTL_H |
| 35 | #include <sys/prctl.h> |
| 36 | #else |
| 37 | #define PR_GET_DUMPABLE 3 |
| 38 | #endif |
| 39 | #if (!defined(HAVE_PRCTL) && defined(linux)) |
| 40 | #include <sys/syscall.h> |
| 41 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 42 | |
| 43 | static char MORE[] = "more"; |
Theodore Ts'o | 546a1ff | 2002-03-07 23:52:56 -0500 | [diff] [blame] | 44 | extern char *getenv PROTOTYPE((const char *)); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 45 | |
Theodore Ts'o | 762c7c6 | 2005-04-06 14:44:16 -0400 | [diff] [blame] | 46 | char *ss_safe_getenv(const char *arg) |
| 47 | { |
Theodore Ts'o | 09f3eba | 2005-04-16 13:56:03 -0400 | [diff] [blame] | 48 | if ((getuid() != geteuid()) || (getgid() != getegid())) |
Theodore Ts'o | 762c7c6 | 2005-04-06 14:44:16 -0400 | [diff] [blame] | 49 | return NULL; |
| 50 | #if HAVE_PRCTL |
Theodore Ts'o | 583d1f8 | 2005-07-25 11:36:43 -0500 | [diff] [blame] | 51 | if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) |
Theodore Ts'o | 762c7c6 | 2005-04-06 14:44:16 -0400 | [diff] [blame] | 52 | return NULL; |
| 53 | #else |
| 54 | #if (defined(linux) && defined(SYS_prctl)) |
Theodore Ts'o | 583d1f8 | 2005-07-25 11:36:43 -0500 | [diff] [blame] | 55 | if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) |
Theodore Ts'o | 762c7c6 | 2005-04-06 14:44:16 -0400 | [diff] [blame] | 56 | return NULL; |
| 57 | #endif |
| 58 | #endif |
| 59 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 60 | #if defined(HAVE_SECURE_GETENV) |
| 61 | return secure_getenv(arg); |
| 62 | #elif defined(HAVE___SECURE_GETENV) |
Theodore Ts'o | 3af0a45 | 2005-05-08 02:15:02 -0400 | [diff] [blame] | 63 | return __secure_getenv(arg); |
Theodore Ts'o | 762c7c6 | 2005-04-06 14:44:16 -0400 | [diff] [blame] | 64 | #else |
Theodore Ts'o | 3af0a45 | 2005-05-08 02:15:02 -0400 | [diff] [blame] | 65 | return getenv(arg); |
Theodore Ts'o | 762c7c6 | 2005-04-06 14:44:16 -0400 | [diff] [blame] | 66 | #endif |
| 67 | } |
| 68 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 69 | /* |
| 70 | * this needs a *lot* of work.... |
| 71 | * |
| 72 | * run in same process |
| 73 | * handle SIGINT sensibly |
| 74 | * allow finer control -- put-page-break-here |
| 75 | */ |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 76 | |
| 77 | #ifndef NO_FORK |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 78 | int ss_pager_create(void) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 79 | { |
| 80 | int filedes[2]; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 81 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 82 | if (pipe(filedes) != 0) |
| 83 | return(-1); |
| 84 | |
| 85 | switch(fork()) { |
| 86 | case -1: |
| 87 | return(-1); |
| 88 | case 0: |
| 89 | /* |
| 90 | * Child; dup read half to 0, close all but 0, 1, and 2 |
| 91 | */ |
| 92 | if (dup2(filedes[0], 0) == -1) |
| 93 | exit(1); |
| 94 | ss_page_stdin(); |
| 95 | default: |
| 96 | /* |
| 97 | * Parent: close "read" side of pipe, return |
| 98 | * "write" side. |
| 99 | */ |
| 100 | (void) close(filedes[0]); |
| 101 | return(filedes[1]); |
| 102 | } |
| 103 | } |
| 104 | #else /* don't fork */ |
| 105 | int ss_pager_create() |
| 106 | { |
| 107 | int fd; |
| 108 | fd = open("/dev/tty", O_WRONLY, 0); |
| 109 | return fd; |
| 110 | } |
| 111 | #endif |
| 112 | |
Theodore Ts'o | 935a123 | 2009-04-22 14:48:59 -0400 | [diff] [blame] | 113 | static int write_all(int fd, char *buf, size_t count) |
| 114 | { |
| 115 | ssize_t ret; |
| 116 | int c = 0; |
| 117 | |
| 118 | while (count > 0) { |
| 119 | ret = write(fd, buf, count); |
| 120 | if (ret < 0) { |
| 121 | if ((errno == EAGAIN) || (errno == EINTR)) |
| 122 | continue; |
| 123 | return -1; |
| 124 | } |
| 125 | count -= ret; |
| 126 | buf += ret; |
| 127 | c += ret; |
| 128 | } |
| 129 | return c; |
| 130 | } |
| 131 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 132 | void ss_page_stdin(void) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 133 | { |
| 134 | int i; |
Theodore Ts'o | 91835c1 | 2003-03-30 22:26:13 -0500 | [diff] [blame] | 135 | sigset_t mask; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 136 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 137 | for (i = 3; i < 32; i++) |
| 138 | (void) close(i); |
| 139 | (void) signal(SIGINT, SIG_DFL); |
Theodore Ts'o | 91835c1 | 2003-03-30 22:26:13 -0500 | [diff] [blame] | 140 | sigprocmask(SIG_BLOCK, 0, &mask); |
| 141 | sigdelset(&mask, SIGINT); |
| 142 | sigprocmask(SIG_SETMASK, &mask, 0); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 143 | if (_ss_pager_name == (char *)NULL) { |
Theodore Ts'o | 762c7c6 | 2005-04-06 14:44:16 -0400 | [diff] [blame] | 144 | if ((_ss_pager_name = ss_safe_getenv("PAGER")) == (char *)NULL) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 145 | _ss_pager_name = MORE; |
| 146 | } |
| 147 | (void) execlp(_ss_pager_name, _ss_pager_name, (char *) NULL); |
| 148 | { |
| 149 | /* minimal recovery if pager program isn't found */ |
| 150 | char buf[80]; |
| 151 | register int n; |
| 152 | while ((n = read(0, buf, 80)) > 0) |
Theodore Ts'o | 935a123 | 2009-04-22 14:48:59 -0400 | [diff] [blame] | 153 | write_all(1, buf, n); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 154 | } |
| 155 | exit(errno); |
| 156 | } |