From 5ecb1adc344a106b9f1167930798bbcb3e286b14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Mon, 28 Aug 2023 11:53:48 +0200 Subject: [PATCH] core: add command `/sys` to show resource limits and usage --- ChangeLog.adoc | 1 + doc/en/weechat_dev.en.adoc | 2 + doc/fr/weechat_dev.fr.adoc | 2 + doc/ja/weechat_dev.ja.adoc | 4 + doc/sr/weechat_dev.sr.adoc | 4 + po/srcfiles.cmake | 2 + src/core/CMakeLists.txt | 1 + src/core/wee-command.c | 40 ++++ src/core/wee-config.c | 6 +- src/core/wee-sys.c | 345 +++++++++++++++++++++++++++++ src/core/wee-sys.h | 35 +++ src/core/wee-util.c | 170 -------------- src/core/wee-util.h | 11 - tests/CMakeLists.txt | 1 + tests/tests.cpp | 1 + tests/unit/core/test-core-sys.cpp | 78 +++++++ tests/unit/core/test-core-util.cpp | 20 -- 17 files changed, 519 insertions(+), 204 deletions(-) create mode 100644 src/core/wee-sys.c create mode 100644 src/core/wee-sys.h create mode 100644 tests/unit/core/test-core-sys.cpp diff --git a/ChangeLog.adoc b/ChangeLog.adoc index 9d4a9b1d7..f008f4cfc 100644 --- a/ChangeLog.adoc +++ b/ChangeLog.adoc @@ -15,6 +15,7 @@ For a list of important changes that require manual actions, please look at rele New features:: + * core: add command `/sys` to show resource limits and usage * core: ignore key bindings with empty command * core: add support of quotes in commands `/key bind` and `/key bindctxt` * core: evaluate command given to `/repeat` with contextual variables (issue #2007) diff --git a/doc/en/weechat_dev.en.adoc b/doc/en/weechat_dev.en.adoc index b84d891ee..569a4902d 100644 --- a/doc/en/weechat_dev.en.adoc +++ b/doc/en/weechat_dev.en.adoc @@ -146,6 +146,7 @@ WeeChat "core" is located in following directories: |    wee-utf8.c | UTF-8 functions. |    wee-util.c | Some other functions. |    wee-version.c | Functions for WeeChat version. +|    wee-sys.c | System functions. |    weechat.c | Main functions: command line options, startup. |    hook/ | Hook functions. |       wee-hook-command-run.c | Hook "command_run". @@ -420,6 +421,7 @@ WeeChat "core" is located in following directories: |          test-core-url.cpp | Tests: URLs. |          test-core-utf8.cpp | Tests: UTF-8. |          test-core-util.cpp | Tests: utility functions. +|          test-core-sys.cpp | Tests: system functions. |       gui/ | Root of unit tests for interfaces. |          test-gui-bar-window.cpp | Tests: bar window functions. |          test-gui-buffer.cpp | Tests: buffer functions. diff --git a/doc/fr/weechat_dev.fr.adoc b/doc/fr/weechat_dev.fr.adoc index d82a0ead9..14006001d 100644 --- a/doc/fr/weechat_dev.fr.adoc +++ b/doc/fr/weechat_dev.fr.adoc @@ -148,6 +148,7 @@ Le cœur de WeeChat est situé dans les répertoires suivants : |    wee-utf8.c | Fonctions UTF-8. |    wee-util.c | Quelques autres fonctions utilitaires. |    wee-version.c | Fonctions pour la version de WeeChat. +|    wee-sys.c | Fonctions système. |    weechat.c | Fonctions principales : options de ligne de commande, démarrage. |    hook/ | Hook functions. |       wee-hook-command-run.c | Hook "command_run". @@ -422,6 +423,7 @@ Le cœur de WeeChat est situé dans les répertoires suivants : |          test-core-url.cpp | Tests : URLs. |          test-core-utf8.cpp | Tests : UTF-8. |          test-core-util.cpp | Tests : fonctions utiles. +|          test-core-sys.cpp | Tests : fonctions système. |       gui/ | Racine des tests unitaires pour les interfaces. |          test-gui-bar-window.cpp | Tests : fonctions de fenêtres de barre. |          test-gui-buffer.cpp | Tests : fonctions de tampons. diff --git a/doc/ja/weechat_dev.ja.adoc b/doc/ja/weechat_dev.ja.adoc index 6d47cde10..060cecefc 100644 --- a/doc/ja/weechat_dev.ja.adoc +++ b/doc/ja/weechat_dev.ja.adoc @@ -158,6 +158,8 @@ WeeChat "core" は以下のディレクトリに配置されています: |    wee-utf8.c | UTF-8 関数 |    wee-util.c | その他の関数 |    wee-version.c | WeeChat バージョンについての関数 +// TRANSLATION MISSING +|    wee-sys.c | System functions. |    weechat.c | 主要関数: コマンドラインオプション、起動 |    hook/ | フック関数 |       wee-hook-command-run.c | "command_run" フック @@ -452,6 +454,8 @@ WeeChat "core" は以下のディレクトリに配置されています: |          test-core-url.cpp | テスト: URL |          test-core-utf8.cpp | テスト: UTF-8 |          test-core-util.cpp | テスト: ユーティリティ関数 +// TRANSLATION MISSING +|          test-core-sys.cpp | Tests: system functions. |       gui/ | インターフェースの単体テストを収める最上位ディレクトリ // TRANSLATION MISSING |          test-gui-bar-window.cpp | Tests: bar window functions. diff --git a/doc/sr/weechat_dev.sr.adoc b/doc/sr/weechat_dev.sr.adoc index ad332339d..77aa124df 100644 --- a/doc/sr/weechat_dev.sr.adoc +++ b/doc/sr/weechat_dev.sr.adoc @@ -148,6 +148,8 @@ WeeChat „језгро” се налази у следећим директо |    wee-utf8.c | UTF-8 фунцкије. |    wee-util.c | Неке друге функције. |    wee-version.c | Функције за WeeChat верзију. +// TRANSLATION MISSING +|    wee-sys.c | System functions. |    weechat.c | Основне функције: опције командне линије, покретање. |    hook/ | Функције кука. |       wee-hook-command-run.c | Кука "command_run". @@ -422,6 +424,8 @@ WeeChat „језгро” се налази у следећим директо |          test-core-url.cpp | Тестови: URL адресе. |          test-core-utf8.cpp | Тестови: UTF-8. |          test-core-util.cpp | Тестови: помоћне функције. +// TRANSLATION MISSING +|          test-core-sys.cpp | Tests: system functions. |       gui/ | Корен unit тестова интерфејса. |          test-gui-bar-window.cpp | Тестови: функције прозора траке. |          test-gui-buffer.cpp | Тестови: бафер функције. diff --git a/po/srcfiles.cmake b/po/srcfiles.cmake index 044ebc30e..17a616fe4 100644 --- a/po/srcfiles.cmake +++ b/po/srcfiles.cmake @@ -101,6 +101,8 @@ SET(WEECHAT_SOURCES ./src/core/wee-util.h ./src/core/wee-version.c ./src/core/wee-version.h +./src/core/wee-sys.c +./src/core/wee-sys.h ./src/gui/curses/gui-curses-bar-window.c ./src/gui/curses/gui-curses-bar-window.h ./src/gui/curses/gui-curses-chat.c diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 8ee698983..dd46ed673 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -53,6 +53,7 @@ set(LIB_CORE_SRC wee-utf8.c wee-utf8.h wee-util.c wee-util.h wee-version.c wee-version.h + wee-sys.c wee-sys.h hook/wee-hook-command-run.c hook/wee-hook-command-run.h hook/wee-hook-command.c hook/wee-hook-command.h hook/wee-hook-completion.c hook/wee-hook-completion.h diff --git a/src/core/wee-command.c b/src/core/wee-command.c index 113debad7..58e767b2b 100644 --- a/src/core/wee-command.c +++ b/src/core/wee-command.c @@ -64,6 +64,7 @@ #include "wee-utf8.h" #include "wee-util.h" #include "wee-version.h" +#include "wee-sys.h" #include "../gui/gui-bar.h" #include "../gui/gui-bar-item.h" #include "../gui/gui-bar-item-custom.h" @@ -7774,6 +7775,35 @@ COMMAND_CALLBACK(window) COMMAND_ERROR; } +/* + * Callback for command "/sys": system actions. + */ + +COMMAND_CALLBACK(sys) +{ + /* make C compiler happy */ + (void) pointer; + (void) data; + (void) buffer; + + COMMAND_MIN_ARGS(2, ""); + + if (string_strcmp (argv[1], "get") == 0) + { + COMMAND_MIN_ARGS(2, "get"); + + if (string_strcmp (argv[2], "rlimit") == 0) + sys_display_rlimit (); + else if (string_strcmp (argv[2], "rusage") == 0) + sys_display_rusage (); + else + COMMAND_ERROR; + return WEECHAT_RC_OK; + } + + COMMAND_ERROR; +} + /* * Hooks WeeChat core commands. */ @@ -9456,6 +9486,16 @@ command_init () " || bare" " || %(windows_numbers)", &command_window, NULL, NULL); + hook_command ( + NULL, "sys", + N_("system actions"), + N_("get rlimit|rusage"), + N_(" get: display system info\n" + "rlimit: display resource limits " + "(see /help weechat.startup.sys_rlimit and man getrlimit)\n" + "rusage: display resource usage (see man getrusage)"), + "get rlimit|rusage", + &command_sys, NULL, NULL); } /* diff --git a/src/core/wee-config.c b/src/core/wee-config.c index 980b7891b..89bde3d70 100644 --- a/src/core/wee-config.c +++ b/src/core/wee-config.c @@ -44,11 +44,11 @@ #include "wee-log.h" #include "wee-network.h" #include "wee-utf8.h" -#include "wee-util.h" #include "wee-list.h" #include "wee-proxy.h" #include "wee-string.h" #include "wee-version.h" +#include "wee-sys.h" #include "../gui/gui-bar.h" #include "../gui/gui-bar-item.h" #include "../gui/gui-bar-item-custom.h" @@ -382,7 +382,7 @@ config_change_sys_rlimit (const void *pointer, void *data, (void) option; if (gui_init_ok) - util_setrlimit (); + sys_setrlimit (); } /* @@ -1482,7 +1482,7 @@ config_weechat_init_after_read () { int context; - util_setrlimit (); + sys_setrlimit (); gui_buffer_notify_set_all (); diff --git a/src/core/wee-sys.c b/src/core/wee-sys.c new file mode 100644 index 000000000..c078e43c1 --- /dev/null +++ b/src/core/wee-sys.c @@ -0,0 +1,345 @@ +/* + * wee-sys.c - system actions + * + * Copyright (C) 2023 Sébastien Helleu + * + * This file is part of WeeChat, the extensible chat client. + * + * WeeChat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * WeeChat is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with WeeChat. If not, see . + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include +#include +#include +#ifdef HAVE_SYS_RESOURCE_H +#include +#endif + +#include "weechat.h" +#include "wee-config.h" +#include "wee-log.h" +#include "wee-string.h" +#include "wee-sys.h" +#include "wee-util.h" +#include "../gui/gui-chat.h" +#include "../gui/gui-window.h" +#include "../plugins/plugin.h" + + +#ifdef HAVE_SYS_RESOURCE_H +struct t_rlimit_resource rlimit_resource[] = +{ +#ifdef RLIMIT_AS + { "as", RLIMIT_AS }, +#endif +#ifdef RLIMIT_CORE + { "core", RLIMIT_CORE }, +#endif +#ifdef RLIMIT_CPU + { "cpu", RLIMIT_CPU }, +#endif +#ifdef RLIMIT_DATA + { "data", RLIMIT_DATA }, +#endif +#ifdef RLIMIT_FSIZE + { "fsize", RLIMIT_FSIZE }, +#endif +#ifdef RLIMIT_LOCKS + { "locks", RLIMIT_LOCKS }, +#endif +#ifdef RLIMIT_MEMLOCK + { "memlock", RLIMIT_MEMLOCK }, +#endif +#ifdef RLIMIT_MSGQUEUE + { "msgqueue", RLIMIT_MSGQUEUE }, +#endif +#ifdef RLIMIT_NICE + { "nice", RLIMIT_NICE }, +#endif +#ifdef RLIMIT_NOFILE + { "nofile", RLIMIT_NOFILE }, +#endif +#ifdef RLIMIT_NPROC + { "nproc", RLIMIT_NPROC }, +#endif +#ifdef RLIMIT_RSS + { "rss", RLIMIT_RSS }, +#endif +#ifdef RLIMIT_RTPRIO + { "rtprio", RLIMIT_RTPRIO }, +#endif +#ifdef RLIMIT_RTTIME + { "rttime", RLIMIT_RTTIME }, +#endif +#ifdef RLIMIT_SIGPENDING + { "sigpending", RLIMIT_SIGPENDING }, +#endif +#ifdef RLIMIT_STACK + { "stack", RLIMIT_STACK }, +#endif + { NULL, 0 }, +}; +#endif /* HAVE_SYS_RESOURCE_H */ + + +/* + * Sets resource limit. + */ + +#ifdef HAVE_SYS_RESOURCE_H +void +sys_setrlimit_resource (const char *resource_name, long long limit) +{ + int i; + struct rlimit rlim; + char str_limit[64]; + + if (!resource_name) + return; + + if (limit == -1) + snprintf (str_limit, sizeof (str_limit), "unlimited"); + else + snprintf (str_limit, sizeof (str_limit), "%lld", limit); + + for (i = 0; rlimit_resource[i].name; i++) + { + if (strcmp (rlimit_resource[i].name, resource_name) == 0) + { + if (limit < -1) + { + gui_chat_printf (NULL, + _("%sInvalid limit for resource \"%s\": %s " + "(must be >= -1)"), + gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], + resource_name, str_limit); + return; + } + rlim.rlim_cur = (limit >= 0) ? (rlim_t)limit : RLIM_INFINITY; + rlim.rlim_max = rlim.rlim_cur; + if (setrlimit (rlimit_resource[i].resource, &rlim) == 0) + { + log_printf (_("Limit for resource \"%s\" has been set to %s"), + resource_name, str_limit); + if (gui_init_ok) + { + gui_chat_printf (NULL, + _("Limit for resource \"%s\" has been set " + "to %s"), + resource_name, str_limit); + } + } + else + { + gui_chat_printf (NULL, + _("%sUnable to set resource limit \"%s\" to " + "%s: error %d %s"), + gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], + resource_name, + str_limit, + errno, + strerror (errno)); + } + return; + } + } + + gui_chat_printf (NULL, + _("%sUnknown resource limit \"%s\" (see /help " + "weechat.startup.sys_rlimit)"), + gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], + resource_name); +} +#endif /* HAVE_SYS_RESOURCE_H */ + +/* + * Sets resource limits using value of option "weechat.startup.sys_rlimit". + */ + +void +sys_setrlimit () +{ +#ifdef HAVE_SYS_RESOURCE_H + char **items, *pos, *error; + int num_items, i; + long long number; + + items = string_split (CONFIG_STRING(config_startup_sys_rlimit), ",", NULL, + WEECHAT_STRING_SPLIT_STRIP_LEFT + | WEECHAT_STRING_SPLIT_STRIP_RIGHT + | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS, + 0, &num_items); + if (items) + { + for (i = 0; i < num_items; i++) + { + pos = strchr (items[i], ':'); + if (pos) + { + pos[0] = '\0'; + error = NULL; + number = strtoll (pos + 1, &error, 10); + if (error && !error[0]) + { + sys_setrlimit_resource (items[i], number); + } + else + { + gui_chat_printf (NULL, + _("%sInvalid limit for resource \"%s\": " + "%s (must be >= -1)"), + gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], + items[i], pos + 1); + } + } + } + string_free_split (items); + } +#endif /* HAVE_SYS_RESOURCE_H */ +} + +/* + * Displays resource limits. + */ + +void +sys_display_rlimit () +{ +#ifdef HAVE_SYS_RESOURCE_H + struct rlimit rlim; + char str_cur[128], str_max[128]; + int i; + + gui_chat_printf (NULL, ""); + gui_chat_printf (NULL, _("Resource limits (see \"man getrlimit\" for help):")); + for (i = 0; rlimit_resource[i].name; i++) + { + if (getrlimit (rlimit_resource[i].resource, &rlim) == 0) + { + if (rlim.rlim_cur == RLIM_INFINITY) + { + snprintf (str_cur, sizeof (str_cur), "unlimited"); + } + else + { + snprintf (str_cur, sizeof (str_cur), + "%lld", (long long)rlim.rlim_cur); + } + if (rlim.rlim_max == RLIM_INFINITY) + { + snprintf (str_max, sizeof (str_max), "unlimited"); + } + else + { + snprintf (str_max, sizeof (str_max), + "%lld", (long long)rlim.rlim_max); + } + gui_chat_printf (NULL, + " %-10s: %s (max: %s)", + rlimit_resource[i].name, str_cur, str_max); + } + else + { + gui_chat_printf (NULL, + _("%sUnable to get resource limit \"%s\": " + "error %d %s"), + gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], + rlimit_resource[i].name, + errno, + strerror (errno)); + } + } +#else /* HAVE_SYS_RESOURCE_H */ + gui_chat_printf (NULL, + _("System function \"%s\" is not available " + "on this system"), + "getrlimit"); +#endif /* HAVE_SYS_RESOURCE_H */ +} + +/* + * Displays resource usage. + */ + +void +sys_display_rusage () +{ +#ifdef HAVE_SYS_RESOURCE_H + struct rusage usage; + char *str_time; + long long microseconds; + + gui_chat_printf (NULL, ""); + gui_chat_printf (NULL, _("Resource usage (see \"man getrusage\" for help):")); + getrusage (RUSAGE_SELF, &usage); + /* ru_utime: user CPU time used */ + microseconds = ((long long)usage.ru_utime.tv_sec * 1000000) + + (long long)usage.ru_utime.tv_usec; + str_time = util_get_microseconds_string (microseconds); + if (str_time) + { + gui_chat_printf (NULL, " ru_utime : %s", str_time); + free (str_time); + } + /* ru_stime: system CPU time used */ + microseconds = ((long long)usage.ru_stime.tv_sec * 1000000) + + (long long)usage.ru_stime.tv_usec; + str_time = util_get_microseconds_string (microseconds); + if (str_time) + { + gui_chat_printf (NULL, " ru_stime : %s", str_time); + free (str_time); + } + /* ru_maxrss: maximum resident set size */ + gui_chat_printf (NULL, " ru_maxrss : %ld", usage.ru_maxrss); + /* ru_ixrss: integral shared memory size */ + gui_chat_printf (NULL, " ru_ixrss : %ld", usage.ru_ixrss); + /* ru_idrss: integral unshared data size */ + gui_chat_printf (NULL, " ru_idrss : %ld", usage.ru_idrss); + /* ru_isrss: integral unshared stack size */ + gui_chat_printf (NULL, " ru_isrss : %ld", usage.ru_isrss); + /* ru_minflt: page reclaims (soft page faults) */ + gui_chat_printf (NULL, " ru_minflt : %ld", usage.ru_minflt); + /* ru_majflt: page faults (hard page faults) */ + gui_chat_printf (NULL, " ru_majflt : %ld", usage.ru_majflt); + /* ru_nswap: swaps */ + gui_chat_printf (NULL, " ru_nswap : %ld", usage.ru_nswap); + /* ru_inblock: block input operations */ + gui_chat_printf (NULL, " ru_inblock : %ld", usage.ru_inblock); + /* ru_oublock: block output operations */ + gui_chat_printf (NULL, " ru_oublock : %ld", usage.ru_oublock); + /* ru_msgsnd: IPC messages sent */ + gui_chat_printf (NULL, " ru_msgsnd : %ld", usage.ru_msgsnd); + /* ru_msgrcv: IPC messages received */ + gui_chat_printf (NULL, " ru_msgrcv : %ld", usage.ru_msgrcv); + /* ru_nsignals: signals received */ + gui_chat_printf (NULL, " ru_nsignals: %ld", usage.ru_nsignals); + /* ru_nvcsw: voluntary context switches */ + gui_chat_printf (NULL, " ru_nvcsw : %ld", usage.ru_nvcsw); + /* ru_nivcsw: involuntary context switches */ + gui_chat_printf (NULL, " ru_nivcsw : %ld", usage.ru_nivcsw); +#else /* HAVE_SYS_RESOURCE_H */ + gui_chat_printf (NULL, + _("System function \"%s\" is not available " + "on this system"), + "getrusage"); +#endif /* HAVE_SYS_RESOURCE_H */ +} diff --git a/src/core/wee-sys.h b/src/core/wee-sys.h new file mode 100644 index 000000000..d78f6fdb9 --- /dev/null +++ b/src/core/wee-sys.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2023 Sébastien Helleu + * + * This file is part of WeeChat, the extensible chat client. + * + * WeeChat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * WeeChat is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with WeeChat. If not, see . + */ + +#ifndef WEECHAT_SYS_H +#define WEECHAT_SYS_H + +#ifdef HAVE_SYS_RESOURCE_H +struct t_rlimit_resource +{ + char *name; /* name of resource */ + int resource; /* value of resource */ +}; +#endif /* HAVE_SYS_RESOURCE_H */ + +extern void sys_setrlimit (); +extern void sys_display_rlimit (); +extern void sys_display_rusage (); + +#endif /* WEECHAT_SYS_H */ diff --git a/src/core/wee-util.c b/src/core/wee-util.c index e39aa01bd..783d4ccf9 100644 --- a/src/core/wee-util.c +++ b/src/core/wee-util.c @@ -46,176 +46,6 @@ #include "../plugins/plugin.h" -#ifdef HAVE_SYS_RESOURCE_H -struct t_rlimit_resource rlimit_resource[] = -{ -#ifdef RLIMIT_AS - { "as", RLIMIT_AS }, -#endif -#ifdef RLIMIT_CORE - { "core", RLIMIT_CORE }, -#endif -#ifdef RLIMIT_CPU - { "cpu", RLIMIT_CPU }, -#endif -#ifdef RLIMIT_DATA - { "data", RLIMIT_DATA }, -#endif -#ifdef RLIMIT_FSIZE - { "fsize", RLIMIT_FSIZE }, -#endif -#ifdef RLIMIT_LOCKS - { "locks", RLIMIT_LOCKS }, -#endif -#ifdef RLIMIT_MEMLOCK - { "memlock", RLIMIT_MEMLOCK }, -#endif -#ifdef RLIMIT_MSGQUEUE - { "msgqueue", RLIMIT_MSGQUEUE }, -#endif -#ifdef RLIMIT_NICE - { "nice", RLIMIT_NICE }, -#endif -#ifdef RLIMIT_NOFILE - { "nofile", RLIMIT_NOFILE }, -#endif -#ifdef RLIMIT_NPROC - { "nproc", RLIMIT_NPROC }, -#endif -#ifdef RLIMIT_RSS - { "rss", RLIMIT_RSS }, -#endif -#ifdef RLIMIT_RTPRIO - { "rtprio", RLIMIT_RTPRIO }, -#endif -#ifdef RLIMIT_RTTIME - { "rttime", RLIMIT_RTTIME }, -#endif -#ifdef RLIMIT_SIGPENDING - { "sigpending", RLIMIT_SIGPENDING }, -#endif -#ifdef RLIMIT_STACK - { "stack", RLIMIT_STACK }, -#endif - { NULL, 0 }, -}; -#endif /* HAVE_SYS_RESOURCE_H */ - - -/* - * Sets resource limit. - */ - -#ifdef HAVE_SYS_RESOURCE_H -void -util_setrlimit_resource (const char *resource_name, long limit) -{ - int i; - struct rlimit rlim; - char str_limit[64]; - - if (!resource_name) - return; - - if (limit == -1) - snprintf (str_limit, sizeof (str_limit), "unlimited"); - else - snprintf (str_limit, sizeof (str_limit), "%ld", limit); - - for (i = 0; rlimit_resource[i].name; i++) - { - if (strcmp (rlimit_resource[i].name, resource_name) == 0) - { - if (limit < -1) - { - gui_chat_printf (NULL, - _("%sInvalid limit for resource \"%s\": %s " - "(must be >= -1)"), - gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], - resource_name, str_limit); - return; - } - rlim.rlim_cur = (limit >= 0) ? (rlim_t)limit : RLIM_INFINITY; - rlim.rlim_max = rlim.rlim_cur; - if (setrlimit (rlimit_resource[i].resource, &rlim) == 0) - { - log_printf (_("Limit for resource \"%s\" has been set to %s"), - resource_name, str_limit); - if (gui_init_ok) - { - gui_chat_printf (NULL, - _("Limit for resource \"%s\" has been set " - "to %s"), - resource_name, str_limit); - } - } - else - { - gui_chat_printf (NULL, - _("%sUnable to set resource limit \"%s\" to " - "%s: error %d %s"), - gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], - resource_name, str_limit, - errno, strerror (errno)); - } - return; - } - } - - gui_chat_printf (NULL, - _("%sUnknown resource limit \"%s\" (see /help " - "weechat.startup.sys_rlimit)"), - gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], - resource_name); -} -#endif /* HAVE_SYS_RESOURCE_H */ - -/* - * Sets resource limits using value of option "weechat.startup.sys_rlimit". - */ - -void -util_setrlimit () -{ -#ifdef HAVE_SYS_RESOURCE_H - char **items, *pos, *error; - int num_items, i; - long number; - - items = string_split (CONFIG_STRING(config_startup_sys_rlimit), ",", NULL, - WEECHAT_STRING_SPLIT_STRIP_LEFT - | WEECHAT_STRING_SPLIT_STRIP_RIGHT - | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS, - 0, &num_items); - if (items) - { - for (i = 0; i < num_items; i++) - { - pos = strchr (items[i], ':'); - if (pos) - { - pos[0] = '\0'; - error = NULL; - number = strtol (pos + 1, &error, 10); - if (error && !error[0]) - { - util_setrlimit_resource (items[i], number); - } - else - { - gui_chat_printf (NULL, - _("%sInvalid limit for resource \"%s\": " - "%s (must be >= -1)"), - gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], - items[i], pos + 1); - } - } - } - string_free_split (items); - } -#endif /* HAVE_SYS_RESOURCE_H */ -} - /* * Compares two timeval structures. * diff --git a/src/core/wee-util.h b/src/core/wee-util.h index d569e0c43..17ed980d1 100644 --- a/src/core/wee-util.h +++ b/src/core/wee-util.h @@ -23,17 +23,6 @@ #include #include -#ifdef HAVE_SYS_RESOURCE_H -struct t_rlimit_resource -{ - char *name; /* name of resource */ - int resource; /* value of resource */ -}; -#endif /* HAVE_SYS_RESOURCE_H */ - -/* limits */ -extern void util_setrlimit (); - /* timeval */ extern int util_timeval_cmp (struct timeval *tv1, struct timeval *tv2); extern long long util_timeval_diff (struct timeval *tv1, struct timeval *tv2); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 66ff18d8d..84701e431 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -43,6 +43,7 @@ set(LIB_WEECHAT_UNIT_TESTS_CORE_SRC unit/core/test-core-url.cpp unit/core/test-core-utf8.cpp unit/core/test-core-util.cpp + unit/core/test-core-sys.cpp unit/gui/test-gui-bar.cpp unit/gui/test-gui-bar-item.cpp unit/gui/test-gui-bar-item-custom.cpp diff --git a/tests/tests.cpp b/tests/tests.cpp index 8ad13ce57..f82f21f61 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -77,6 +77,7 @@ IMPORT_TEST_GROUP(CoreString); IMPORT_TEST_GROUP(CoreUrl); IMPORT_TEST_GROUP(CoreUtf8); IMPORT_TEST_GROUP(CoreUtil); +IMPORT_TEST_GROUP(CoreSys); /* GUI */ IMPORT_TEST_GROUP(GuiBar); IMPORT_TEST_GROUP(GuiBarItem); diff --git a/tests/unit/core/test-core-sys.cpp b/tests/unit/core/test-core-sys.cpp new file mode 100644 index 000000000..bccb70ff9 --- /dev/null +++ b/tests/unit/core/test-core-sys.cpp @@ -0,0 +1,78 @@ +/* + * test-core-sys.cpp - test system functions + * + * Copyright (C) 2023 Sébastien Helleu + * + * This file is part of WeeChat, the extensible chat client. + * + * WeeChat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * WeeChat is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with WeeChat. If not, see . + */ + +#include "CppUTest/TestHarness.h" + +#include "tests/tests.h" + +extern "C" +{ +#include +#include +#include +#include +#include "src/core/wee-string.h" +#include "src/core/wee-util.h" +} + +TEST_GROUP(CoreSys) +{ +}; + +/* + * Tests functions: + * sys_setrlimit_resource + */ + +TEST(CoreSys, SetrlimitResource) +{ + /* TODO: write tests */ +} + +/* + * Tests functions: + * sys_setrlimit + */ + +TEST(CoreSys, Setrlimit) +{ + /* TODO: write tests */ +} + +/* + * Tests functions: + * sys_display_rlimit + */ + +TEST(CoreSys, DisplayRlimit) +{ + /* TODO: write tests */ +} + +/* + * Tests functions: + * sys_display_rusage + */ + +TEST(CoreSys, DisplayRusage) +{ + /* TODO: write tests */ +} diff --git a/tests/unit/core/test-core-util.cpp b/tests/unit/core/test-core-util.cpp index e5021db53..d4b167970 100644 --- a/tests/unit/core/test-core-util.cpp +++ b/tests/unit/core/test-core-util.cpp @@ -37,26 +37,6 @@ TEST_GROUP(CoreUtil) { }; -/* - * Tests functions: - * util_setrlimit_resource - */ - -TEST(CoreUtil, SetrlimitResource) -{ - /* TODO: write tests */ -} - -/* - * Tests functions: - * util_setrlimit - */ - -TEST(CoreUtil, Setrlimit) -{ - /* TODO: write tests */ -} - /* * Tests functions: * util_timeval_cmp