Skip to content

Commit 5b1749b

Browse files
committed
options: restrict daemon client priorities
1 parent f4f9cd5 commit 5b1749b

5 files changed

Lines changed: 74 additions & 225 deletions

File tree

‎configure.ac‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ AC_CHECK_FUNCS(waitpid wait4 getcwd chown chmod lchmod mknod mkfifo fdopendir \
978978
seteuid strerror putenv iconv_open locale_charset nl_langinfo getxattr \
979979
extattr_get_link sigaction sigprocmask setattrlist getgrouplist \
980980
initgroups utimensat futimens posix_fallocate attropen setvbuf nanosleep usleep \
981-
setenv unsetenv mbrtowc getpriority setpriority)
981+
setenv unsetenv mbrtowc setpriority)
982982

983983
dnl dirfd() is a macro or static inline on several systems (the BSDs), so the
984984
dnl default AC_CHECK_FUNCS link probe -- which declares `char dirfd(void);` and

‎main.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,8 @@ void start_server(int f_in, int f_out, int argc, char *argv[])
13091309

13101310
if (protocol_version >= 23)
13111311
io_start_multiplex_out(f_out);
1312+
/* Priority diagnostics need the negotiated message channel.
1313+
* File-list work starts below. */
13121314
niceness_apply();
13131315
if (am_daemon && io_timeout && protocol_version >= 31)
13141316
send_msg_int(MSG_IO_TIMEOUT, io_timeout);

‎niceness.c‎

Lines changed: 9 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,21 @@
99
#ifdef HAVE_SYS_SYSCALL_H
1010
#include <sys/syscall.h>
1111
#endif
12-
1312
extern int am_server;
1413
extern int am_daemon;
1514

1615
#define PRIORITY_UNSET (-100)
1716
#define DEFAULT_NICE 19
1817

19-
#if defined HAVE_GETPRIORITY && defined HAVE_SYS_RESOURCE_H
20-
#define SUPPORT_GET_PRIORITY 1
21-
#endif
22-
23-
#if defined SUPPORT_GET_PRIORITY && defined HAVE_SETPRIORITY
18+
#if defined HAVE_SETPRIORITY && defined HAVE_SYS_RESOURCE_H
2419
#define SUPPORT_NICE_PRIORITY 1
2520
#endif
2621

2722
#if defined HAVE_LINUX_IOPRIO_H && defined HAVE_SYS_SYSCALL_H \
28-
&& defined SUPPORT_GET_PRIORITY \
29-
&& defined SYS_ioprio_get && defined SYS_ioprio_set \
30-
&& defined IOPRIO_PRIO_CLASS \
31-
&& defined IOPRIO_PRIO_DATA && defined IOPRIO_PRIO_VALUE
23+
&& defined SYS_ioprio_set && defined IOPRIO_PRIO_VALUE
3224
#define SUPPORT_IO_PRIORITY 1
3325
#endif
3426

35-
#ifdef SUPPORT_IO_PRIORITY
36-
#ifdef IOPRIO_PRIO_LEVEL
37-
#define RSYNC_IOPRIO_LEVEL(value) IOPRIO_PRIO_LEVEL(value)
38-
#else
39-
/* Older headers expose the whole data field; the level is its low 3 bits. */
40-
#define RSYNC_IOPRIO_LEVEL(value) (IOPRIO_PRIO_DATA(value) & 7)
41-
#endif
42-
#endif
43-
4427
enum io_priority_class {
4528
IO_PRIORITY_NONE,
4629
IO_PRIORITY_REALTIME,
@@ -266,120 +249,21 @@ char *niceness_remote_option(void)
266249
return arg;
267250
}
268251

269-
#ifdef SUPPORT_IO_PRIORITY
270-
static int effective_io_priority(int class, int data, int cpu, int *rank_p, int *data_p)
271-
{
272-
if (class == IOPRIO_CLASS_NONE) {
273-
class = IOPRIO_CLASS_BE;
274-
data = (cpu + 20) / 5;
275-
if (data > 7)
276-
data = 7;
277-
}
278-
279-
switch (class) {
280-
case IOPRIO_CLASS_RT:
281-
*rank_p = 0;
282-
break;
283-
case IOPRIO_CLASS_BE:
284-
*rank_p = 1;
285-
break;
286-
case IOPRIO_CLASS_IDLE:
287-
*rank_p = 2;
288-
data = 0;
289-
break;
290-
default:
291-
return 0;
292-
}
293-
*data_p = data;
294-
return 1;
295-
}
296-
297-
static int requested_io_priority(const struct priority_setting *setting, int cpu,
298-
int *rank_p, int *data_p)
299-
{
300-
int class;
301-
302-
switch (setting->io_class) {
303-
case IO_PRIORITY_NONE:
304-
class = IOPRIO_CLASS_NONE;
305-
break;
306-
case IO_PRIORITY_REALTIME:
307-
class = IOPRIO_CLASS_RT;
308-
break;
309-
case IO_PRIORITY_BEST_EFFORT:
310-
class = IOPRIO_CLASS_BE;
311-
break;
312-
case IO_PRIORITY_IDLE:
313-
class = IOPRIO_CLASS_IDLE;
314-
break;
315-
default:
316-
return 0;
317-
}
318-
return effective_io_priority(class, setting->io_data, cpu, rank_p, data_p);
319-
}
320-
#endif
321-
322252
int niceness_check_daemon_request(char *err_buf, size_t err_len)
323253
{
324254
struct priority_setting *setting = &priorities.local;
325255

326-
if (setting->cpu == PRIORITY_UNSET && setting->io_class == PRIORITY_UNSET)
327-
return 1;
328-
329-
#if defined SUPPORT_NICE_PRIORITY || defined SUPPORT_IO_PRIORITY
330-
int current_cpu;
331-
332-
errno = 0;
333-
current_cpu = getpriority(PRIO_PROCESS, 0);
334-
if (errno) {
335-
snprintf(err_buf, err_len, "cannot inspect daemon process priority: %s\n",
336-
strerror(errno));
256+
if (setting->cpu != PRIORITY_UNSET && setting->cpu != DEFAULT_NICE) {
257+
snprintf(err_buf, err_len,
258+
"daemon clients may only request CPU nice 19\n");
337259
return 0;
338260
}
339-
#endif
340-
341-
#ifdef SUPPORT_NICE_PRIORITY
342-
if (setting->cpu != PRIORITY_UNSET && setting->cpu < current_cpu) {
343-
snprintf(err_buf, err_len, "--nice cannot raise daemon CPU priority\n");
261+
if (setting->io_class != PRIORITY_UNSET
262+
&& setting->io_class != IO_PRIORITY_IDLE) {
263+
snprintf(err_buf, err_len,
264+
"daemon clients may only request idle I/O priority\n");
344265
return 0;
345266
}
346-
#endif
347-
348-
#ifdef SUPPORT_IO_PRIORITY
349-
if (setting->io_class != PRIORITY_UNSET) {
350-
int raw = syscall(SYS_ioprio_get, IOPRIO_WHO_PROCESS, 0);
351-
int requested_cpu = current_cpu;
352-
int current_rank, current_data, requested_rank, requested_data;
353-
354-
if (raw < 0) {
355-
snprintf(err_buf, err_len, "cannot inspect daemon I/O priority: %s\n",
356-
strerror(errno));
357-
return 0;
358-
}
359-
#ifdef SUPPORT_NICE_PRIORITY
360-
if (setting->cpu != PRIORITY_UNSET)
361-
requested_cpu = setting->cpu;
362-
#endif
363-
/* IOPRIO_CLASS_NONE derives its level from CPU nice. */
364-
if (!effective_io_priority(IOPRIO_PRIO_CLASS(raw), RSYNC_IOPRIO_LEVEL(raw),
365-
current_cpu, &current_rank, &current_data)
366-
|| !requested_io_priority(setting, requested_cpu,
367-
&requested_rank, &requested_data)) {
368-
snprintf(err_buf, err_len, "unsupported daemon I/O priority class\n");
369-
return 0;
370-
}
371-
if (requested_rank < current_rank
372-
|| (requested_rank == current_rank && requested_data < current_data)) {
373-
snprintf(err_buf, err_len, "--nice cannot raise daemon I/O priority\n");
374-
return 0;
375-
}
376-
}
377-
#endif
378-
379-
#if !defined SUPPORT_NICE_PRIORITY && !defined SUPPORT_IO_PRIORITY
380-
(void)err_buf;
381-
(void)err_len;
382-
#endif
383267

384268
return 1;
385269
}

‎rsync.1.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4163,11 +4163,11 @@ sign) if you want the local shell to expand it.
41634163
--nice=local:19/be_7,remote:5
41644164

41654165
The remote rsync must support this option before a remote setting can be
4166-
used. Daemon clients may keep or lower the daemon worker's inherited
4167-
priorities but cannot raise them. A daemon administrator can also refuse
4168-
the option with `refuse options = nice`. When starting a daemon, the local
4169-
setting is applied before it accepts client settings. A standalone listener
4170-
passes that priority to its workers. The remote setting has no meaning.
4166+
used. Daemon clients may request only CPU nice 19, idle I/O priority or
4167+
both. A daemon administrator can also refuse the option with
4168+
`refuse options = nice`. When starting a daemon, the local setting is
4169+
applied before it accepts client settings. A standalone listener passes
4170+
that priority to its workers. A daemon's remote setting has no meaning.
41714171

41724172
If a priority change is unsupported or fails, rsync warns and continues the
41734173
transfer.

0 commit comments

Comments
 (0)