ia64/xen-unstable

changeset 16079:3d13b318349a

xenconsoled: portability fixes:
- Use openpty(), which does the same as the sequence
of open(), grantpt(), unlockpt(), ptsname(), tcgetattr()
simplifies code
- Check return code from tcsetattr()
- sprintf() -> snprintf()
- OpenBSD lacks POSIX grantpt() and unlockpt()
requires use of openpty()
- Solaris lacks POSIX openpty() via feedback from SUN (John Levon)
implement openpty() for Solaris, tested and ok'd by SUN (John
Levon)

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
author Keir Fraser <keir@xensource.com>
date Mon Oct 08 10:55:50 2007 +0100 (2007-10-08)
parents ff99e8da117f
children 685054d5fa48
files config/StdGNU.mk config/SunOS.mk tools/console/Makefile tools/console/daemon/io.c tools/console/daemon/utils.c
line diff
     1.1 --- a/config/StdGNU.mk	Mon Oct 08 10:47:58 2007 +0100
     1.2 +++ b/config/StdGNU.mk	Mon Oct 08 10:55:50 2007 +0100
     1.3 @@ -21,6 +21,7 @@ LIB64DIR = lib64
     1.4  
     1.5  SOCKET_LIBS =
     1.6  CURSES_LIBS = -lncurses
     1.7 +UTIL_LIBS = -lutil
     1.8  SONAME_LDFLAG = -soname
     1.9  SHLIB_CFLAGS = -shared
    1.10  
     2.1 --- a/config/SunOS.mk	Mon Oct 08 10:47:58 2007 +0100
     2.2 +++ b/config/SunOS.mk	Mon Oct 08 10:55:50 2007 +0100
     2.3 @@ -22,6 +22,7 @@ LIB64DIR = lib/amd64
     2.4  
     2.5  SOCKET_LIBS = -lsocket
     2.6  CURSES_LIBS = -lcurses
     2.7 +UTIL_LIBS =
     2.8  SONAME_LDFLAG = -h
     2.9  SHLIB_CFLAGS = -R /usr/sfw/$(LIBDIR) -shared
    2.10  
     3.1 --- a/tools/console/Makefile	Mon Oct 08 10:47:58 2007 +0100
     3.2 +++ b/tools/console/Makefile	Mon Oct 08 10:55:50 2007 +0100
     3.3 @@ -22,11 +22,11 @@ clean:
     3.4  
     3.5  xenconsoled: $(patsubst %.c,%.o,$(wildcard daemon/*.c))
     3.6  	$(CC) $(CFLAGS) $^ -o $@ -L$(XEN_LIBXC) -L$(XEN_XENSTORE) \
     3.7 -              $(SOCKET_LIBS) -lxenctrl -lxenstore
     3.8 +              $(UTIL_LIBS) $(SOCKET_LIBS) -lxenctrl -lxenstore
     3.9  
    3.10  xenconsole: $(patsubst %.c,%.o,$(wildcard client/*.c))
    3.11  	$(CC) $(CFLAGS) $^ -o $@ -L$(XEN_LIBXC) -L$(XEN_XENSTORE) \
    3.12 -	      $(SOCKET_LIBS) -lxenctrl -lxenstore
    3.13 +	      $(UTIL_LIBS) $(SOCKET_LIBS) -lxenctrl -lxenstore
    3.14  
    3.15  .PHONY: install
    3.16  install: $(BIN)
     4.1 --- a/tools/console/daemon/io.c	Mon Oct 08 10:47:58 2007 +0100
     4.2 +++ b/tools/console/daemon/io.c	Mon Oct 08 10:55:50 2007 +0100
     4.3 @@ -26,7 +26,6 @@
     4.4  #include <xen/io/console.h>
     4.5  #include <xenctrl.h>
     4.6  
     4.7 -#include <malloc.h>
     4.8  #include <stdlib.h>
     4.9  #include <errno.h>
    4.10  #include <string.h>
    4.11 @@ -36,6 +35,11 @@
    4.12  #include <termios.h>
    4.13  #include <stdarg.h>
    4.14  #include <sys/mman.h>
    4.15 +#if defined(__NetBSD__) || defined(__OpenBSD__)
    4.16 +#include <util.h>
    4.17 +#elif defined(__linux__) || defined(__Linux__)
    4.18 +#include <pty.h>
    4.19 +#endif
    4.20  
    4.21  #define MAX(a, b) (((a) > (b)) ? (a) : (b))
    4.22  #define MIN(a, b) (((a) < (b)) ? (a) : (b))
    4.23 @@ -213,43 +217,81 @@ static int create_domain_log(struct doma
    4.24  	return fd;
    4.25  }
    4.26  
    4.27 +#ifdef __sun__
    4.28 +/* Once Solaris has openpty(), this is going to be removed. */
    4.29 +int openpty(int *amaster, int *aslave, char *name,
    4.30 +	    struct termios *termp, struct winsize *winp)
    4.31 +{
    4.32 +	int mfd, sfd;
    4.33 +
    4.34 +	*amaster = *aslave = -1;
    4.35 +	mfd = sfd = -1;
    4.36 +
    4.37 +	mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
    4.38 +	if (mfd < 0)
    4.39 +		goto err0;
    4.40 +
    4.41 +	if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
    4.42 +		goto err1;
    4.43 +
    4.44 +	/* This does not match openpty specification,
    4.45 +	 * but as long as this does not hurt, this is acceptable.
    4.46 +	 */
    4.47 +	mfd = sfd;
    4.48 +
    4.49 +	if (termp != NULL && tcgetattr(sfd, termp) < 0)
    4.50 +		goto err1;	
    4.51 +
    4.52 +	if (amaster)
    4.53 +		*amaster = mfd;
    4.54 +	if (aslave)
    4.55 +		*aslave = sfd;
    4.56 +	if (name)
    4.57 +		strlcpy(name, ptsname(mfd), sizeof(slave));
    4.58 +	if (winp)
    4.59 +		ioctl(sfd, TIOCSWINSZ, winp);
    4.60 +
    4.61 +	return 0;
    4.62 +
    4.63 +err1:
    4.64 +	close(mfd);
    4.65 +err0:
    4.66 +	return -1;
    4.67 +}
    4.68 +#endif
    4.69 +
    4.70  
    4.71  static int domain_create_tty(struct domain *dom)
    4.72  {
    4.73 +	char slave[80];
    4.74 +	struct termios term;
    4.75  	char *path;
    4.76 -	int master;
    4.77 +	int master, slavefd;
    4.78 +	int err;
    4.79  	bool success;
    4.80 -
    4.81 -	if ((master = open("/dev/ptmx",O_RDWR|O_NOCTTY)) == -1 ||
    4.82 -	    grantpt(master) == -1 || unlockpt(master) == -1) {
    4.83 -		dolog(LOG_ERR, "Failed to create tty for domain-%d",
    4.84 -		      dom->domid);
    4.85 -		master = -1;
    4.86 -	} else {
    4.87 -		const char *slave = ptsname(master);
    4.88 -		struct termios term;
    4.89 -		char *data;
    4.90 -		unsigned int len;
    4.91 +	char *data;
    4.92 +	unsigned int len;
    4.93  
    4.94 -		if (tcgetattr(master, &term) != -1) {
    4.95 -			cfmakeraw(&term);
    4.96 -			tcsetattr(master, TCSAFLUSH, &term);
    4.97 -		}
    4.98 +	if (openpty(&master, &slavefd, slave, &term, NULL) < 0) {
    4.99 +		master = -1;
   4.100 +		err = errno;
   4.101 +		dolog(LOG_ERR, "Failed to create tty for domain-%d (errno = %i, %s)",
   4.102 +		      dom->domid, err, strerror(err));
   4.103 +		return master;
   4.104 +	}
   4.105  
   4.106 -		if (dom->use_consolepath) {
   4.107 -			success = asprintf(&path, "%s/limit", dom->conspath) !=
   4.108 -				-1;
   4.109 -			if (!success)
   4.110 -				goto out;
   4.111 -			data = xs_read(xs, XBT_NULL, path, &len);
   4.112 -			if (data) {
   4.113 -				dom->buffer.max_capacity = strtoul(data, 0, 0);
   4.114 -				free(data);
   4.115 -			}
   4.116 -			free(path);
   4.117 -		}
   4.118 +	cfmakeraw(&term);
   4.119 +	if (tcsetattr(master, TCSAFLUSH, &term) < 0) {
   4.120 +		err = errno;
   4.121 +		dolog(LOG_ERR, "Failed to set tty attribute  for domain-%d (errno = %i, %s)",
   4.122 +		      dom->domid, err, strerror(err));
   4.123 +		goto out;
   4.124 +	}
   4.125  
   4.126 -		success = asprintf(&path, "%s/limit", dom->serialpath) != -1;
   4.127 +
   4.128 +	if (dom->use_consolepath) {
   4.129 +		success = asprintf(&path, "%s/limit", dom->conspath) !=
   4.130 +			-1;
   4.131  		if (!success)
   4.132  			goto out;
   4.133  		data = xs_read(xs, XBT_NULL, path, &len);
   4.134 @@ -258,30 +300,38 @@ static int domain_create_tty(struct doma
   4.135  			free(data);
   4.136  		}
   4.137  		free(path);
   4.138 +	}
   4.139  
   4.140 -		success = asprintf(&path, "%s/tty", dom->serialpath) != -1;
   4.141 +	success = asprintf(&path, "%s/limit", dom->serialpath) != -1;
   4.142 +	if (!success)
   4.143 +		goto out;
   4.144 +	data = xs_read(xs, XBT_NULL, path, &len);
   4.145 +	if (data) {
   4.146 +		dom->buffer.max_capacity = strtoul(data, 0, 0);
   4.147 +		free(data);
   4.148 +	}
   4.149 +	free(path);
   4.150 +
   4.151 +	success = asprintf(&path, "%s/tty", dom->serialpath) != -1;
   4.152 +	if (!success)
   4.153 +		goto out;
   4.154 +	success = xs_write(xs, XBT_NULL, path, slave, strlen(slave));
   4.155 +	free(path);
   4.156 +	if (!success)
   4.157 +		goto out;
   4.158 +
   4.159 +	if (dom->use_consolepath) {
   4.160 +		success = (asprintf(&path, "%s/tty", dom->conspath) != -1);
   4.161  		if (!success)
   4.162  			goto out;
   4.163  		success = xs_write(xs, XBT_NULL, path, slave, strlen(slave));
   4.164  		free(path);
   4.165  		if (!success)
   4.166  			goto out;
   4.167 +	}
   4.168  
   4.169 -		if (dom->use_consolepath) {
   4.170 -			success = asprintf(&path, "%s/tty", dom->conspath) !=
   4.171 -				-1;
   4.172 -			if (!success)
   4.173 -				goto out;
   4.174 -			success = xs_write(xs, XBT_NULL, path, slave,
   4.175 -					   strlen(slave));
   4.176 -			free(path);
   4.177 -			if (!success)
   4.178 -				goto out;
   4.179 -		}
   4.180 -
   4.181 -		if (fcntl(master, F_SETFL, O_NONBLOCK) == -1)
   4.182 -			goto out;
   4.183 -	}
   4.184 +	if (fcntl(master, F_SETFL, O_NONBLOCK) == -1)
   4.185 +		goto out;
   4.186  
   4.187  	return master;
   4.188   out:
   4.189 @@ -410,7 +460,7 @@ static bool watch_domain(struct domain *
   4.190  	char domid_str[3 + MAX_STRLEN(dom->domid)];
   4.191  	bool success;
   4.192  
   4.193 -	sprintf(domid_str, "dom%u", dom->domid);
   4.194 +	snprintf(domid_str, sizeof(domid_str), "dom%u", dom->domid);
   4.195  	if (watch) {
   4.196  		success = xs_watch(xs, dom->serialpath, domid_str);
   4.197  		if (success) {
     5.1 --- a/tools/console/daemon/utils.c	Mon Oct 08 10:47:58 2007 +0100
     5.2 +++ b/tools/console/daemon/utils.c	Mon Oct 08 10:55:50 2007 +0100
     5.3 @@ -96,7 +96,7 @@ void daemonize(const char *pidfile)
     5.4  		exit(1);
     5.5  	}
     5.6  
     5.7 -	len = sprintf(buf, "%ld\n", (long)getpid());
     5.8 +	len = snprintf(buf, sizeof(buf), "%ld\n", (long)getpid());
     5.9  	if (write(fd, buf, len) < 0)
    5.10  		exit(1);
    5.11