1997-04-26 17:21:57 +04:00
|
|
|
/*
|
|
|
|
* Pager: Routines to create a "more" running out of a particular file
|
|
|
|
* descriptor.
|
|
|
|
*
|
|
|
|
* Copyright 1987, 1988 by MIT Student Information Processing Board
|
|
|
|
*
|
1999-10-23 05:16:22 +04:00
|
|
|
* Permission to use, copy, modify, and distribute this software and
|
|
|
|
* its documentation for any purpose is hereby granted, provided that
|
|
|
|
* the names of M.I.T. and the M.I.T. S.I.P.B. not be used in
|
|
|
|
* advertising or publicity pertaining to distribution of the software
|
|
|
|
* without specific, written prior permission. M.I.T. and the
|
|
|
|
* M.I.T. S.I.P.B. make no representations about the suitability of
|
|
|
|
* this software for any purpose. It is provided "as is" without
|
|
|
|
* express or implied warranty.
|
1997-04-26 17:21:57 +04:00
|
|
|
*/
|
|
|
|
|
1997-04-26 17:58:21 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
1997-04-26 17:34:30 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
1997-04-26 17:58:21 +04:00
|
|
|
#ifdef HAVE_ERRNO_H
|
|
|
|
#include <errno.h>
|
1997-04-29 20:17:09 +04:00
|
|
|
#else
|
|
|
|
extern int errno;
|
1997-04-26 17:58:21 +04:00
|
|
|
#endif
|
1997-04-26 17:34:30 +04:00
|
|
|
|
1997-04-26 17:21:57 +04:00
|
|
|
#include "ss_internal.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <signal.h>
|
2005-04-06 22:44:16 +04:00
|
|
|
#ifdef HAVE_SYS_PRCTL_H
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#else
|
|
|
|
#define PR_GET_DUMPABLE 3
|
|
|
|
#endif
|
|
|
|
#if (!defined(HAVE_PRCTL) && defined(linux))
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#endif
|
1997-04-26 17:21:57 +04:00
|
|
|
|
|
|
|
static char MORE[] = "more";
|
|
|
|
extern char *_ss_pager_name;
|
2002-03-08 07:52:56 +03:00
|
|
|
extern char *getenv PROTOTYPE((const char *));
|
1997-04-26 17:21:57 +04:00
|
|
|
|
2005-04-06 22:44:16 +04:00
|
|
|
char *ss_safe_getenv(const char *arg)
|
|
|
|
{
|
2005-04-16 21:56:03 +04:00
|
|
|
if ((getuid() != geteuid()) || (getgid() != getegid()))
|
2005-04-06 22:44:16 +04:00
|
|
|
return NULL;
|
|
|
|
#if HAVE_PRCTL
|
2005-07-25 20:36:43 +04:00
|
|
|
if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
|
2005-04-06 22:44:16 +04:00
|
|
|
return NULL;
|
|
|
|
#else
|
|
|
|
#if (defined(linux) && defined(SYS_prctl))
|
2005-07-25 20:36:43 +04:00
|
|
|
if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
|
2005-04-06 22:44:16 +04:00
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE___SECURE_GETENV
|
2005-05-08 10:15:02 +04:00
|
|
|
return __secure_getenv(arg);
|
2005-04-06 22:44:16 +04:00
|
|
|
#else
|
2005-05-08 10:15:02 +04:00
|
|
|
return getenv(arg);
|
2005-04-06 22:44:16 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1997-04-26 17:21:57 +04:00
|
|
|
/*
|
|
|
|
* this needs a *lot* of work....
|
|
|
|
*
|
|
|
|
* run in same process
|
|
|
|
* handle SIGINT sensibly
|
|
|
|
* allow finer control -- put-page-break-here
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef NO_FORK
|
2003-03-31 07:26:13 +04:00
|
|
|
int ss_pager_create(void)
|
1997-04-26 17:21:57 +04:00
|
|
|
{
|
|
|
|
int filedes[2];
|
|
|
|
|
|
|
|
if (pipe(filedes) != 0)
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
switch(fork()) {
|
|
|
|
case -1:
|
|
|
|
return(-1);
|
|
|
|
case 0:
|
|
|
|
/*
|
|
|
|
* Child; dup read half to 0, close all but 0, 1, and 2
|
|
|
|
*/
|
|
|
|
if (dup2(filedes[0], 0) == -1)
|
|
|
|
exit(1);
|
|
|
|
ss_page_stdin();
|
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* Parent: close "read" side of pipe, return
|
|
|
|
* "write" side.
|
|
|
|
*/
|
|
|
|
(void) close(filedes[0]);
|
|
|
|
return(filedes[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else /* don't fork */
|
|
|
|
int ss_pager_create()
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
fd = open("/dev/tty", O_WRONLY, 0);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void ss_page_stdin()
|
|
|
|
{
|
|
|
|
int i;
|
2003-03-31 07:26:13 +04:00
|
|
|
sigset_t mask;
|
|
|
|
|
1997-04-26 17:21:57 +04:00
|
|
|
for (i = 3; i < 32; i++)
|
|
|
|
(void) close(i);
|
|
|
|
(void) signal(SIGINT, SIG_DFL);
|
2003-03-31 07:26:13 +04:00
|
|
|
sigprocmask(SIG_BLOCK, 0, &mask);
|
|
|
|
sigdelset(&mask, SIGINT);
|
|
|
|
sigprocmask(SIG_SETMASK, &mask, 0);
|
1997-04-26 17:21:57 +04:00
|
|
|
if (_ss_pager_name == (char *)NULL) {
|
2005-04-06 22:44:16 +04:00
|
|
|
if ((_ss_pager_name = ss_safe_getenv("PAGER")) == (char *)NULL)
|
1997-04-26 17:21:57 +04:00
|
|
|
_ss_pager_name = MORE;
|
|
|
|
}
|
|
|
|
(void) execlp(_ss_pager_name, _ss_pager_name, (char *) NULL);
|
|
|
|
{
|
|
|
|
/* minimal recovery if pager program isn't found */
|
|
|
|
char buf[80];
|
|
|
|
register int n;
|
|
|
|
while ((n = read(0, buf, 80)) > 0)
|
|
|
|
write(1, buf, n);
|
|
|
|
}
|
|
|
|
exit(errno);
|
|
|
|
}
|