This is necessary because python3 insists that hostnames should
be utf-8, which is a rather questionable assumption.
Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
Reviewed-by: Rob Browning <rlb@defaultvalue.org>
[rlb@defaultvalue.org: don't define HOST_NAME_MAX if it's not already]
Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
+static PyObject *bup_gethostname(PyObject *mod, PyObject *ignore)
+{
+#ifdef HOST_NAME_MAX
+ char buf[HOST_NAME_MAX + 1] = {};
+#else
+ /* 'SUSv2 guarantees that "Host names are limited to 255 bytes".' */
+ char buf[256] = {};
+#endif
+
+ if (gethostname(buf, sizeof(buf) - 1))
+ return PyErr_SetFromErrno(PyExc_IOError);
+ return PyBytes_FromString(buf);
+}
+
static PyMethodDef helper_methods[] = {
{ "write_sparsely", bup_write_sparsely, METH_VARARGS,
"Write buf excepting zeros at the end. Return trailing zero count." },
static PyMethodDef helper_methods[] = {
{ "write_sparsely", bup_write_sparsely, METH_VARARGS,
"Write buf excepting zeros at the end. Return trailing zero count." },
"Return the group database entry for the given group name,"
" as a tuple with all C strings as bytes(), or None if the group does"
" not exist." },
"Return the group database entry for the given group name,"
" as a tuple with all C strings as bytes(), or None if the group does"
" not exist." },
+ { "gethostname", bup_gethostname, METH_NOARGS,
+ "Return the current hostname (as bytes)" },
{ NULL, NULL, 0, NULL }, // sentinel
};
{ NULL, NULL, 0, NULL }, // sentinel
};
"""Get the FQDN of this machine."""
global _hostname
if not _hostname:
"""Get the FQDN of this machine."""
global _hostname
if not _hostname:
- _hostname = socket.getfqdn().encode('iso-8859-1')
+ _hostname = _helpers.gethostname()