当前位置: 首页>>代码示例>>C++>>正文


C++ sethostname函数代码示例

本文整理汇总了C++中sethostname函数的典型用法代码示例。如果您正苦于以下问题:C++ sethostname函数的具体用法?C++ sethostname怎么用?C++ sethostname使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了sethostname函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: do_sethostname

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;
			}    
	}
}
开发者ID:carriercomm,项目名称:myboxfs,代码行数:33,代码来源:hostname.c

示例2: ATF_TC_BODY

ATF_TC_BODY(sethostname_perm, tc)
{

	errno = 0;

	ATF_REQUIRE_ERRNO(EPERM, sethostname(host, sizeof(host)) == -1);
}
开发者ID:Hooman3,项目名称:minix,代码行数:7,代码来源:t_sethostname.c

示例3: child_main

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;
}
开发者ID:ya790206,项目名称:example_code,代码行数:31,代码来源:filesystem_namespace.c

示例4: hyper_start_containers

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;
}
开发者ID:jefby,项目名称:hyperstart,代码行数:25,代码来源:container.c

示例5: main

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;
}
开发者ID:alkedr,项目名称:simple-linux-containers,代码行数:35,代码来源:test_uts_namespace.c

示例6: SetHostName

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();
}
开发者ID:direvius,项目名称:porto,代码行数:7,代码来源:unix.cpp

示例7: child_main

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;
}
开发者ID:gwtony,项目名称:c-example,代码行数:26,代码来源:net_namespace.c

示例8: context_update_kernel_hostname

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;
}
开发者ID:klausi,项目名称:systemd,代码行数:30,代码来源:hostnamed.c

示例9: loopback_set_hostname

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;
}
开发者ID:connectivity,项目名称:connman-stable,代码行数:26,代码来源:loopback.c

示例10: command_hostname

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);
}
开发者ID:bproctor,项目名称:utils,代码行数:30,代码来源:hostname.c

示例11: cleanup

/*
 * 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();
}
开发者ID:CSU-GH,项目名称:okl4_3.0,代码行数:30,代码来源:sethostname03.c

示例12: uwsgi_hook_hostname

static int uwsgi_hook_hostname(char *arg) {
#ifdef __CYGWIN__
	return -1;
#else
	return sethostname(arg, strlen(arg));
#endif
}
开发者ID:austinglaser,项目名称:csci3308-project,代码行数:7,代码来源:hooks.c

示例13: container_main

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;
}
开发者ID:defrur,项目名称:coding-journal,代码行数:7,代码来源:uts.c

示例14: nm_policy_set_system_hostname

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);
}
开发者ID:alfmatos,项目名称:NetworkManager,代码行数:34,代码来源:nm-policy-hostname.c

示例15: TEST

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));
}
开发者ID:0xDEC0DE8,项目名称:platform_bionic,代码行数:7,代码来源:unistd_test.cpp


注:本文中的sethostname函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。