Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes and improvements #113

Open
wants to merge 197 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
197 commits
Select commit Hold shift + click to select a range
ef0d2e6
update code
HiGarfield Apr 17, 2022
c3047aa
avoid overflow
HiGarfield May 2, 2022
6827969
add includes
HiGarfield Sep 4, 2022
25724a4
fix buffer size
HiGarfield Sep 4, 2022
01a6873
use basename of the executable program file
HiGarfield Sep 4, 2022
20801f9
make large local arrays static
HiGarfield Sep 21, 2022
aa82a4e
simplify getppid_of
HiGarfield Sep 22, 2022
9dc6182
simplify read_process_info
HiGarfield Sep 22, 2022
26ad6c0
simplify get_pid_max
HiGarfield Sep 23, 2022
f363902
clean up code and fix build for macOS and FreeBSD
HiGarfield Sep 23, 2022
2cb93d3
add CI
HiGarfield Sep 24, 2022
86120a8
simplify Makefile
HiGarfield Dec 24, 2022
c2bbf5f
optimize code
HiGarfield Dec 24, 2022
8d95343
drop starttime attribute of a process
HiGarfield Dec 25, 2022
0499bd9
optimize pid_t
HiGarfield Dec 27, 2022
83911da
make code more readable
HiGarfield Jan 1, 2023
ae27ff8
supress unused parameter warning
HiGarfield Jan 1, 2023
17edfd2
use clock_gettime to get time
HiGarfield Jan 1, 2023
f8a3235
fix int overflow
HiGarfield Jan 1, 2023
b18a236
drop unnecessary cast
HiGarfield Jan 1, 2023
0dcf4d2
drop unnecessary memset
HiGarfield Jan 1, 2023
30ab492
use macro PROC_SUPER_MAGIC
HiGarfield Jan 2, 2023
23aea96
fix potential int overflow
HiGarfield Jan 2, 2023
95c20b7
fix compiler warnings
HiGarfield Jan 2, 2023
13645d0
improve Makefiles
HiGarfield Jan 3, 2023
44cb66d
simplify getting program_name
HiGarfield Jan 15, 2023
c44a780
fix pid_t type
HiGarfield Jan 16, 2023
ece6941
remove unnecessary cast for malloc
HiGarfield Jan 16, 2023
1e4b863
copy string using strncat
HiGarfield Jan 16, 2023
35e2f60
remove deprecated ubuntu-18.04 environment in workflow
HiGarfield Jan 16, 2023
0c04029
simplify handling exe name
HiGarfield Jan 16, 2023
d14781f
use return 0 instead of exit(0) at the end of main()
HiGarfield Jan 17, 2023
faf5fa2
use CLOCK_MONOTONIC for sleep function calls if available
HiGarfield Jan 17, 2023
7a0e57e
use clock_gettime only when available
HiGarfield Jan 17, 2023
0ca578d
Makefile: allow overriding CFLAGSand LDFLAGS on the command line
HiGarfield Jan 17, 2023
86df423
fix compiler warning in test code
HiGarfield Jan 17, 2023
94a9519
fix unit test code
HiGarfield Jan 18, 2023
1b6e598
fix a comment
HiGarfield Jan 18, 2023
b337a4e
simplify CI.yml
HiGarfield Jan 18, 2023
e3a1901
CI: run unit test after build
HiGarfield Jan 18, 2023
988c5f1
update Makefiles
HiGarfield Jan 18, 2023
e9bba7a
remove unnecessary assertion
HiGarfield Jan 18, 2023
5813c16
fix unused-result warning
HiGarfield Jan 18, 2023
c301f21
remove unnecessary memset
HiGarfield Jan 18, 2023
5618db3
simplify find_process_by_name
HiGarfield Jan 18, 2023
daf38d9
fix process name comparison on Mac OS X
HiGarfield Jan 18, 2023
06ade1e
improve increase_priority()
HiGarfield Jan 19, 2023
044113c
improve tests
HiGarfield Jan 19, 2023
70f3a6d
CI: run unit test using root
HiGarfield Jan 19, 2023
9be7bf1
more clear code style
HiGarfield Jan 19, 2023
0d195b4
change process.cputime to double for higher precision
HiGarfield Jan 19, 2023
8a5ea01
fix process name comparison
HiGarfield Jan 20, 2023
f59a645
fix tests and add more tests
HiGarfield Jan 20, 2023
3ea19f0
test for long filenames that may be truncated
HiGarfield Jan 20, 2023
2832a1d
remove program_name member from struct process_filter
HiGarfield Jan 22, 2023
1d3e5b3
set last_update to current time in init_process_group
HiGarfield Jan 22, 2023
bd6abb3
fix the judgment logic of the child process
HiGarfield Jan 22, 2023
a3e2704
improve kproc2proc
HiGarfield Jan 23, 2023
1fb4141
try to find the parent process when processes share the same name
HiGarfield Jan 23, 2023
6c671ed
refactor code to reduce redundancy
HiGarfield Jan 24, 2023
6321325
revert efficient code for FreeBSD
HiGarfield Jan 24, 2023
538db78
improve SIGINT and SIGTERM processing
HiGarfield Jan 24, 2023
39a80bc
fix cppcheck warnings
HiGarfield Jan 25, 2023
f0084e8
improve tests
HiGarfield Jan 25, 2023
6f18488
fix process_iterator
HiGarfield Jan 25, 2023
29def4a
add test_getppid_of
HiGarfield Jan 25, 2023
dbcb7a7
add conditional compilations
HiGarfield Jan 25, 2023
efe761d
add checks for malloc
HiGarfield Jan 25, 2023
71d8a38
refine read_process_info for linux
HiGarfield Jan 26, 2023
40b63ea
fix conditional compilation of get_time()
HiGarfield Jan 29, 2023
6ae1882
simplify process_iterator_apple
HiGarfield Jan 29, 2023
4c77be2
fix conditional compilation of sleep_timespec()
HiGarfield Jan 29, 2023
e036c86
use basename() implemented in libgen.h
HiGarfield Jan 31, 2023
deaed61
fix termination of cpulimit
HiGarfield Jan 31, 2023
cdec02b
fix build on non-linux platforms
HiGarfield Feb 18, 2023
0ac8c80
improve __attribute__((unused))
HiGarfield Mar 22, 2023
96a71be
fix comments
HiGarfield Mar 22, 2023
8bb3012
update CI
HiGarfield Jul 23, 2023
209a8f3
fix warning on basename()
HiGarfield Apr 6, 2024
1517fcc
improve get_time()
HiGarfield Apr 6, 2024
97cad36
improve code style
HiGarfield Apr 6, 2024
707849f
CI: update
HiGarfield Dec 23, 2023
c61a0c4
improve increase_priority()
HiGarfield Apr 6, 2024
b6e48eb
add casting for malloc()
HiGarfield Apr 6, 2024
1f8d820
refine signal handler
HiGarfield Apr 6, 2024
aad5468
optimize code
HiGarfield Apr 6, 2024
0e29303
re-organize includes and improve macros
HiGarfield Apr 6, 2024
daa699a
fix potential integer overflow
HiGarfield Apr 12, 2024
c1791f5
add type check for the directory entry if possible
HiGarfield Apr 14, 2024
4f1a780
simplify code
HiGarfield Apr 14, 2024
e985fd7
speed up is_child_of() for Linux
HiGarfield Apr 15, 2024
57b1851
more strict check on procfs
HiGarfield Apr 17, 2024
e5705dc
improve test
HiGarfield Apr 16, 2024
a61fb98
fix typos
HiGarfield Apr 18, 2024
02a2e37
avoid == for folating point numbers
HiGarfield Apr 18, 2024
6f7763c
improve getting the process start time for Linux
HiGarfield Apr 27, 2024
785803d
remove an unnecessary macro of HZ
HiGarfield Apr 27, 2024
2013ec1
use strncpy instead of memcpy when copying a string
HiGarfield Apr 29, 2024
7e69cd4
fix FreeBSD build error
HiGarfield May 3, 2024
69c9608
update Makefile
HiGarfield May 1, 2024
70e085d
get Ticks Per Second only once
HiGarfield Jul 20, 2024
d56108c
CI: update runners
HiGarfield Jul 20, 2024
af48e4a
refactor and improve the macros
HiGarfield Aug 28, 2024
16ef30c
adapt the time slot to the system load
HiGarfield Aug 29, 2024
d2c30a5
fix a memory leak
HiGarfield Aug 29, 2024
0a578f7
refactor
HiGarfield Aug 29, 2024
418c792
refactor process hash table implementation
HiGarfield Sep 1, 2024
49d0bdd
list: use safe_free macro
HiGarfield Sep 1, 2024
d4f0018
simplify code
HiGarfield Sep 1, 2024
717ac81
unify parameter names for function declaration and definition
HiGarfield Sep 2, 2024
331c2a4
rewrite comments
HiGarfield Sep 2, 2024
c586d08
remove unused code
HiGarfield Sep 2, 2024
494a5dc
improve basename macro
HiGarfield Sep 2, 2024
1013312
add .clang-format
HiGarfield Sep 6, 2024
8e23cc3
format code
HiGarfield Sep 6, 2024
a7a1234
unify comments
HiGarfield Sep 6, 2024
ede0b3a
process_dup: add a cast for the returned pointer
HiGarfield Sep 8, 2024
f814c7c
cpulimit.c: refine comments
HiGarfield Sep 8, 2024
ddef98a
get_pid_max: return -1 on unsupported platforms
HiGarfield Sep 16, 2024
6ed466d
fix command name comparison
HiGarfield Sep 28, 2024
e894057
update CI
HiGarfield Sep 29, 2024
1718200
improve is_child_of for MacOS
HiGarfield Oct 2, 2024
6196303
add multi_process_busy.c
HiGarfield Oct 3, 2024
338453c
use NULL instead of EMPTYLIST
HiGarfield Oct 3, 2024
1c0efa3
fix max path length
HiGarfield Oct 3, 2024
3d4528c
Utilize a pointer to a constant as the parameter for the process_dup …
HiGarfield Oct 5, 2024
06eaf4d
use explicit address of loop function
HiGarfield Oct 5, 2024
814aa8a
Refactor __basename to eliminate the cppcheck warning
HiGarfield Oct 5, 2024
7230574
add multi_process_busy to .gitignore
HiGarfield Oct 5, 2024
a78f656
Utilize explicit function pointers
HiGarfield Oct 5, 2024
5d93358
Resolve the warning regarding the unused return value of the atexit()…
HiGarfield Oct 5, 2024
fb24475
Move the #include "util.h" directive to the end
HiGarfield Oct 5, 2024
bce0cc2
Include the required header <sys/types.h> in util.h
HiGarfield Oct 5, 2024
70f54f8
update CI
HiGarfield Oct 5, 2024
12df0da
Refactor the read_process_info function to enhance readability
HiGarfield Oct 5, 2024
f499231
Enhance the implementation of the is_numeric function
HiGarfield Oct 5, 2024
c5e3deb
Simplify the check_proc function
HiGarfield Oct 5, 2024
aeefc68
Refine getppid_of function for Linux
HiGarfield Oct 6, 2024
e84fbc6
Refine get_start_time function for Linux
HiGarfield Oct 6, 2024
60acf9d
Simplify get_next_process for Linux
HiGarfield Oct 6, 2024
e5d9d86
Address potential NULL pointer dereference in the close_process_itera…
HiGarfield Oct 6, 2024
0598038
Refine kproc2proc function for FreeBSD
HiGarfield Oct 6, 2024
2c90059
Simplify get_single_process for FreeBSD
HiGarfield Oct 6, 2024
bc6e1e8
Fix return value of close_process_iterator
HiGarfield Oct 7, 2024
8acebed
Simplify find_process_by_name function
HiGarfield Oct 7, 2024
cc176da
Refine close_process_group function
HiGarfield Oct 7, 2024
257cf5b
Reorganize header includes
HiGarfield Oct 10, 2024
e8c8b85
Limit CPU usage within the range of 0 to 1
HiGarfield Oct 11, 2024
a65f116
Refine Makefile
HiGarfield Oct 11, 2024
89a8f1f
Use dynamically allocated memory to store the process structure
HiGarfield Oct 12, 2024
1e1a4fb
Simplify the extraction of the program name
HiGarfield Oct 12, 2024
759760b
Simplify the test_process_name function
HiGarfield Oct 12, 2024
b0e4ea6
Dynamically allocate memory for large character arrays
HiGarfield Oct 12, 2024
0c3a637
Refactor the code to address the declaration-after-statement error
HiGarfield Oct 13, 2024
5d8a0cc
Fix segment fault
HiGarfield Oct 13, 2024
8a287ff
Correct the error handling for the kvm_close function
HiGarfield Oct 14, 2024
e59c08a
minor improvements
HiGarfield Oct 14, 2024
74acd65
Fix misuse of `proc_listpids` return value
HiGarfield Oct 15, 2024
fe020e4
Improve `get_pid_max`
HiGarfield Oct 15, 2024
1ff8c2a
Refine `get_ncpu`
HiGarfield Oct 15, 2024
ad2a0a3
Standardize handling of unsupported platforms
HiGarfield Oct 15, 2024
c7ce7ea
Refine `get_process_pti`
HiGarfield Oct 15, 2024
93d35ee
Carefully check the data type of the buffer returned by proc_listpids
HiGarfield Oct 24, 2024
f492848
Simplify error handling for unspecified or incorrectly specified targ…
HiGarfield Oct 26, 2024
4abba92
Verify that pid_t is equivalent to int for MacOS
HiGarfield Oct 29, 2024
dc9e4f4
Refine `basename` calls
HiGarfield Oct 30, 2024
748e6c4
Loosen procfs check for Linux
HiGarfield Oct 30, 2024
1bfc0c6
Add support for limit values represented in decimals
HiGarfield Nov 1, 2024
71af3f2
Refine `MAX` and `MIN` calls
HiGarfield Nov 19, 2024
f1ce2dc
Refactor code to eliminate type conversion warnings
HiGarfield Nov 21, 2024
095d2b0
Improve code to ensure compatibility with the MUSL C library
HiGarfield Nov 25, 2024
d1c3b4c
Improve print output format
HiGarfield Nov 25, 2024
f879f98
Standardize the naming convention for macros
HiGarfield Nov 25, 2024
3cecf4d
Add _GNU_SOURCE macro definition to more file headers
HiGarfield Nov 26, 2024
8cf3f00
Refactor: Use `calloc` instead of `malloc` + `memset` to simplify the…
HiGarfield Nov 27, 2024
b6d540f
Remove redundant `exit` calls after `print_usage`
HiGarfield Nov 27, 2024
66d95f9
Rename `print_usage` to `print_usage_and_exit` for clarity
HiGarfield Nov 27, 2024
f37e82a
Improve Makefile
HiGarfield Nov 27, 2024
e879a9b
Define an empty filename using C++-compatible syntax
HiGarfield Nov 27, 2024
79b9aa2
Enhance argument validation for stricter input checks
HiGarfield Nov 28, 2024
228b9cc
Change exit code to 0 for the -h (help) option
HiGarfield Nov 28, 2024
d6a03fc
Improve validation for arguments of `-p` and `-l` options
HiGarfield Nov 28, 2024
c0bd4a0
Improve PID not found message to avoid confusion
HiGarfield Dec 2, 2024
9f86ba4
Use pointers to const variables wherever possible
HiGarfield Dec 2, 2024
1b32e72
Remove NDEBUG to ensure assert statements are always executed
HiGarfield Dec 2, 2024
d5c7adc
Simplify the return value for removing a process from the process table
HiGarfield Dec 4, 2024
3397946
Use sysctl to retrieve the PID list in macOS
HiGarfield Dec 4, 2024
d370101
update CI
HiGarfield Dec 5, 2024
705d651
Rename variables
HiGarfield Dec 5, 2024
6d75146
Remove unnecessary type casting
HiGarfield Dec 13, 2024
7fda7c1
Double the size of the process information buffer
HiGarfield Dec 13, 2024
fc645a3
More tests on `find_process_by_name` function
HiGarfield Dec 13, 2024
84f444a
Use `EXIT_FAILURE` and `EXIT_SUCCESS` macros for exit statuses
HiGarfield Dec 18, 2024
26d3b3b
Fix and enhance error handling
HiGarfield Dec 18, 2024
b4d508f
Refactor the preparation of `execvp` arguments for simplicity
HiGarfield Dec 18, 2024
e1fd920
Fix a casting error
HiGarfield Dec 21, 2024
94d4526
Remove unreachable code in `process_iterator_test.c`
HiGarfield Dec 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SortIncludes: false
BasedOnStyle: Microsoft
ColumnLimit: 0
IndentWidth: 4
81 changes: 81 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: CI

on:
push:
branches: [ master ]
workflow_dispatch:

permissions: {}

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build:
name: Build on ${{ matrix.os }}
permissions:
contents: read
strategy:
matrix:
os: [ubuntu-20.04, ubuntu-22.04, ubuntu-24.04, macos-13, macos-14, macos-15]
runs-on: ${{ matrix.os }}

steps:
- name: Checkout
uses: actions/checkout@main
with:
ref: master
fetch-depth: 0

- name: Compile and Test
run: |
if [[ "${{ matrix.os }}" == ubuntu-* ]]; then
export LDFLAGS="$LDFLAGS -static"
fi
make
sudo ./tests/process_iterator_test
random_file="$(mktemp $(printf 'X%.0s' $(seq 1 255)))"
cp ./tests/process_iterator_test ./tests/${random_file}
echo "./tests/${random_file}"
sudo ./tests/${random_file}

- name: Upload Artifacts
uses: actions/upload-artifact@main
with:
name: cpulimit-${{ matrix.os }}
path: src/cpulimit

build-FreeBSD:
name: Build on FreeBSD-${{ matrix.osver }}
permissions:
contents: read
strategy:
matrix:
osver: ['13.4', '14.1', '15.0']
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@main

- name: Build on FreeBSD
uses: vmactions/freebsd-vm@v1
with:
release: ${{ matrix.osver }}
usesh: true
prepare: |
pkg install -y lang/gcc gmake sudo
run: |
gmake
sudo ./tests/process_iterator_test
random_file="$(mktemp $(printf 'X%.0s' $(seq 1 255)))"
cp ./tests/process_iterator_test ./tests/${random_file}
echo "./tests/${random_file}"
sudo ./tests/${random_file}

- name: Upload Artifacts
uses: actions/upload-artifact@main
with:
name: cpulimit-FreeBSD-${{ matrix.osver }}
path: src/cpulimit
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
default: all

.DEFAULT:
cd src && $(MAKE) $@
cd tests && $(MAKE) $@
$(MAKE) -C src $@
$(MAKE) -C tests $@
3 changes: 3 additions & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.o
*~
cpulimit
59 changes: 40 additions & 19 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -1,28 +1,49 @@
CC?=gcc
CFLAGS?=-Wall -g -D_GNU_SOURCE
TARGETS=cpulimit
LIBS=list.o process_iterator.o process_group.o

UNAME := $(shell uname)

# Compiler configuration
CC ?= gcc

# Compiler flags
override CFLAGS := $(filter-out -std=% -ansi -W%, $(CFLAGS)) \
-std=c89 \
-Wall \
-Wextra \
-pedantic \
-Wold-style-definition \
-Wmissing-prototypes \
-Wstrict-prototypes \
-Werror

# Target binary
TARGET := cpulimit

# Detect operating system
UNAME ?= $(shell uname)

# Platform-specific linker flags
ifeq ($(UNAME), FreeBSD)
LIBS+=-lkvm
override LDFLAGS += -lkvm
endif

all:: $(TARGETS) $(LIBS)
ifeq ($(UNAME), Darwin)
override LDFLAGS += -lproc
endif

cpulimit: cpulimit.c $(LIBS)
$(CC) -o cpulimit cpulimit.c $(LIBS) $(CFLAGS)
# Check for librt availability
override LDFLAGS += $(shell \
echo "int main(void){ return 0; }" | \
$(CC) -x c -o /dev/null -lrt - 2>/dev/null && \
echo -lrt \
)

process_iterator.o: process_iterator.c process_iterator.h
$(CC) -c process_iterator.c $(CFLAGS)
# Phony targets
.PHONY: all clean

list.o: list.c list.h
$(CC) -c list.c $(CFLAGS)
# Default target
all: $(TARGET)

process_group.o: process_group.c process_group.h process_iterator.o list.o
$(CC) -c process_group.c $(CFLAGS)
# Build target
$(TARGET): $(wildcard *.c *.h)
$(CC) $(CFLAGS) $(filter-out process_iterator_%.c %.h, $^) $(LDFLAGS) -o $@

# Clean target
clean:
rm -f *~ *.o $(TARGETS)

rm -f *~ $(TARGET)
Loading