Skip to content

Commit 7bdb6d7

Browse files
committed
Merge pull request #896
Fix sysctl file read precedence * Respect sort order of filenames from sysctl.d directories (complete commit 140a945). * Display sysctl key value pairs in debug * Add `ni_var_array_sort()` function.
2 parents 5d6e1d4 + 90fd8b8 commit 7bdb6d7

File tree

4 files changed

+41
-38
lines changed

4 files changed

+41
-38
lines changed

client/suse/compat-suse.c

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ static ni_bool_t __ni_ipv6_disbled;
123123
NULL }
124124
#define __NI_SUSE_SYSCTL_SUFFIX ".conf"
125125
#define __NI_SUSE_SYSCTL_BOOT "/boot/sysctl.conf-"
126-
#define __NI_SUSE_SYSCTL_DIRS { "/lib/sysctl.d", \
126+
#define __NI_SUSE_SYSCTL_DIRS { "/run/sysctl.d", \
127+
"/etc/sysctl.d", \
128+
"/usr/local/lib/sysctl.d", \
127129
"/usr/lib/sysctl.d", \
128-
"/usr/local/lib/sysctl.d", \
129-
"/etc/sysctl.d", \
130-
"/run/sysctl.d", \
130+
"/lib/sysctl.d", \
131131
NULL }
132132
#define __NI_SUSE_SYSCTL_FILE "/etc/sysctl.conf"
133133
#define __NI_SUSE_PROC_IPV6_DIR "/proc/sys/net/ipv6"
@@ -323,21 +323,16 @@ __ni_suse_read_default_hostname(const char *root, char **hostname)
323323
return *hostname;
324324
}
325325

326-
static int
327-
__ni_suse_string_compare(const void *lhs, const void *rhs)
328-
{
329-
return strcmp(*(const char **)lhs, *(const char **)rhs);
330-
}
331326

332327
static ni_bool_t
333328
__ni_suse_read_global_ifsysctl(const char *root, const char *path)
334329
{
335330
const char *sysctldirs[] = __NI_SUSE_SYSCTL_DIRS, **sysctld;
336331
ni_string_array_t files = NI_STRING_ARRAY_INIT;
332+
ni_var_array_t sysctl_d_files = NI_VAR_ARRAY_INIT;
337333
char dirname[PATH_MAX];
338334
char pathbuf[PATH_MAX];
339335
const char *name;
340-
char *real = NULL;
341336
unsigned int i;
342337
struct utsname u;
343338

@@ -350,10 +345,8 @@ __ni_suse_read_global_ifsysctl(const char *root, const char *path)
350345
if (uname(&u) == 0) {
351346
snprintf(pathbuf, sizeof(pathbuf), "%s%s%s", root,
352347
__NI_SUSE_SYSCTL_BOOT, u.release);
353-
name = ni_realpath(pathbuf, &real);
354-
if (name && ni_isreg(name))
355-
ni_string_array_append(&files, name);
356-
ni_string_free(&real);
348+
if (ni_file_exists(pathbuf))
349+
ni_string_array_append(&files, pathbuf);
357350
}
358351

359352
/*
@@ -367,38 +360,30 @@ __ni_suse_read_global_ifsysctl(const char *root, const char *path)
367360
continue;
368361

369362
if (ni_scandir(dirname, "*"__NI_SUSE_SYSCTL_SUFFIX, &names)) {
370-
371-
/*
372-
* config files in sysctl.d directories are often prefixed with
373-
* numbers determining an order, so we should preserve that order
374-
*/
375-
qsort(names.data, names.count, sizeof(char *),
376-
__ni_suse_string_compare);
377-
378363
for (i = 0; i < names.count; ++i) {
379364
char *path = NULL;
380365
if (!ni_string_printf(&path, "%s/%s", dirname, names.data[i]))
381366
continue;
382-
name = ni_realpath(path, &real);
367+
if (!ni_var_array_get(&sysctl_d_files, names.data[i]))
368+
ni_var_array_append(&sysctl_d_files, names.data[i], path);
383369
ni_string_free(&path);
384-
if (name && ni_isreg(name))
385-
ni_string_array_append(&files, name);
386-
ni_string_free(&real);
387370
}
388371
}
389372
ni_string_array_destroy(&names);
390373
}
374+
ni_var_array_sort_by_name(&sysctl_d_files);
375+
for(i = 0; i < sysctl_d_files.count; i++)
376+
ni_string_array_append(&files, sysctl_d_files.data[i].value);
377+
ni_var_array_destroy(&sysctl_d_files);
391378

392379
/*
393380
* then the old /etc/sysctl.conf
394381
*/
395382
snprintf(pathbuf, sizeof(pathbuf), "%s%s", root, __NI_SUSE_SYSCTL_FILE);
396-
name = ni_realpath(pathbuf, &real);
397-
if (name && ni_isreg(name)) {
398-
if (ni_string_array_index(&files, name) == -1)
399-
ni_string_array_append(&files, name);
383+
if (ni_file_exists(pathbuf)) {
384+
if (ni_string_array_index(&files, pathbuf) == -1)
385+
ni_string_array_append(&files, pathbuf);
400386
}
401-
ni_string_free(&real);
402387

403388
/*
404389
* finally ifsysctl if they exist
@@ -410,12 +395,10 @@ __ni_suse_read_global_ifsysctl(const char *root, const char *path)
410395
snprintf(pathbuf, sizeof(pathbuf), "%s/%s/%s",
411396
root, path, __NI_SUSE_IFSYSCTL_FILE);
412397

413-
name = ni_realpath(pathbuf, &real);
414-
if (name && ni_isreg(name)) {
415-
if (ni_string_array_index(&files, name) == -1)
416-
ni_string_array_append(&files, name);
398+
if (ni_file_exists(pathbuf)) {
399+
if (ni_string_array_index(&files, pathbuf) == -1)
400+
ni_string_array_append(&files, pathbuf);
417401
}
418-
ni_string_free(&real);
419402

420403
for (i = 0; i < files.count; ++i) {
421404
name = files.data[i];

client/suse/ifsysctl.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,10 @@ __ni_sysctl_file_load(ni_var_array_t *vars, const char *filename,
148148

149149
fp = fopen(filename, "re");
150150
if(fp == NULL) {
151-
if (errno != ENOENT) {
151+
if (errno != ENOENT)
152152
ni_error("Unable to open %s: %m", filename);
153-
}
153+
else
154+
ni_debug_readwrite("Cannot open '%s': %m", filename);
154155
return FALSE;
155156
}
156157

@@ -261,6 +262,7 @@ __ni_ifsysctl_vars_map(ni_var_array_t *vars, const char *key, const char *val)
261262
/*
262263
* And finally add it to the array
263264
*/
265+
ni_debug_readwrite("Add sysctl variable '%s=%s'", key, val);
264266
ni_var_array_set(vars, key, val);
265267
ni_stringbuf_destroy(&buf);
266268
}

include/wicked/util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct ni_variable {
4444
char * name;
4545
char * value;
4646
};
47+
typedef int (*ni_var_compare_fn_t)(const ni_var_t*, const ni_var_t*);
4748

4849
#define NI_VAR_INIT { .name = NULL, .value = NULL }
4950

@@ -193,6 +194,9 @@ extern ni_bool_t ni_var_array_set_ulong(ni_var_array_t *, const char *, unsigned
193194
extern ni_bool_t ni_var_array_set_double(ni_var_array_t *, const char *, double);
194195
extern ni_bool_t ni_var_array_set_boolean(ni_var_array_t *, const char *, int);
195196

197+
extern void ni_var_array_sort(ni_var_array_t *, ni_var_compare_fn_t);
198+
extern void ni_var_array_sort_by_name(ni_var_array_t *);
199+
196200
extern void ni_var_array_list_append(ni_var_array_t **, ni_var_array_t *);
197201
extern void ni_var_array_list_destroy(ni_var_array_t **);
198202

src/util.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <dirent.h>
1515
#include <ctype.h>
1616
#include <stdio.h>
17+
#include <stdlib.h>
1718
#include <unistd.h>
1819
#include <signal.h>
1920
#include <fcntl.h>
@@ -1035,6 +1036,19 @@ ni_var_array_set_boolean(ni_var_array_t *nva, const char *name, int value)
10351036
return ni_var_array_set(nva, name, value? "yes" : "no");
10361037
}
10371038

1039+
void
1040+
ni_var_array_sort(ni_var_array_t *nva, ni_var_compare_fn_t fn)
1041+
{
1042+
qsort(nva->data, nva->count, sizeof(ni_var_t),
1043+
(int (*)(const void *, const void *)) fn);
1044+
}
1045+
1046+
void
1047+
ni_var_array_sort_by_name(ni_var_array_t *nva)
1048+
{
1049+
ni_var_array_sort(nva, ni_var_name_cmp);
1050+
}
1051+
10381052
void
10391053
ni_var_array_list_append(ni_var_array_t **list, ni_var_array_t *nva)
10401054
{

0 commit comments

Comments
 (0)