Skip to content

Commit 8c90f50

Browse files
committed
modified: include/tp_core.h
modified: src/main.c modified: src/tp_core.c
1 parent c4e66f4 commit 8c90f50

3 files changed

Lines changed: 109 additions & 65 deletions

File tree

include/tp_core.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <linux/prctl.h>
77
#include <unistd.h>
88
#include <signal.h>
9+
#include <errno.h>
10+
911

1012
#include <stdlib.h>
1113
#include <ctype.h>
@@ -15,10 +17,14 @@ int tp_is_number(const char *str);
1517

1618
// kill processes
1719
// TODO: add signals instead of a forcing(-9) bool
18-
PROC_T tp_kill_by_comm(const char *target_name, bool force);
20+
bool tp_kill_by_comm(const char *target_name, bool force);
21+
1922

2023
// list processes
21-
PROC_T tp_list_proc(base_t* b);
24+
bool tp_list_proc(base_t* b);
2225

2326
// '---help'
2427
void tp_show_help();
28+
29+
// kills pids from argv returns 0 on failure 1 on success
30+
bool kill_pids_from_argv(char **argv, int start, int argc, int sig);

src/main.c

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "tp_core.h"
22
#include "tp_logger.h"
33

4-
int main(int argc, char **argv) {
4+
int main(int argc, char *argv[]) {
55
base_t b;
66

77
if (argc == 1) {
@@ -11,54 +11,69 @@ int main(int argc, char **argv) {
1111

1212
for (int i = 1; i < argc; i++) {
1313
if (!strcmp(argv[i], "--list") || !strcmp(argv[i], "-l")) {
14-
if (tp_list_proc(&b) == FAILED) return 1;
14+
if (!tp_list_proc(&b)) return 1;
1515
}
16+
1617
else if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
1718
tp_show_help();
19+
return 0;
1820
}
21+
1922
else if (!strcmp(argv[i], "--kill-by-program-name") || !strcmp(argv[i], "-kn")) {
20-
bool force = false;
21-
22-
if (i + 2 < argc && (!strcmp(argv[i + 2], "--sig-kill") || !strcmp(argv[i + 2], "-sg"))) {
23-
force = true;
23+
if (i + 1 >= argc) {
24+
TP_ERROR("No program name specified after %s\n", argv[i]);
25+
return 1;
2426
}
2527

26-
if (i + 1 < argc) {
27-
tp_kill_by_comm(argv[i + 1], force);
28-
i += force ? 2 : 1;
29-
} else {
30-
TP_ERROR("Error: --kill-by-program-name requires a program name\n");
28+
int sig = SIGTERM;
29+
i++;
30+
31+
if (argv[i][0] == '-' && isdigit(argv[i][1])) {
32+
sig = atoi(argv[i] + 1);
33+
i++;
34+
}
35+
36+
if (i >= argc) {
37+
TP_ERROR("No program name specified after signal\n");
3138
return 1;
3239
}
40+
41+
const char *program_name = argv[i];
42+
43+
// still not implemented
44+
return 1;
3345
}
34-
else if (!strcmp(argv[i], "--kill-by-pid") || !strcmp(argv[i], "-kp")) {
35-
bool force = false;
36-
37-
if (i + 2 < argc && (!strcmp(argv[i + 2], "--sig-kill") || !strcmp(argv[i + 2], "-sg"))) {
38-
force = true;
46+
47+
else if (!strcmp(argv[i], "--kill-by-pid") || !strcmp(argv[i], "-kp")) {
48+
if (i + 1 >= argc) {
49+
TP_ERROR("No PID specified after %s\n", argv[i]);
50+
return 1;
3951
}
4052

41-
if (i + 1 < argc) {
42-
pid_t pid = atoi(argv[i + 1]);
43-
int signal = force ? SIGKILL : SIGTERM;
53+
int sig = SIGTERM;
54+
i++;
4455

45-
if (kill(pid, signal) == 0) {
46-
printf("Killed PID %d with signal %s\n", pid, force ? "SIGKILL" : "SIGTERM");
47-
} else {
48-
TP_ERROR("Failed to kill process (PID: %d)\n", pid);
56+
57+
if (argv[i][0] == '-' && isdigit(argv[i][1])) {
58+
sig = atoi(argv[i] + 1);
59+
i++;
60+
if (i >= argc) {
61+
TP_ERROR("No PID specified after signal %d\n", sig);
62+
return 1;
4963
}
64+
}
5065

51-
i += force ? 2 : 1;
52-
} else {
53-
TP_ERROR("Error: --kill-by-pid requires a PID\n");
66+
67+
if (!kill_pids_from_argv(argv, i, argc, sig)) {
5468
return 1;
5569
}
56-
}
70+
}
71+
5772
else {
5873
TP_ERROR("Unknown option: %s\n", argv[i]);
5974
return 1;
6075
}
6176
}
6277

6378
return 0;
64-
}
79+
}

src/tp_core.c

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,73 +11,60 @@ int tp_is_number(const char *str) {
1111

1212

1313

14-
15-
16-
PROC_T tp_kill_by_comm(const char *target_name, bool force) {
14+
// kills a process by name returns 0 for failure 1 for success
15+
bool tp_kill_by_comm(const char *target_name, bool force) {
1716
DIR *proc = opendir("/proc");
1817
if (!proc) {
1918
TP_ERROR("opendir /proc");
20-
return FAILED;
19+
return false;
2120
}
2221

2322
struct dirent *entry;
23+
int sig = force ? SIGKILL : SIGTERM;
24+
2425
while ((entry = readdir(proc)) != NULL) {
2526
if (!tp_is_number(entry->d_name)) continue;
2627

27-
char comm_path[MAX_PATH];
28+
char comm_path[PATH_MAX];
2829
snprintf(comm_path, sizeof(comm_path), "/proc/%s/comm", entry->d_name);
2930

3031
FILE *f = fopen(comm_path, "r");
3132
if (!f) continue;
3233

33-
char comm[MAX_PATH];
34+
char comm[256];
3435
if (fgets(comm, sizeof(comm), f)) {
35-
36-
comm[strcspn(comm, "\n")] = 0;
36+
comm[strcspn(comm, "\n")] = 0; // remove newline
3737

3838
if (strcmp(comm, target_name) == 0) {
3939
pid_t pid = atoi(entry->d_name);
4040

41-
if (!force) {
42-
if (kill(pid, SIGTERM) == 0) {
43-
printf("Killed %s (PID: %d)\n", comm, pid);
44-
}
45-
46-
else {
47-
printf("(%s) There is no such a process\n", comm);
48-
}
49-
}
50-
51-
else {
52-
if (kill(pid, SIGTERM) == 0) {
53-
printf("Killed %s (PID: %d)\n", comm, pid);
54-
}
55-
56-
else {
57-
printf("(%s) There is no such a process\n", comm);
58-
}
41+
if (kill(pid, sig) == 0) {
42+
printf("Killed %s (PID: %d) with signal %d\n", comm, pid, sig);
43+
} else {
44+
printf("Failed to kill %s (PID: %d): %s\n", comm, pid, strerror(errno));
5945
}
6046
}
47+
} else {
48+
TP_ERROR("Failed to read comm for PID %s\n", entry->d_name);
6149
}
62-
63-
else {
64-
printf("Couldn't" RED "kill %s\n", target_name);
65-
}
50+
6651
fclose(f);
6752
}
6853

6954
closedir(proc);
55+
return true;
7056
}
7157

7258

7359

74-
PROC_T tp_list_proc(base_t* b) {
60+
// list processes returns 0 for failure 1 for success
61+
bool tp_list_proc(base_t* b) {
7562
b->cmdline_file = NULL;
7663
b->proc_dir = opendir("/proc");
7764

7865
if (!b->proc_dir) {
7966
TP_ERROR("Couldn't open /proc");
80-
return FAILED;
67+
return false;
8168
}
8269

8370
struct dirent *entry;
@@ -118,9 +105,10 @@ PROC_T tp_list_proc(base_t* b) {
118105

119106
closedir(b->proc_dir);
120107

121-
return SUCCESS;
108+
return true;
122109
}
123110

111+
// prints help
124112
void tp_show_help() {
125113
printf("© 2025 TinyProcess.\n\nUsage: process [FLAGS] [ARGS]\n\nFLAGS:\n"
126114
"\t--help | -h | --h shows this message\n"
@@ -129,6 +117,41 @@ void tp_show_help() {
129117
"\t--kill-by-pid | -kp kills a process by pid.\n"
130118
"\t--sig-kill | -sg forces a process to close(SIGKILL)\n"
131119
"\t(ARGS are user arguments).\n\te.g: -kp [pid] (pid is an argument)\n"
132-
);
120+
);
133121

134-
}
122+
}
123+
124+
// kills pids from argv returns 0 on failure 1 on success
125+
bool kill_pids_from_argv(char **argv, int start, int argc, int sig) {
126+
int valid_pid = 0;
127+
128+
for (int i = start; i < argc; i++) {
129+
char *endptr = NULL;
130+
pid_t pid = (pid_t) strtol(argv[i], &endptr, 10);
131+
132+
if (endptr == argv[i] || *endptr != '\0') {
133+
TP_ERROR("Invalid PID: %s\n", argv[i]);
134+
continue;
135+
}
136+
137+
valid_pid++;
138+
139+
// both if and else are called
140+
int ret = kill(pid, sig);
141+
printf("kill returned: %d\n", ret);
142+
if (ret == 0) {
143+
TP_INFO("Killed PID %d with signal %d\n", pid, sig);
144+
} else {
145+
TP_ERROR("Failed to send signal %d to PID %d", sig, pid);
146+
}
147+
148+
}
149+
150+
if (valid_pid == 0) {
151+
TP_ERROR("No valid PIDs provided.\n");
152+
return false;
153+
}
154+
155+
return true;
156+
}
157+

0 commit comments

Comments
 (0)