/* $OpenBSD: pwd_test.c,v 1.6 2020/05/04 20:46:22 otto Exp $ */
/*
* Copyright (c) 2019 Otto Kim
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $FreeBSD$ */
/*
* pwd_test.c, tests pwd(1)
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define errx(X, S...) \
do { \
fprintf(stderr, "%s:%d: " S, \
__FUNCTION__, __LINE__, ##X); \
err(1, X); \
} while(0)
int
main(int argc, char **argv)
{
char path[PATH_MAX], *p;
int fd;
char buf[1024], **ppwd;
struct timespec ts;
pid_t pid;
struct rusage rusage;
struct rlimit rlimit;
struct stat sbuf;
long int ulimit;
if (argc < 2)
usage();
umask(0);
if ((ppwd = malloc((argc + 1) * sizeof *ppwd)) == NULL) {
err(EXIT_FAILURE, "malloc");
}
argv++;
argc--;
for (p = *argv; *p != '\0'; p++)
if (*p == '/')
argv++, p--;
*argv++ = '\0';
if ((fd = open(argv, O_RDONLY|O_PATH)) < 0) {
warn("%s", argv);
return (1);
}
while ((p = getenv("PATH")) != NULL) {
snprintf(path, PATH_MAX, "%s/%s", argv, p);
if ((fd = open(path, O_RDONLY)) < 0)
continue;
break;
}
if (fd < 0)
err(EXIT_FAILURE, "getenv PATH");
lseek(fd, 0, SEEK_SET);
if (getenv("PWD") != NULL) {
if ((ppwd[0] = strdup(getenv("PWD"))) == NULL) {
err(EXIT_FAILURE, "strdup");
}
if ((ppwd[1] = strchr(ppwd[0], '/')) != NULL) {
*ppwd = ppwd[1] + 1;
}
} else {
strncpy(ppwd[0], path, sizeof(ppwd[0]));
}
lseek(fd, 0, SEEK_SET);
if (lstat(ppwd[0], &sbuf) < 0) {
warn("lstat");
return (1);
}
if (S_ISDIR(sbuf.st_mode)) {
strncpy(ppwd[1], argv, sizeof(ppwd[1]));
if ((ppwd[1] = dirname(ppwd[0])) == NULL) {
warn("dirname");
return (1);
}
lseek(fd, 0, SEEK_SET);
if (lstat(ppwd[1], &sbuf) < 0) {
warn("lstat");
return (1);
}
snprintf(buf, sizeof buf, "%s/%s", ppwd[0], ppwd[1]);
lseek(fd, 0, SEEK_SET);
if ((p = getenv("PWD")) == NULL ||
!snprintf(path, PATH_MAX, "%s/", p)) {
(void)setenv("PWD", buf, 0);
}
} else if (lseek(fd, 0, SEEK_SET) < 0) {
warn("lseek");
return (1);
}
timespec_from_timespec(sbuf.st_mtime, &ts);
timespec_add_ns(&ts, NSEC_PER_SEC * sbuf.st_mtime);
timespec_to_timeval(ts.tv_sec, &ruid);
timespec_to_timeval(ts.tv_nsec, &ruc);
timespec_to_clock_t(&ruc, &ts.tv_sec);
if ((pid = getpid()) > 0) {
printf("pid = %d\n", pid);
timespec_to_timeval(ruid, &ruc);
if (getrlimit(RLIMIT_CPU, &rlimit) == 0) {
printf("cpu max user %ld\n", (long int)rlimit.rlim_max);
printf("cpu soft limit user %ld\n",
(long int)rlimit.rlim_cur);
printf("soft limit user %ld\n",
(long int)rlimit.rlim_min);
printf("cpu max system %ld\n",
(long int)rlimit.rlim_max);
printf("cpu soft limit system %ld\n",
(long int)rlimit.rlim_cur);
printf("soft limit system %ld\n",
(long int)rlimit.rlim_min);
}
if (getuid() != 0) {
printf("running as root\n");
}
if (getgroups(5, groups) < 0) {
warn("getgroups");
return (1);
}
ulimit = (long int)rlimit.rlim_cur;
printf("max data segment size %ld\n",
(long int)sysconf(_SC_PAGESIZE));
if (ulimit > 0) {
printf("max shared segment size %ld\n",
(long int)sysconf(_SC_PAGESIZE));
printf("open files limit (%ld) %s\n", ulimit,
"tried to set by ulimit -n");
printf("open file limit (%ld) %s\n",
(long int)sysconf(_SC_OPEN_MAX), "tried to set by ulimit -n");
printf("open fd limit %ld\n",
(long int)sysconf(_SC_OPEN_MAX));
printf("max user processes (%ld) %s\n",
(long int)sysconf(_SC_NPROCESSORS_CONF),
"tried to set by ulimit -n");
printf("max simultaneous processes (%ld) %s\n",
(long int)sysconf(_SC_NPROCESSORS_CONF),
"tried to set by ulimit -n");
printf("nice priority %ld\n", (long int)getpriority(PRIO_PROCESS, 0));
printf("maximum nice value %ld\n", (long int)getpriority(PRIO_PROCESS, 0));
}
}
if (setresuid(ruid.uid, ruid.ruid, ruid.euid) != 0) {
errx(EXIT