update styles to match kernel C, better fprintf

This commit is contained in:
tengel 2024-03-20 09:30:28 -05:00
parent e52e95905d
commit a63a514d3e

View file

@ -1,12 +1,12 @@
/*
limitcheck.c - given a user, drop privileges and check ulimits
Compile: gcc -o limitcheck limitcheck.c
Use: sudo ./limitcheck <user>
Author: Troy Engel
License: APL 2.0
*/
* limitcheck.c - given a user, drop privileges and check ulimits
*
* Compile: gcc -o limitcheck limitcheck.c
* Use: sudo ./limitcheck <user>
*
* Author: Troy Engel
* License: APL 2.0
*/
#include <sys/types.h>
#include <sys/resource.h>
@ -16,7 +16,13 @@
#include <pwd.h>
#include <grp.h>
static int drop_privileges(const char *user) {
/*
* portions of this cribbed from tcpdump; it's important it's done in
* a very specific order - initgroups for validity, then group, then
* user.
*/
static int drop_privileges(const char *user)
{
struct passwd *pw = NULL;
pw = getpwnam(user);
if (pw) {
@ -36,44 +42,67 @@ static int drop_privileges(const char *user) {
}
}
static void print_limits() {
/* /usr/include/bits/resource.h */
static void print_limits()
{
struct rlimit rl;
fprintf(stdout, " %11s %11s\n", "soft", "hard");
fprintf(stdout, "%15s %11s %11s\n",
" ", "soft", "hard");
/*
* this could be done as a tagged, multidimensional array using a struct
* with an enum, the work involved in C is a pain - the natural state in
* C is all array elements are the same type and size, mixed int + char
* are horrible to code around. doing it one by one is simpler code.
*/
getrlimit(RLIMIT_CORE, &rl);
fprintf(stdout, "core file size: %11d %11d\n", (long long int)rl.rlim_cur,
fprintf(stdout, "%15s %11d %11d\n",
"core file size:",
(long long int)rl.rlim_cur,
(long long int)rl.rlim_max);
getrlimit(RLIMIT_CPU, &rl);
fprintf(stdout, "cpu time: %11d %11d\n", (long long int)rl.rlim_cur,
fprintf(stdout, "%15s %11d %11d\n",
"cpu time:",
(long long int)rl.rlim_cur,
(long long int)rl.rlim_max);
getrlimit(RLIMIT_DATA, &rl);
fprintf(stdout, "data seg size: %11d %11d\n", (long long int)rl.rlim_cur,
fprintf(stdout, "%15s %11d %11d\n",
"data seg size:",
(long long int)rl.rlim_cur,
(long long int)rl.rlim_max);
getrlimit(RLIMIT_FSIZE, &rl);
fprintf(stdout, "file size: %11d %11d\n", (long long int)rl.rlim_cur,
fprintf(stdout, "%15s %11d %11d\n",
"file size:",
(long long int)rl.rlim_cur,
(long long int)rl.rlim_max);
getrlimit(RLIMIT_NOFILE, &rl);
fprintf(stdout, "open files: %11d %11d\n", (long long int)rl.rlim_cur,
fprintf(stdout, "%15s %11d %11d\n",
"open files:",
(long long int)rl.rlim_cur,
(long long int)rl.rlim_max);
getrlimit(RLIMIT_STACK, &rl);
fprintf(stdout, "stack size: %11d %11d\n", (long long int)rl.rlim_cur,
fprintf(stdout, "%15s %11d %11d\n",
"stack size:",
(long long int)rl.rlim_cur,
(long long int)rl.rlim_max);
getrlimit(RLIMIT_AS, &rl);
fprintf(stdout, "memory size: %11d %11d\n", (long long int)rl.rlim_cur,
fprintf(stdout, "%15s %11d %11d\n",
"memory size:",
(long long int)rl.rlim_cur,
(long long int)rl.rlim_max);
}
int main(int argc, char **argv) {
/* we only accept one argument - the user to check */
/* we only accept one argument - the user to check */
int main(int argc, char **argv)
{
if (argc == 2) {
if (drop_privileges(argv[1]) == 0) {
print_limits();