update styles to match kernel C, better fprintf
This commit is contained in:
parent
e52e95905d
commit
a63a514d3e
1 changed files with 50 additions and 21 deletions
71
limitcheck.c
71
limitcheck.c
|
|
@ -1,12 +1,12 @@
|
||||||
/*
|
/*
|
||||||
limitcheck.c - given a user, drop privileges and check ulimits
|
* limitcheck.c - given a user, drop privileges and check ulimits
|
||||||
|
*
|
||||||
Compile: gcc -o limitcheck limitcheck.c
|
* Compile: gcc -o limitcheck limitcheck.c
|
||||||
Use: sudo ./limitcheck <user>
|
* Use: sudo ./limitcheck <user>
|
||||||
|
*
|
||||||
Author: Troy Engel
|
* Author: Troy Engel
|
||||||
License: APL 2.0
|
* License: APL 2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/resource.h>
|
#include <sys/resource.h>
|
||||||
|
|
@ -16,7 +16,13 @@
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#include <grp.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;
|
struct passwd *pw = NULL;
|
||||||
pw = getpwnam(user);
|
pw = getpwnam(user);
|
||||||
if (pw) {
|
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;
|
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);
|
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);
|
(long long int)rl.rlim_max);
|
||||||
|
|
||||||
getrlimit(RLIMIT_CPU, &rl);
|
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);
|
(long long int)rl.rlim_max);
|
||||||
|
|
||||||
getrlimit(RLIMIT_DATA, &rl);
|
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);
|
(long long int)rl.rlim_max);
|
||||||
|
|
||||||
getrlimit(RLIMIT_FSIZE, &rl);
|
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);
|
(long long int)rl.rlim_max);
|
||||||
|
|
||||||
getrlimit(RLIMIT_NOFILE, &rl);
|
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);
|
(long long int)rl.rlim_max);
|
||||||
|
|
||||||
getrlimit(RLIMIT_STACK, &rl);
|
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);
|
(long long int)rl.rlim_max);
|
||||||
|
|
||||||
getrlimit(RLIMIT_AS, &rl);
|
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);
|
(long long int)rl.rlim_max);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
/* we only accept one argument - the user to check */
|
||||||
|
int main(int argc, char **argv)
|
||||||
/* we only accept one argument - the user to check */
|
{
|
||||||
if (argc == 2) {
|
if (argc == 2) {
|
||||||
if (drop_privileges(argv[1]) == 0) {
|
if (drop_privileges(argv[1]) == 0) {
|
||||||
print_limits();
|
print_limits();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue