Exemplo n.º 1
0
void do_sethostname(char *s, int isfile)
{
	FILE *f;
	char buf[255];

	if (!s)
		return;
	if (!isfile) {
		if (sethostname(s, strlen(s)) < 0) {
			if (errno == EPERM)
			{
				printf("%% you must be root to change the hostname\n");
				return;
			}
			else
			{
				printf("%% error sethostname\r\n");
				return;
			}
		}
	} else {
		f = xfopen(s, "r");
		fgets(buf, 255, f);
		fclose(f);
		if (buf[strlen(buf) - 1] == '\n')
			buf[strlen(buf) - 1] = 0;
		if (sethostname(buf, strlen(buf)) < 0)
			{
			    printf("%% error sethostname\n");
			    return;
			}    
	}
}
Exemplo n.º 2
0
ATF_TC_BODY(sethostname_perm, tc)
{

	errno = 0;

	ATF_REQUIRE_ERRNO(EPERM, sethostname(host, sizeof(host)) == -1);
}
Exemplo n.º 3
0
int child_main(void* arg)
{
    char c;

    // init sync primitive
    close(checkpoint[1]);

    // setup hostname
    printf(" - [%5d] World !\n", getpid());
    sethostname("In Namespace", 12);

    // remount "/proc" to get accurate "top" && "ps" output

    chdir("root/");
    int chroot_err = chroot(".");
    if (chroot_err != 0) {
        printf("chroot error\n");
    }
    mount("proc", "/proc", "proc", 0, NULL);
    chdir("/bin");
    setuid(0);
    setgid(0);

    // wait...
    read(checkpoint[0], &c, 1);

    execv(child_args[0], child_args);
    printf("Ooops\n");
    printf("result %d\n", errno);
    return 1;
}
Exemplo n.º 4
0
int hyper_start_containers(struct hyper_pod *pod)
{
	int i;

	/* mount new proc directory */
	if (umount("/proc") < 0) {
		perror("umount proc filesystem failed\n");
		return -1;
	}

	if (mount("proc", "/proc", "proc", 0, NULL) < 0) {
		perror("mount proc filesystem failed\n");
		return -1;
	}

	if (sethostname(pod->hostname, strlen(pod->hostname)) < 0) {
		perror("set host name failed");
		return -1;
	}

	for (i = 0; i < pod->c_num; i++)
		hyper_start_container(&pod->c[i]);

	return 0;
}
int main() {
  const char * old_domain_name = get_domain_name();
  const char * old_host_name = get_host_name();

  step("parent: old domainname: %s", old_domain_name);
  step("parent: old hostname: %s", old_host_name);

  step("parent: fork");
  if (fork()) {
    step("parent: wait for child to exit");
    int status = 0;
    do wait(&status); while (!WIFEXITED(status));

    assertStrEquals("parent: domain name", old_domain_name, get_domain_name());
    assertStrEquals("parent: host name", old_host_name, get_host_name());
  } else {
    step("child: create container with separate uts namespace");
    struct slc_create_container_parameters params;
    initialize_default_fs_root(&params.fs_root);
    slc_create_container(&params, 0);

    const char * new_domain_name = "new_domain_name";
    const char * new_host_name = "new_host_name";
    
    step("child: set domain name to '%s'", new_domain_name);
    setdomainname(new_domain_name, strlen(new_domain_name));
    assertStrEquals("child: domain name", new_domain_name, get_domain_name());
    
    step("child: set host name to '%s'", new_host_name);
    sethostname(new_host_name, strlen(new_host_name));
    assertStrEquals("child: host name", new_host_name, get_host_name());
  }

  return 0;
}
Exemplo n.º 6
0
TError SetHostName(const std::string &name) {
    int ret = sethostname(name.c_str(), name.length());
    if (ret < 0)
        return TError(EError::Unknown, errno, "sethostname(" + name + ")");

    return TError::Success();
}
Exemplo n.º 7
0
int child_main(void* arg)
{
  char c;

  // init sync primitive
  close(checkpoint[1]);

  // setup hostname
  printf(" - [%5d] World !\n", getpid());
  sethostname("In Namespace", 12);

  // remount "/proc" to get accurate "top" && "ps" output
  mount("proc", "/proc", "proc", 0, NULL);

  // wait for network setup in parent
  read(checkpoint[0], &c, 1);

  // setup network
  system("ip link set lo up");
  system("ip link set veth1 up");
  system("ip addr add 169.254.1.2/30 dev veth1");

  execv(child_args[0], child_args);
  printf("Ooops\n");
  return 1;
}
Exemplo n.º 8
0
static int context_update_kernel_hostname(Context *c) {
        const char *static_hn;
        const char *hn;

        assert(c);

        static_hn = c->data[PROP_STATIC_HOSTNAME];

        /* /etc/hostname with something other than "localhost"
         * has the highest preference ... */
        if (hostname_is_useful(static_hn))
                hn = static_hn;

        /* ... the transient host name, (ie: DHCP) comes next ...*/
        else if (!isempty(c->data[PROP_HOSTNAME]))
                hn = c->data[PROP_HOSTNAME];

        /* ... fallback to static "localhost.*" ignored above ... */
        else if (!isempty(static_hn))
                hn = static_hn;

        /* ... and the ultimate fallback */
        else
                hn = "localhost";

        if (sethostname(hn, strlen(hn)) < 0)
                return -errno;

        return 0;
}
Exemplo n.º 9
0
static int loopback_set_hostname(const char *hostname)
{
	const char *ptr;
	int err, len;

	if (g_strcmp0(hostname, "<hostname>") == 0)
		return 0;

	len = strlen(hostname);

	if (connman_inet_check_hostname(hostname, len) == FALSE)
		return -EINVAL;

	if ((ptr = strstr(hostname, ".")) != NULL)
		len = ptr - hostname;

	if (sethostname(hostname, len) < 0) {
		err = -errno;
		connman_error("Failed to set hostname to %s", hostname);
		return err;
	}

	connman_info("Setting hostname to %s", hostname);

	return 0;
}
Exemplo n.º 10
0
int command_hostname (int argc, char **argv)
{
   char hostname[MAXHOSTNAMELEN];

   progname = argv[0];
   
   if (argc == 1)
   {
      if (gethostname (hostname, MAXHOSTNAMELEN) < 0)
         error ("cannot get hostname: %s", strerror (errno));
      puts (hostname);
      return (EXIT_SUCCESS);
   }
   else if (argc == 2)
   {
      if (!strcmp (argv[1], "--help"))
         puts (help_text);
      else if (!strcmp (argv[1], "--version"))
         puts ("hostname: version "VERSION);
      else
      {
         if (sethostname (argv[1], strlen (argv[1])) < 0)
            error ("cannot set hostname: %s", strerror (errno));
      }
   }
   else
      terror ("too many arguments");
      
   return (EXIT_SUCCESS);
}
Exemplo n.º 11
0
/*
 * cleanup()  - performs all one time cleanup for this test
 *		completion or premature exit.
 */
void
cleanup()
{
	int ret;
	/*
	 * print timing stats if that option was specified.
	 * print errno log if that option was specified.
	 */
	TEST_CLEANUP;

	 /* Set effective user id back to root/super user */
	if (seteuid(0) == -1) {
		tst_resm(TWARN, "seteuid failed to "
			"to set the effective uid to root" );
		perror("seteuid");
	}

	/* Restore host name */
	if( (ret = sethostname (hname, strlen(hname))) < 0 ) {
		tst_resm(TWARN, "sethostname() failed while restoring"
			" hostname to \"%s\"", hname);
	} 

	/* exit with return code appropriate for results */
	tst_exit();
}
Exemplo n.º 12
0
ATF_TC_BODY(sethostname_basic, tc)
{
	char name[MAXHOSTNAMELEN];
	size_t i;

	for (i = 0; i < __arraycount(hosts); i++) {

		(void)memset(name, 0, sizeof(name));

		ATF_REQUIRE(sethostname(hosts[i], sizeof(hosts[i])) == 0);
		ATF_REQUIRE(gethostname(name, sizeof(name)) == 0);
		ATF_REQUIRE(strcmp(hosts[i], name) == 0);
	}

	(void)sethostname(host, sizeof(host));
}
Exemplo n.º 13
0
static int uwsgi_hook_hostname(char *arg) {
#ifdef __CYGWIN__
	return -1;
#else
	return sethostname(arg, strlen(arg));
#endif
}
Exemplo n.º 14
0
int container_main(void * arg) {
    printf("Container - inside the container!\n"); 
    sethostname("container",10); /* 设置hostname */
    execv(container_args[0], container_args);
    printf("Something's wrong\n");
    return 1;
}
Exemplo n.º 15
0
gboolean
nm_policy_set_system_hostname (const char *new_hostname, const char *msg)
{
	char old_hostname[HOST_NAME_MAX + 1];
	const char *name;
	int ret;

	if (new_hostname)
		g_warn_if_fail (strlen (new_hostname));

	old_hostname[HOST_NAME_MAX] = '\0';
	errno = 0;
	ret = gethostname (old_hostname, HOST_NAME_MAX);
	if (ret != 0) {
		nm_log_warn (LOGD_DNS, "couldn't get the system hostname: (%d) %s",
		             errno, strerror (errno));
	} else {
		/* Don't set the hostname if it isn't actually changing */
		if (   (new_hostname && !strcmp (old_hostname, new_hostname))
		    || (!new_hostname && !strcmp (old_hostname, FALLBACK_HOSTNAME4)))
			return FALSE;
	}

	name = (new_hostname && strlen (new_hostname)) ? new_hostname : FALLBACK_HOSTNAME4;

	nm_log_info (LOGD_DNS, "Setting system hostname to '%s' (%s)", name, msg);
	ret = sethostname (name, strlen (name));
	if (ret != 0) {
		nm_log_warn (LOGD_DNS, "couldn't set the system hostname to '%s': (%d) %s",
		             name, errno, strerror (errno));
	}

	return (ret == 0);
}
Exemplo n.º 16
0
TEST(UNISTD_TEST, sethostname) {
  // The permissions check happens before the argument check, so this will
  // fail for a different reason if you're running as root than if you're
  // not, but it'll fail either way. Checking that we have the symbol is about
  // all we can do for sethostname(2).
  ASSERT_EQ(-1, sethostname("", -1));
}
Exemplo n.º 17
0
int main(int ac, char **av)
{
    int lc;

    tst_parse_opts(ac, av, NULL, NULL);

    /* Do initial setup */
    setup();

    /* Check for looping state if -i option is given */
    for (lc = 0; TEST_LOOPING(lc); lc++) {

        tst_count = 0;

        /* call sethostname() */
        TEST(sethostname(ltpthost, sizeof(ltpthost)));

        if ((TEST_RETURN == -1) && (TEST_ERRNO == EPERM)) {
            tst_resm(TPASS, "Expected Failure; Got EPERM");
        } else {
            tst_resm(TFAIL, "call failed to produce "
                     "expected error;  errno: %d : %s",
                     TEST_ERRNO, strerror(TEST_ERRNO));
        }

    }

    /* cleanup and exit */
    cleanup();
    tst_exit();

}
Exemplo n.º 18
0
static PyObject *_agentlib_sethostname(PyObject *self, PyObject *args)
{
    char *host_string;
    int err;
    size_t host_len;

    if (!PyArg_ParseTuple(args, "s", &host_string))
    {
        return NULL;
    }

    host_len = strlen(host_string);
    if (host_len > 63)
        host_len = 63;

    err = sethostname(host_string, host_len);
    if (err < 0)
    {
        err = errno;
        return PyErr_Format(PyExc_SystemError,
                "sethostname() failed with errno '%d'", err);
    }

    Py_RETURN_NONE;
}
Exemplo n.º 19
0
static void do_sethostname(char *s, int isfile)
{
	FILE *f;
	char buf[255];

	if (!s)
		return;
	if (!isfile) {
		if (sethostname(s, strlen(s)) < 0) {
			if (errno == EPERM)
				bb_error_msg_and_die("you must be root to change the hostname");
			else
				bb_perror_msg_and_die("sethostname");
		}
	} else {
		f = bb_xfopen(s, "r");
		while (fgets(buf, 255, f) != NULL) {
			if (buf[0] =='#') {
				continue;
			}
			chomp(buf);
			do_sethostname(buf, 0);
		}
#ifdef CONFIG_FEATURE_CLEAN_UP
		fclose(f);
#endif
	}
}
Exemplo n.º 20
0
static int childFunc(void *arg) {
  struct utsname uts;
  sethostname(arg, strlen(arg)); /* Change hostname in UTS namespace of child */
  uname(&uts); /* Retrieve hostname - uses an output parameter */
  printf("uts.nodename in child:  %s\n", uts.nodename);
  return 0;
}
Exemplo n.º 21
0
int main(int argc, char *argv[]) {
	if(argc != 2) {
		printf("Use : %s [ hostname ]\n", argv[0]);
		return 0;
	}
	struct utsname uts;
	int pid = fork();

	if(pid == 0) {
		unshare(CLONE_NEWUTS);
		if(sethostname(argv[1], strlen(argv[1])) != 0) {
			printf("sethostname() failed\n");
			return 0;
		}
		uname(&uts);
		printf("Child hostname : %s\n", uts.nodename);
		
		return 0;
	}

	uname(&uts);
	printf("Parent hostname : %s\n", uts.nodename);

	return 0;
}
Exemplo n.º 22
0
static void
test_main(void)
{
	const char name[] = "foobarserver";
	int rv;
	mStatus status;

	rv = sethostname(&name[0], sizeof(name) - 1);
	assert(rv == 0);

	status = mDNS_Init(&mDNSStorage, &PlatformStorage, mDNS_Init_NoCache,
	    mDNS_Init_ZeroCacheSize, mDNS_Init_AdvertiseLocalAddresses,
	    mDNS_Init_NoInitCallback, mDNS_Init_NoInitCallbackContext);
	assert(status == mStatus_NoError);

	foobar_create_thread();

	while (1) {
		struct timeval timeout = { .tv_sec = 0x3fffffff, .tv_usec = 0 };
		sigset_t signals;
		mDNSBool got_something;

		mDNSPosixRunEventLoopOnce(&mDNSStorage, &timeout, &signals, &got_something);
	}
}
Exemplo n.º 23
0
int
main(int argc, char *argv[])
{
	int ch, sflag;
	char *p, hostname[MAXHOSTNAMELEN];

	sflag = 0;
	while ((ch = getopt(argc, argv, "s")) != -1)
		switch (ch) {
		case 's':
			sflag = 1;
			break;
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	if (argc > 1)
		usage();

	if (*argv) {
		if (sethostname(*argv, strlen(*argv)))
			err(1, "sethostname");
	} else {
		if (gethostname(hostname, sizeof(hostname)))
			err(1, "gethostname");
		if (sflag && (p = strchr(hostname, '.')))
			*p = '\0';
		(void)printf("%s\n", hostname);
	}
	exit(0);
}
Exemplo n.º 24
0
static int do_start(void *arg)
{
	struct start_arg *start_arg = arg;
	char **args = *start_arg->args;
	int flags = *start_arg->flags;
	uid_t uid = *start_arg->uid;
	int want_default_mounts = start_arg->want_default_mounts;
	const char *want_hostname = start_arg->want_hostname;

	if ((flags & CLONE_NEWNS) && want_default_mounts)
		lxc_setup_fs();

	if ((flags & CLONE_NEWUTS) && want_hostname)
		if (sethostname(want_hostname, strlen(want_hostname)) < 0) {
			ERROR("failed to set hostname %s: %s", want_hostname, strerror(errno));
			exit(1);
		}

	// Setuid is useful even without a new user id space
	if (start_arg->setuid && setuid(uid)) {
		ERROR("failed to set uid %d: %s", uid, strerror(errno));
		exit(1);
	}

	execvp(args[0], args);

	ERROR("failed to exec: '%s': %s", args[0], strerror(errno));
	return 1;
}
Exemplo n.º 25
0
/*
 * Actually setup our network interface with the values derived from the
 * PROM, DHCP or interactively from the user.
 */
static void
setup_interface()
{
	char		str[MAXHOSTNAMELEN];	/* will accomodate an IP too */
	size_t		len;
	struct in_addr	in_addr;

	len = sizeof (str);
	if (bootinfo_get(BI_HOST_IP, str, &len, NULL) == BI_E_SUCCESS &&
	    (in_addr.s_addr = inet_addr(str)) != (in_addr_t)-1) {
		in_addr.s_addr = htonl(in_addr.s_addr);
		ipv4_setipaddr(&in_addr);
	}

	len = sizeof (str);
	if (bootinfo_get(BI_SUBNET_MASK, str, &len, NULL) == BI_E_SUCCESS &&
	    (in_addr.s_addr = inet_addr(str)) != (in_addr_t)-1) {
		in_addr.s_addr = htonl(in_addr.s_addr);
		ipv4_setnetmask(&in_addr);
	}

	len = sizeof (str);
	if (bootinfo_get(BI_ROUTER_IP, str, &len, NULL) == BI_E_SUCCESS &&
	    (in_addr.s_addr = inet_addr(str)) != (in_addr_t)-1) {
		in_addr.s_addr = htonl(in_addr.s_addr);
		ipv4_setdefaultrouter(&in_addr);
		(void) ipv4_route(IPV4_ADD_ROUTE, RT_DEFAULT, NULL, &in_addr);
	}

	len = sizeof (str);
	if (bootinfo_get(BI_HOSTNAME, str, &len, NULL) == BI_E_SUCCESS) {
		(void) sethostname(str, len);
	}
}
Exemplo n.º 26
0
int child_main(void *args)
{
	printf("in child process \n");
	sethostname("NewNameSpace", 20);
	execv(child_args[0], child_args);

	return 1;
}
Exemplo n.º 27
0
int child_main(void* arg) {
    printf(" - World !\n");
    sethostname("In Namespace", 12);
    execv(child_args[0], child_args);
    printf("Oops\n");

    return 1;
}
Exemplo n.º 28
0
// Returns a `Try` of the result of attempting to set the `hostname`.
inline Try<Nothing> setHostname(const std::string& hostname)
{
  if (sethostname(hostname.c_str(), hostname.size()) != 0) {
    return ErrnoError();
  }

  return Nothing();
}
Exemplo n.º 29
0
/**
 * Change hostname
 */
void setup_hostname(const char *name)
{
    if (!name) return;
    if (sethostname(name, strlen(name)) == -1) {
        SYSERROR("can`t set hostname");
        throw -1;
    }
}
Exemplo n.º 30
0
ATF_TC_BODY(sethostname_limit, tc)
{
	char name[MAXHOSTNAMELEN + 1];

	(void)memset(name, 0, sizeof(name));

	ATF_REQUIRE(sethostname(name, sizeof(name)) == -1);
}