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
|
/*
* util.c
*
* Copyright (c) 1990, 1991, John W. Eaton.
*
* You may distribute under the terms of the GNU General Public
* License as specified in the file COPYING that comes with the man
* distribution.
*
* John W. Eaton
* jwe@che.utexas.edu
* Department of Chemical Engineering
* The University of Texas at Austin
* Austin, Texas 78712
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include "util.h"
#include "gripedefs.h"
int debug = 0;
char * progname = 0;
/*
* Extract last element of a name like /foo/bar/baz.
*/
char *
mkprogname (char *s) {
char *t;
t = strrchr (s, '/');
if (t == (char *)NULL)
t = s;
else
t++;
return my_strdup (t);
}
/*
* Is file a nonempty and newer than file b?
*
* case:
*
* a newer than b returns 1
* a older than b returns 0
* stat on a fails or a empty returns -1
* stat on b fails or b empty returns -2
* both fail or empty returns -3
*/
int
is_newer (char *fa, char *fb) {
struct stat fa_sb;
struct stat fb_sb;
register int fa_stat;
register int fb_stat;
register int status = 0;
fa_stat = stat (fa, &fa_sb);
if (fa_stat != 0 || fa_sb.st_size == 0)
status = 1;
fb_stat = stat (fb, &fb_sb);
if (fb_stat != 0 || fb_sb.st_size == 0)
status |= 2;
if (status != 0)
return -status;
return (fa_sb.st_mtime > fb_sb.st_mtime);
}
int ruid, rgid, euid, egid, suid;
void
get_permissions (void) {
ruid = getuid();
euid = geteuid();
rgid = getgid();
egid = getegid();
suid = (ruid != euid || rgid != egid);
}
void
no_privileges (void) {
if (suid) {
#ifndef __CYGWIN32__
setreuid(ruid, ruid);
setregid(rgid, rgid);
#endif
suid = 0;
}
}
/*
* Unfortunately, Linux libc system() ignores SIGINT and SIGQUIT
* The consequence is that "man -K" cannot be interrupted.
* Here a private copy that doesn't fiddle with signals.
* (But see below.)
*/
static int
system0 (char *command) {
int pid, pid2, status;
pid = fork();
if (pid == -1) {
perror(progname);
fatal (CANNOT_FORK, command);
}
if (pid == 0) {
char *argv[4];
argv[0] = "sh";
argv[1] = "-c";
argv[2] = command;
argv[3] = 0;
execv("/bin/sh", argv); /* was: execve(*,*,environ); */
exit(127);
}
do {
pid2 = wait(&status);
if (pid2 == -1)
return -1;
} while(pid2 != pid);
return status;
}
/*
* What to do upon an interrupt? Experience shows that
* if we exit immediately, sh notices that its child has
* died and will try to fiddle with the tty.
* Simultaneously, also less will fiddle with the tty,
* resetting the mode before exiting.
* This leads to undesirable races. So, we catch SIGINT here
* and exit after the child has exited.
*/
static int interrupted = 0;
static void catch_int(int a) {
interrupted = 1;
}
static int
system1 (char *command) {
void (*prev_handler)(int) = signal (SIGINT,catch_int);
int ret = system0(command);
if (interrupted)
exit(1);
signal(SIGINT,prev_handler);
return ret;
}
static int
my_system (char *command) {
int pid, pid2, status, stat;
if (!suid)
return system1 (command);
#ifdef _POSIX_SAVED_IDS
/* we need not fork */
setuid(ruid);
setgid(rgid);
status = system1(command);
setuid(euid);
setgid(egid);
return (WIFEXITED(status) ? WEXITSTATUS(status) : 127);
#endif
fflush(stdout); fflush(stderr);
pid = fork();
if (pid == -1) {
perror(progname);
fatal (CANNOT_FORK, command);
}
if (pid == 0) {
setuid(ruid);
setgid(rgid);
status = system1 (command);
exit(WIFEXITED(status) ? WEXITSTATUS(status) : 127);
}
pid2 = wait (&stat);
if (pid2 == -1) {
perror(progname);
fatal (WAIT_FAILED, command); /* interrupted? */
}
if (pid2 != pid)
fatal (GOT_WRONG_PID);
if (WIFEXITED(stat) && WEXITSTATUS(stat) != 127)
return WEXITSTATUS(stat);
fatal (CHILD_TERMINATED_ABNORMALLY, command);
return -1; /* not reached */
}
FILE *
my_popen(const char *command, const char *type) {
FILE *r;
if (!suid)
return popen(command, type);
#ifdef _POSIX_SAVED_IDS
setuid(ruid);
setgid(rgid);
r = popen(command, type);
setuid(euid);
setgid(egid);
return r;
#endif
no_privileges();
return popen(command, type);
}
/*
* Attempt a system () call.
*/
int
do_system_command (char *command, int silent) {
int status = 0;
/*
* If we're debugging, don't really execute the command -- you never
* know what might be in that mangled string :-O.
*/
if (debug == 1)
gripe (NO_EXEC, command);
else
status = my_system (command);
if (status && !silent)
gripe (SYSTEM_FAILED, command, status);
return status;
}
char *
my_malloc (int n) {
char *s = (char*) malloc(n);
if (!s)
fatal (OUT_OF_MEMORY, n);
return s;
}
char *
my_strdup (char *s) {
char *t = my_malloc(strlen(s) + 1);
strcpy(t, s);
return t;
}
/******************* modifyed by Wang XinKai ,1999.3.19 *********/
char *getmsg(int n)
{
static char buffer[100];
buffer[0] = 0;
//sprintf(buffer,"error, errorcode = %d\n",n);
return buffer;
}
void
gripe (int n, ...) {
va_list p;
va_start(p, n);
vfprintf (stderr, getmsg(n), p);
va_end(p);
fflush (stderr);
}
void
fatal (int n, ...) {
va_list p;
fprintf (stderr, "%s: ", progname);
va_start(p, n);
vfprintf (stderr, getmsg(n), p);
va_end(p);
exit (1);
}
/************************** modify end ************************/
|