summaryrefslogtreecommitdiff
path: root/src/shutdown/hpr_confirm_hostname.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2020-11-06 17:11:28 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2020-11-06 17:11:28 +0000
commit7aa18bcbd83c4342bc2eefa4d6d96756ae7541a0 (patch)
tree157e1787d165dd052b2d49772887e31f49394c68 /src/shutdown/hpr_confirm_hostname.c
parent4d47d83042e0844da2fcfff9bd73de60124e7902 (diff)
downloads6-linux-init-7aa18bcbd83c4342bc2eefa4d6d96756ae7541a0.tar.xz
Add -i option to shutdown and hpr
Diffstat (limited to 'src/shutdown/hpr_confirm_hostname.c')
-rw-r--r--src/shutdown/hpr_confirm_hostname.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/shutdown/hpr_confirm_hostname.c b/src/shutdown/hpr_confirm_hostname.c
new file mode 100644
index 0000000..00b3de7
--- /dev/null
+++ b/src/shutdown/hpr_confirm_hostname.c
@@ -0,0 +1,45 @@
+/* ISC license. */
+
+#include <string.h>
+#include <strings.h>
+#include <unistd.h>
+#include <termios.h>
+#include <errno.h>
+#include <limits.h>
+
+#include <skalibs/allreadwrite.h>
+#include <skalibs/bytestr.h>
+#include <skalibs/strerr2.h>
+
+#include "hpr.h"
+
+#define PROMPT "Please enter the machine's hostname: "
+
+void hpr_confirm_hostname (void)
+{
+ char name[HOST_NAME_MAX + 1] ;
+ char buf[HOST_NAME_MAX + 1] ;
+ char *p ;
+ ssize_t r ;
+ if (!isatty(0) || !isatty(1))
+ strerr_diefu1sys(100, "ask hostname confirmation") ;
+ if (gethostname(name, HOST_NAME_MAX) < 0)
+ strerr_diefu1sys(111, "get host name") ;
+ name[HOST_NAME_MAX] = 0 ;
+ p = strchr(name, '.') ;
+ if (p) *p = 0 ;
+ if (allwrite(1, PROMPT, sizeof(PROMPT)-1) < 0)
+ strerr_diefu1sys(111, "write to stdout") ;
+ if (tcdrain(1) < 0)
+ strerr_diefu1sys(111, "tcdrain stdout") ;
+ if (tcflush(0, TCIFLUSH) < 0)
+ strerr_diefu1sys(111, "empty stdin buffer") ;
+ r = fd_read(0, buf, HOST_NAME_MAX) ;
+ if (!r) errno = EPIPE ;
+ if (r <= 0) strerr_diefu1sys(111, "read from stdin") ;
+ buf[byte_chr(buf, r, '\n')] = 0 ;
+ p = strchr(buf, '.') ;
+ if (p) *p = 0 ;
+ if (strcasecmp(name, buf))
+ strerr_dief2x(1, "hostname mismatch: expecting ", name) ;
+}