diff --git a/cap.c b/cap.c index 9d83bbc..1e09431 100644 --- a/cap.c +++ b/cap.c @@ -344,10 +344,11 @@ int main(int argc, char **argv) snprintf(RULE_NAME, BUFFER, "root%d", RULE_NAME_NUMBER); if (create_ipset(RULE_NAME) != 0) { - fprintf(stderr, "创建 IPSet %s 失败\n", RULE_NAME); + _printf("创建 IPSet %s 失败\n", RULE_NAME); } - while (1) { + while (1) + { _printf("子进程当前 Ipset Rule 名 %s\n", RULE_NAME); count = get_ip_count_in_ipset(RULE_NAME); diff --git a/cap.o b/cap.o index 10ea2f4..01d3e93 100644 Binary files a/cap.o and b/cap.o differ diff --git a/common.c b/common.c index b336329..043a308 100644 --- a/common.c +++ b/common.c @@ -1,21 +1,15 @@ #include "common.h" // 计算字符串长度 -int _strlen(char *str) -{ - char *_p = NULL; - +int _strlen(const char *str) { if (str == NULL) return 0; - _p = strchr(str, '\0'); - - if (_p == NULL) - return 0; - + const char *_p = strchr(str, '\0'); return _p - str; } + // 自定义 printf 函数 void _printf(const char *format, ...) { @@ -86,7 +80,7 @@ int whitelist(char *client_ip, char (*whitelist_ip)[WHITELIST_IP_NUM]) break; } - // 对比client_ip的前缀是否与白名单中的IP段匹配 + // 对比 client_ip 的前缀是否与白名单中的IP段匹配 if (strncmp(client_ip, whitelist_ip[i], strlen(whitelist_ip[i])) == 0) { return 1; // 匹配成功 } @@ -102,12 +96,12 @@ int isregion(char *str, char (*region_list)[WHITELIST_IP_NUM]) char *p; for (i = 0; i < WHITELIST_IP_NUM; i++) { - // 如果region_list[i]为空字符串,跳出循环 + // 如果 region_list[i] 为空字符串,跳出循环 if (region_list[i][0] == '\0') { break; } - // 在str中查找region_list[i] + // 在str中查找 region_list[i] p = strstr(str, region_list[i]); if (p != NULL) { return 1; // 匹配成功,返回1 @@ -128,19 +122,23 @@ int8_t _copy_new_mem(char *src, int src_len, char **dest) return 0; } -char *_time() -{ - char temp[BUFFER]; - char *wday[] = { "0", "1", "2", "3", "4", "5", "6" }; - time_t t; - struct tm *p; - time(&t); - p = localtime(&t); // 取得当地时间 +// 返回的时间字符串存储在静态缓冲区中 +char *_time() { + static char temp[BUFFER]; + const char *wday[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; + time_t t = time(NULL); + struct tm *p = localtime(&t); - memset(temp, 0, BUFFER); - snprintf(temp, BUFFER, "[%d/%02d/%02d %s %02d:%02d:%02d] ", (1900 + p->tm_year), (1 + p->tm_mon), p->tm_mday, wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec); + if (!p) { + perror("localtime failed"); + return NULL; + } - return strdup(temp); + snprintf(temp, sizeof(temp), "[%d/%02d/%02d %s %02d:%02d:%02d]", + 1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday, + wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec); + + return temp; // 返回静态缓冲区地址 } int is_valid_ip(const char *ip) @@ -152,22 +150,73 @@ int is_valid_ip(const char *ip) return result != 0; } -int _nice(int increment) -{ +int _nice(int increment) { + // 获取当前优先级 int oldprio = getpriority(PRIO_PROCESS, getpid()); - printf("%d\n", oldprio); + if (oldprio == -1 && errno != 0) { + perror("getpriority failed"); + return -1; + } - return setpriority(PRIO_PROCESS, getpid(), oldprio + increment); + printf("Current priority: %d\n", oldprio); + + // 检查是否溢出 + if ((increment > 0 && oldprio > INT_MAX - increment) || + (increment < 0 && oldprio < INT_MIN - increment)) { + fprintf(stderr, "Priority overflow error\n"); + return -1; + } + + // 计算新的优先级 + int newprio = oldprio + increment; + + // 检查新的优先级是否在有效范围内 + if (newprio < PRIO_MIN || newprio > PRIO_MAX) { + fprintf(stderr, "New priority out of range: %d (valid range is %d to %d)\n", newprio, PRIO_MIN, PRIO_MAX); + return -1; + } + + // 设置新的优先级 + if (setpriority(PRIO_PROCESS, getpid(), newprio) == -1) { + perror("setpriority failed"); + return -1; + } + + printf("New priority: %d\n", newprio); + return 0; } // 判断命令是否存在 -int _command_exists(const char *command) -{ - char buffer[BUFFER]; - snprintf(buffer, sizeof(buffer), "%s > /dev/null 2>&1", command); - int status = system(buffer); +int command_exists(const char *command) { + const char *path_env = getenv("PATH"); + if (!path_env) { + return 0; // 如果 PATH 不存在,返回不存在 + } - return (status == 0); + char filepath[1024]; // 缓冲区大小 + const char *dir = path_env; + + while (dir && *dir) { + // 查找 PATH 中的下一个目录 + const char *end = strchr(dir, ':'); + size_t len = end ? (size_t)(end - dir) : strlen(dir); + + // 构建路径并检查长度 + if (snprintf(filepath, sizeof(filepath), "%.*s/%s", (int)len, dir, command) >= (int)sizeof(filepath)) { + return 0; // 缓冲区溢出,返回不存在 + } + + // 检查文件是否存在且可执行 + if (access(filepath, X_OK) == 0) { + puts(filepath); + return 1; // 命令存在 + } + + // 更新 dir 指针 + dir = end ? end + 1 : NULL; + } + + return 0; // 命令不存在 } // 定义一个函数,执行命令并返回输出 diff --git a/common.h b/common.h index d77bfbe..338e5d2 100644 --- a/common.h +++ b/common.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -28,7 +29,7 @@ extern char *_time(); -extern int _strlen(char *str); +extern int _strlen(const char *str); extern void _printf(const char *format, ...); extern int _nice(int increment); diff --git a/common.o b/common.o index 1ae8024..fe67c2c 100644 Binary files a/common.o and b/common.o differ diff --git a/denyip b/denyip index 072267f..5a97a67 100644 Binary files a/denyip and b/denyip differ