mirror of
https://github.com/weechat/weechat.git
synced 2026-06-30 14:56:39 +02:00
core: add command /sys to show resource limits and usage
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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 | Тестови: бафер функције.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -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 ();
|
||||
|
||||
|
||||
@@ -0,0 +1,345 @@
|
||||
/*
|
||||
* wee-sys.c - system actions
|
||||
*
|
||||
* Copyright (C) 2023 Sébastien Helleu <flashcode@flashtux.org>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/time.h>
|
||||
#ifdef HAVE_SYS_RESOURCE_H
|
||||
#include <sys/resource.h>
|
||||
#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 */
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Sébastien Helleu <flashcode@flashtux.org>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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 */
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -23,17 +23,6 @@
|
||||
#include <time.h>
|
||||
#include <sys/time.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 */
|
||||
|
||||
/* 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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* test-core-sys.cpp - test system functions
|
||||
*
|
||||
* Copyright (C) 2023 Sébastien Helleu <flashcode@flashtux.org>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "CppUTest/TestHarness.h"
|
||||
|
||||
#include "tests/tests.h"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
#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 */
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user