summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA. Wilcox <AWilcox@Wilcox-Tech.com>2022-04-23 02:41:04 -0500
committerA. Wilcox <AWilcox@Wilcox-Tech.com>2022-04-23 02:41:04 -0500
commit2a708d9795c2474be8af2b4920c3806fc7b8f5cf (patch)
treea571d1153581f677c1bb84c847dbf541d50faab7
parent7b782cd4082e4d8aae483b5e4b68b62063ec2615 (diff)
downloadshimmy-2a708d9795c2474be8af2b4920c3806fc7b8f5cf.tar.gz
shimmy-2a708d9795c2474be8af2b4920c3806fc7b8f5cf.tar.bz2
shimmy-2a708d9795c2474be8af2b4920c3806fc7b8f5cf.tar.xz
shimmy-2a708d9795c2474be8af2b4920c3806fc7b8f5cf.zip
hostname: Add ability to set node name
-rw-r--r--hostname/hostname.c62
1 files changed, 59 insertions, 3 deletions
diff --git a/hostname/hostname.c b/hostname/hostname.c
index 3dc1224..d19e4b8 100644
--- a/hostname/hostname.c
+++ b/hostname/hostname.c
@@ -18,6 +18,18 @@
#include <sys/socket.h>
#include <netdb.h>
+/* open, read, close */
+#include <sys/stat.h>
+#include <fcntl.h>
+
+int sethostname(const char *value,
+#ifdef __linux__
+ size_t
+#else
+ int
+#endif
+ namelen);
+
int do_print_nodename(bool verbose, bool domain, bool fqdn, bool address, bool shorten)
{
long hostname = sysconf(_SC_HOST_NAME_MAX);
@@ -113,9 +125,53 @@ int do_print_nodename(bool verbose, bool domain, bool fqdn, bool address, bool s
int do_set_nodename(bool file, const char *value)
{
- fprintf(stderr, "setting nodenamet to %s %s not yet supported\n",
- (file ? "contents of file" : "literal string"), value);
- return 1;
+ size_t len;
+
+ if(file)
+ {
+ /* This is insane. There is no validation whatsoever.
+ * However, strace on net-tools hostname(1) shows this to be
+ * the exact algorithm used, so... */
+ char buf[1024];
+ int fildes = open(value, O_RDONLY);
+ ssize_t result = 0;
+
+ if(fildes == -1)
+ {
+ perror("open");
+ return EXIT_FAILURE;
+ }
+
+ while(true)
+ {
+ char *curr = buf;
+ result = read(fildes, buf, sizeof buf);
+ switch(result)
+ {
+ case -1:
+ perror("read");
+ close(fildes);
+ return EXIT_FAILURE;
+ case 0:
+ close(fildes);
+ return EXIT_SUCCESS;
+ }
+ do
+ {
+ len = result;
+ char *pos = strchr(curr, '\n');
+ if(pos)
+ {
+ len = pos - curr;
+ }
+ sethostname(curr, (len > 63) ? 63 : len);
+ } while((curr = strchr(curr, '\n')));
+ }
+ }
+
+ len = strlen(value);
+ sethostname(value, len);
+ return EXIT_SUCCESS;
}
void usage()