From 0ae929fa94a0d9d88014b0ff46fe6b9a99d4925b Mon Sep 17 00:00:00 2001 From: David Howells Date: Tue, 22 Jan 2019 17:47:31 +0000 Subject: [PATCH] Honour 'sysname' setting in preload If a defaults section exists in the configuration and this contains a setting for the workstation's @sys substitution list, e.g.: [defaults] sysname = fedora28_x86 amd64_linux then load this into kernel after the cell list has been preloaded. Signed-off-by: David Howells --- src/include/kafs/cellserv.h | 1 + src/lib_cell_lookup.c | 6 ++++ src/preload-cells.c | 62 +++++++++++++++++++++---------------- 3 files changed, 42 insertions(+), 27 deletions(-) diff --git a/src/include/kafs/cellserv.h b/src/include/kafs/cellserv.h index 00e081e..a232b0b 100644 --- a/src/include/kafs/cellserv.h +++ b/src/include/kafs/cellserv.h @@ -147,6 +147,7 @@ extern int kafs_dns_lookup_vlservers(struct kafs_server_list *vsl, extern struct kafs_profile kafs_config_profile; extern struct kafs_cell_db *kafs_cellserv_db; extern const char *kafs_this_cell; +extern const char *kafs_sysname; extern int kafs_read_config(const char *const *files, struct kafs_report *report); extern struct kafs_cell *kafs_lookup_cell(const char *cell_name, diff --git a/src/lib_cell_lookup.c b/src/lib_cell_lookup.c index 2029707..f048151 100644 --- a/src/lib_cell_lookup.c +++ b/src/lib_cell_lookup.c @@ -24,6 +24,7 @@ static const char *const kafs_std_config[] = { struct kafs_profile kafs_config_profile = { .name = "" }; struct kafs_cell_db *kafs_cellserv_db; const char *kafs_this_cell; +const char *kafs_sysname; #define verbose(r, fmt, ...) \ do { \ @@ -72,6 +73,11 @@ static void kafs_read_defaults(struct kafs_profile *prof, struct kafs_report *re p = kafs_profile_get_string(def, "thiscell", report); if (p) kafs_this_cell = p; + + /* Find the @sys substitutions (sysname = ...) */ + p = kafs_profile_get_string(def, "sysname", report); + if (p) + kafs_sysname = p; } /* diff --git a/src/preload-cells.c b/src/preload-cells.c index c51bba3..d0db1c6 100644 --- a/src/preload-cells.c +++ b/src/preload-cells.c @@ -61,6 +61,39 @@ static void _error(const char *fmt, ...) va_end(va); } +/* + * Write a string to a proc file. + */ +static void write_to_proc(const char *file, const char *str, bool redirect_to_stdout) +{ + int fd = 1, n; + + if (!str) + return; + + if (redirect_to_stdout) { + printf("WRITE '%s' TO %s\n", str, file); + return; + } + + fd = open(file, O_WRONLY); + if (fd == -1) { + _error("Can't open %s: %m", file); + exit(1); + } + + n = strlen(str); + if (write(fd, str, n) != n) { + _error("Can't write '%s' to %s: %m", str, file); + exit(1); + } + + if (close(fd) == -1) { + _error("Can't close %s: %m", file); + exit(1); + } +} + /* * Parse the cell database file */ @@ -104,33 +137,8 @@ int do_preload(const struct kafs_cell_db *db, bool redirect_to_stdout) } } - if (kafs_this_cell) { - if (!redirect_to_stdout) { - fd = open("/proc/net/afs/rootcell", O_WRONLY); - if (fd == -1) { - _error("Can't open /proc/fs/afs/rootcell: %m"); - exit(1); - } - } - - n = strlen(kafs_this_cell); - if (write(fd, kafs_this_cell, n) != n) { - _error("Can't set root cell '%s': %m", kafs_this_cell); - exit(1); - } - - if (!redirect_to_stdout) { - if (close(fd) == -1) { - _error("Can't close /proc/fs/afs/rootcell: %m"); - exit(1); - } - } else { - if (write(1, "\n", 1) == -1) - perror("stdout"); - } - - } - + write_to_proc("/proc/net/afs/rootcell", kafs_this_cell, redirect_to_stdout); + write_to_proc("/proc/net/afs/sysname", kafs_sysname, redirect_to_stdout); exit(0); } -- 2.49.0