| 1 |
/* gawkmisc.c --- miscellaneous gawk routines that are OS specific. |
| 2 |
|
| 3 |
Copyright (C) 1986, 1988, 1989, 1991 - 1998, 2001 - 2004, 2011, |
| 4 |
2021, 2022, 2023, 2025, |
| 5 |
the Free Software Foundation, Inc. |
| 6 |
|
| 7 |
This program is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 3, or (at your option) |
| 10 |
any later version. |
| 11 |
|
| 12 |
This program is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with this program; if not, write to the Free Software Foundation, |
| 19 |
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
| 20 |
|
| 21 |
#ifdef __CYGWIN__ |
| 22 |
#ifdef __MSYS__ |
| 23 |
#define WIN32_LEAN_AND_MEAN |
| 24 |
#include <windows.h> |
| 25 |
#include <sys/cygwin.h> |
| 26 |
#endif |
| 27 |
#include <io.h> /* for declaration of setmode(). */ |
| 28 |
#endif |
| 29 |
|
| 30 |
#ifdef HAVE_SYS_PERSONALITY_H // for linux |
| 31 |
#include <sys/personality.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#ifdef HAVE_SPAWN_H |
| 35 |
#include <spawn.h> // for macos |
| 36 |
#endif |
| 37 |
|
| 38 |
const char quote = '\''; |
| 39 |
const char *defpath = DEFPATH; |
| 40 |
const char *deflibpath = DEFLIBPATH; |
| 41 |
const char envsep = ':'; |
| 42 |
|
| 43 |
#ifndef INVALID_HANDLE |
| 44 |
/* FIXME: is this value for INVALID_HANDLE correct? */ |
| 45 |
#define INVALID_HANDLE -1 |
| 46 |
#endif |
| 47 |
|
| 48 |
/* gawk_name --- pull out the "gawk" part from how the OS called us */ |
| 49 |
|
| 50 |
const char * |
| 51 |
gawk_name(const char *filespec) |
| 52 |
{ |
| 53 |
const char *p; |
| 54 |
|
| 55 |
/* "path/name" -> "name" */ |
| 56 |
p = strrchr(filespec, '/'); |
| 57 |
return (p == NULL ? (char *) filespec : p + 1); |
| 58 |
} |
| 59 |
|
| 60 |
/* os_arg_fixup --- fixup the command line */ |
| 61 |
|
| 62 |
void |
| 63 |
os_arg_fixup(int *argcp, char ***argvp) |
| 64 |
{ |
| 65 |
/* no-op */ |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
/* os_devopen --- open special per-OS devices */ |
| 70 |
|
| 71 |
int |
| 72 |
os_devopen(const char *name, int flag) |
| 73 |
{ |
| 74 |
/* no-op */ |
| 75 |
return INVALID_HANDLE; |
| 76 |
} |
| 77 |
|
| 78 |
/* optimal_bufsize --- determine optimal buffer size */ |
| 79 |
|
| 80 |
/* |
| 81 |
* Enhance this for debugging purposes, as follows: |
| 82 |
* |
| 83 |
* Always stat the file, stat buffer is used by higher-level code. |
| 84 |
* |
| 85 |
* if (AWKBUFSIZE == "exact") |
| 86 |
* return the file size |
| 87 |
* else if (AWKBUFSIZE == a number) |
| 88 |
* always return that number |
| 89 |
* else |
| 90 |
* if the size is < default_blocksize |
| 91 |
* return the size |
| 92 |
* else |
| 93 |
* return default_blocksize |
| 94 |
* end if |
| 95 |
* endif |
| 96 |
* |
| 97 |
* Hair comes in an effort to only deal with AWKBUFSIZE |
| 98 |
* once, the first time this routine is called, instead of |
| 99 |
* every time. Performance, dontyaknow. |
| 100 |
*/ |
| 101 |
|
| 102 |
size_t |
| 103 |
optimal_bufsize(int fd, struct stat *stb) |
| 104 |
{ |
| 105 |
char *val; |
| 106 |
static size_t env_val = 0; |
| 107 |
static bool first = true; |
| 108 |
static bool exact = false; |
| 109 |
|
| 110 |
/* force all members to zero in case OS doesn't use all of them. */ |
| 111 |
memset(stb, '\0', sizeof(struct stat)); |
| 112 |
|
| 113 |
/* always stat, in case stb is used by higher level code. */ |
| 114 |
if (fstat(fd, stb) == -1) |
| 115 |
fatal("can't stat fd %d (%s)", fd, strerror(errno)); |
| 116 |
|
| 117 |
if (first) { |
| 118 |
first = false; |
| 119 |
|
| 120 |
if ((val = getenv("AWKBUFSIZE")) != NULL) { |
| 121 |
if (strcmp(val, "exact") == 0) |
| 122 |
exact = true; |
| 123 |
else if (isdigit((unsigned char) *val)) { |
| 124 |
for (; *val && isdigit((unsigned char) *val); val++) |
| 125 |
env_val = (env_val * 10) + *val - '0'; |
| 126 |
|
| 127 |
return env_val; |
| 128 |
} |
| 129 |
} |
| 130 |
} else if (! exact && env_val > 0) |
| 131 |
return env_val; |
| 132 |
/* else |
| 133 |
fall through */ |
| 134 |
|
| 135 |
/* |
| 136 |
* System V.n, n < 4, doesn't have the file system block size in the |
| 137 |
* stat structure. So we have to make some sort of reasonable |
| 138 |
* guess. We use stdio's BUFSIZ, since that is what it was |
| 139 |
* meant for in the first place. |
| 140 |
*/ |
| 141 |
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
| 142 |
#define DEFBLKSIZE (stb->st_blksize > 0 ? stb->st_blksize : BUFSIZ) |
| 143 |
#else |
| 144 |
#define DEFBLKSIZE BUFSIZ |
| 145 |
#endif |
| 146 |
|
| 147 |
if (S_ISREG(stb->st_mode) /* regular file */ |
| 148 |
&& 0 < stb->st_size /* non-zero size */ |
| 149 |
&& (stb->st_size < DEFBLKSIZE /* small file */ |
| 150 |
|| exact)) /* or debugging */ |
| 151 |
return stb->st_size; /* use file size */ |
| 152 |
|
| 153 |
return DEFBLKSIZE; |
| 154 |
} |
| 155 |
|
| 156 |
/* ispath --- return true if path has directory components */ |
| 157 |
|
| 158 |
int |
| 159 |
ispath(const char *file) |
| 160 |
{ |
| 161 |
return (strchr(file, '/') != NULL); |
| 162 |
} |
| 163 |
|
| 164 |
/* isdirpunct --- return true if char is a directory separator */ |
| 165 |
|
| 166 |
int |
| 167 |
isdirpunct(int c) |
| 168 |
{ |
| 169 |
return (c == '/'); |
| 170 |
} |
| 171 |
|
| 172 |
/* os_close_on_exec --- set close on exec flag, print warning if fails */ |
| 173 |
|
| 174 |
void |
| 175 |
os_close_on_exec(int fd, const char *name, const char *what, const char *dir) |
| 176 |
{ |
| 177 |
int curflags = 0; |
| 178 |
|
| 179 |
if (fd <= 2) /* sanity */ |
| 180 |
return; |
| 181 |
|
| 182 |
/* |
| 183 |
* Per POSIX, use Read/Modify/Write - get the flags, |
| 184 |
* add FD_CLOEXEC, set the flags back. |
| 185 |
*/ |
| 186 |
|
| 187 |
if ((curflags = fcntl(fd, F_GETFD)) < 0) { |
| 188 |
warning(_("%s %s `%s': could not get fd flags: (fcntl F_GETFD: %s)"), |
| 189 |
what, dir, name, strerror(errno)); |
| 190 |
return; |
| 191 |
} |
| 192 |
|
| 193 |
#ifndef FD_CLOEXEC |
| 194 |
#define FD_CLOEXEC 1 |
| 195 |
#endif |
| 196 |
|
| 197 |
curflags |= FD_CLOEXEC; |
| 198 |
|
| 199 |
if (fcntl(fd, F_SETFD, curflags) < 0) |
| 200 |
warning(_("%s %s `%s': could not set close-on-exec: (fcntl F_SETFD: %s)"), |
| 201 |
what, dir, name, strerror(errno)); |
| 202 |
} |
| 203 |
|
| 204 |
/* os_isdir --- is this an fd on a directory? */ |
| 205 |
|
| 206 |
#if ! defined(S_ISDIR) && defined(S_IFDIR) |
| 207 |
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) |
| 208 |
#endif |
| 209 |
|
| 210 |
int |
| 211 |
os_isdir(int fd) |
| 212 |
{ |
| 213 |
struct stat sbuf; |
| 214 |
|
| 215 |
return (fstat(fd, &sbuf) == 0 && S_ISDIR(sbuf.st_mode)); |
| 216 |
} |
| 217 |
|
| 218 |
/* os_isreadable --- fd can be read from */ |
| 219 |
|
| 220 |
int |
| 221 |
os_isreadable(const awk_input_buf_t *iobuf, bool *isdir) |
| 222 |
{ |
| 223 |
*isdir = false; |
| 224 |
|
| 225 |
if (iobuf->fd == INVALID_HANDLE) |
| 226 |
return false; |
| 227 |
|
| 228 |
switch (iobuf->sbuf.st_mode & S_IFMT) { |
| 229 |
case S_IFREG: |
| 230 |
case S_IFCHR: /* ttys, /dev/null, .. */ |
| 231 |
#ifdef S_IFSOCK |
| 232 |
case S_IFSOCK: |
| 233 |
#endif |
| 234 |
#ifdef S_IFIFO |
| 235 |
case S_IFIFO: |
| 236 |
#endif |
| 237 |
return true; |
| 238 |
case S_IFDIR: |
| 239 |
*isdir = true; |
| 240 |
/* fall through */ |
| 241 |
default: |
| 242 |
return false; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
/* os_is_setuid --- true if running setuid root */ |
| 247 |
|
| 248 |
int |
| 249 |
os_is_setuid() |
| 250 |
{ |
| 251 |
long uid, euid; |
| 252 |
|
| 253 |
uid = getuid(); |
| 254 |
euid = geteuid(); |
| 255 |
|
| 256 |
return (euid == 0 && euid != uid); |
| 257 |
} |
| 258 |
|
| 259 |
/* os_setbinmode --- set binary mode on file */ |
| 260 |
|
| 261 |
int |
| 262 |
os_setbinmode(int fd, int mode) |
| 263 |
{ |
| 264 |
#ifdef __CYGWIN__ |
| 265 |
setmode (fd, mode); |
| 266 |
#endif |
| 267 |
return 0; |
| 268 |
} |
| 269 |
|
| 270 |
/* os_restore_mode --- restore the original mode of the console device */ |
| 271 |
|
| 272 |
void |
| 273 |
os_restore_mode(int fd) |
| 274 |
{ |
| 275 |
/* no-op */ |
| 276 |
return; |
| 277 |
} |
| 278 |
|
| 279 |
/* os_isatty --- return true if fd is a tty */ |
| 280 |
|
| 281 |
int |
| 282 |
os_isatty(int fd) |
| 283 |
{ |
| 284 |
return isatty(fd); |
| 285 |
} |
| 286 |
|
| 287 |
/* files_are_same --- return true if files are identical */ |
| 288 |
|
| 289 |
int |
| 290 |
files_are_same(char *path, SRCFILE *src) |
| 291 |
{ |
| 292 |
struct stat st; |
| 293 |
|
| 294 |
return (stat(path, & st) == 0 |
| 295 |
&& st.st_dev == src->sbuf.st_dev |
| 296 |
&& st.st_ino == src->sbuf.st_ino); |
| 297 |
} |
| 298 |
|
| 299 |
void |
| 300 |
init_sockets(void) |
| 301 |
{ |
| 302 |
} |
| 303 |
|
| 304 |
void |
| 305 |
os_maybe_set_errno(void) |
| 306 |
{ |
| 307 |
} |
| 308 |
|
| 309 |
/* os_disable_aslr --- disable Address Space Layout Randomization */ |
| 310 |
|
| 311 |
// This for Linux and MacOS. It's not needed on other *nix systems. |
| 312 |
|
| 313 |
void |
| 314 |
os_disable_aslr(const char *persist_file, char **argv) |
| 315 |
{ |
| 316 |
#if defined(HAVE_PERSONALITY) && defined(HAVE_ADDR_NO_RANDOMIZE) |
| 317 |
// This code is Linux specific, both the reliance on /proc/self/exe |
| 318 |
// and the personality system call. |
| 319 |
if (persist_file != NULL) { |
| 320 |
const char *cp = getenv("GAWK_PMA_REINCARNATION"); |
| 321 |
|
| 322 |
if (cp == NULL) { |
| 323 |
char fullpath[BUFSIZ]; |
| 324 |
int n; |
| 325 |
|
| 326 |
if ((n = readlink("/proc/self/exe", fullpath, sizeof(fullpath)-1)) < 0) { |
| 327 |
fprintf(stderr, _("warning: /proc/self/exe: readlink: %s\n"), |
| 328 |
strerror(errno)); |
| 329 |
return; |
| 330 |
} |
| 331 |
fullpath[n] = '\0'; |
| 332 |
putenv("GAWK_PMA_REINCARNATION=true"); |
| 333 |
if (personality(PER_LINUX | ADDR_NO_RANDOMIZE) < 0) { |
| 334 |
fprintf(stderr, _("warning: personality: %s\n"), |
| 335 |
strerror(errno)); |
| 336 |
fflush(stderr); |
| 337 |
// do the exec anyway... |
| 338 |
} |
| 339 |
execv(fullpath, argv); |
| 340 |
} else |
| 341 |
(void) unsetenv("GAWK_PMA_REINCARNATION"); |
| 342 |
} |
| 343 |
#endif |
| 344 |
#ifdef HAVE__NSGETEXECUTABLEPATH |
| 345 |
// This code is for macos |
| 346 |
if (persist_file != NULL) { |
| 347 |
const char *cp = getenv("GAWK_PMA_REINCARNATION"); |
| 348 |
|
| 349 |
if (cp == NULL) { |
| 350 |
char fullpath[BUFSIZ]; |
| 351 |
int n; |
| 352 |
posix_spawnattr_t p_attr; |
| 353 |
int status; |
| 354 |
pid_t pid; |
| 355 |
extern char **environ; |
| 356 |
size_t size = BUFSIZ; |
| 357 |
|
| 358 |
memset(fullpath, 0, BUFSIZ); |
| 359 |
n = _NSGetExecutablePath(fullpath, &size); |
| 360 |
|
| 361 |
putenv("GAWK_PMA_REINCARNATION=true"); |
| 362 |
|
| 363 |
posix_spawnattr_init(&p_attr); |
| 364 |
posix_spawnattr_setflags(&p_attr, 0x100); |
| 365 |
status = posix_spawnp(&pid, fullpath, NULL, &p_attr, argv, environ); |
| 366 |
if (status == 0) { |
| 367 |
if (waitpid(pid, &status, WUNTRACED) != -1) { |
| 368 |
if (WIFEXITED(status)) |
| 369 |
exit WEXITSTATUS(status); // use original exit code |
| 370 |
} else { |
| 371 |
fprintf(stderr, _("waitpid: got exit status %#o\n"), status); |
| 372 |
exit(EXIT_FATAL); |
| 373 |
} |
| 374 |
} else { |
| 375 |
fprintf(stderr, _("fatal: posix_spawn: %s\n"), strerror(errno)); |
| 376 |
exit(EXIT_FATAL); |
| 377 |
} |
| 378 |
} else |
| 379 |
(void) unsetenv("GAWK_PMA_REINCARNATION"); |
| 380 |
} |
| 381 |
#endif |
| 382 |
} |
| 383 |
|
| 384 |
// For MSYS, restore behavior of working in text mode. |
| 385 |
#ifdef __MSYS__ |
| 386 |
void |
| 387 |
cygwin_premain0(int argc, char **argv, struct per_process *myself) |
| 388 |
{ |
| 389 |
static struct __cygwin_perfile pf[] = { |
| 390 |
{ "", O_RDONLY | O_TEXT }, |
| 391 |
/*{ "", O_WRONLY | O_BINARY },*/ |
| 392 |
{ NULL, 0 } |
| 393 |
}; |
| 394 |
cygwin_internal(CW_PERFILE, pf); |
| 395 |
} |
| 396 |
|
| 397 |
void |
| 398 |
cygwin_premain2(int argc, char **argv, struct per_process *myself) |
| 399 |
{ |
| 400 |
setmode(fileno (stdin), O_TEXT); |
| 401 |
} |
| 402 |
#endif |
| 403 |
|
| 404 |
#ifdef __CYGWIN__ |
| 405 |
size_t |
| 406 |
wcitomb (char *s, int wc, mbstate_t *ps) |
| 407 |
{ |
| 408 |
/* If s is NULL, behave as if s pointed to an internal buffer and wc |
| 409 |
was a null wide character (L''). wcrtomb will do that for us*/ |
| 410 |
if (wc <= 0xffff || !s) |
| 411 |
return wcrtomb (s, (wchar_t) wc, ps); |
| 412 |
|
| 413 |
wchar_t wc_arr[2]; |
| 414 |
const wchar_t *wcp = wc_arr; |
| 415 |
|
| 416 |
wc -= 0x10000; |
| 417 |
wc_arr[0] = (wc >> 10) + 0xd800; |
| 418 |
wc_arr[1] = (wc & 0x3ff) + 0xdc00; |
| 419 |
return wcsnrtombs (s, &wcp, 2, SIZE_MAX, ps); |
| 420 |
} |
| 421 |
#endif |