Remove kernel memory allocation code from ipf.c
[qemu-kvm/fedora.git] / slirp / misc.c
blob032a1f7b1f74958ff87d08f05159318148c82371
1 /*
2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
6 */
8 #define WANT_SYS_IOCTL_H
9 #include <slirp.h>
11 u_int curtime, time_fasttimo, last_slowtimo;
13 #if 0
14 int x_port = -1;
15 int x_display = 0;
16 int x_screen = 0;
18 int
19 show_x(buff, inso)
20 char *buff;
21 struct socket *inso;
23 if (x_port < 0) {
24 lprint("X Redir: X not being redirected.\r\n");
25 } else {
26 lprint("X Redir: In sh/bash/zsh/etc. type: DISPLAY=%s:%d.%d; export DISPLAY\r\n",
27 inet_ntoa(our_addr), x_port, x_screen);
28 lprint("X Redir: In csh/tcsh/etc. type: setenv DISPLAY %s:%d.%d\r\n",
29 inet_ntoa(our_addr), x_port, x_screen);
30 if (x_display)
31 lprint("X Redir: Redirecting to display %d\r\n", x_display);
34 return CFG_OK;
39 * XXX Allow more than one X redirection?
41 void
42 redir_x(inaddr, start_port, display, screen)
43 u_int32_t inaddr;
44 int start_port;
45 int display;
46 int screen;
48 int i;
50 if (x_port >= 0) {
51 lprint("X Redir: X already being redirected.\r\n");
52 show_x(0, 0);
53 } else {
54 for (i = 6001 + (start_port-1); i <= 6100; i++) {
55 if (solisten(htons(i), inaddr, htons(6000 + display), 0)) {
56 /* Success */
57 x_port = i - 6000;
58 x_display = display;
59 x_screen = screen;
60 show_x(0, 0);
61 return;
64 lprint("X Redir: Error: Couldn't redirect a port for X. Weird.\r\n");
67 #endif
69 #ifndef HAVE_INET_ATON
70 int
71 inet_aton(cp, ia)
72 const char *cp;
73 struct in_addr *ia;
75 u_int32_t addr = inet_addr(cp);
76 if (addr == 0xffffffff)
77 return 0;
78 ia->s_addr = addr;
79 return 1;
81 #endif
84 * Get our IP address and put it in our_addr
86 void
87 getouraddr()
89 char buff[256];
90 struct hostent *he = NULL;
92 if (gethostname(buff,256) == 0)
93 he = gethostbyname(buff);
94 if (he)
95 our_addr = *(struct in_addr *)he->h_addr;
96 if (our_addr.s_addr == 0)
97 our_addr.s_addr = loopback_addr.s_addr;
100 #if SIZEOF_CHAR_P == 8
102 struct quehead_32 {
103 u_int32_t qh_link;
104 u_int32_t qh_rlink;
107 inline void
108 insque_32(a, b)
109 void *a;
110 void *b;
112 register struct quehead_32 *element = (struct quehead_32 *) a;
113 register struct quehead_32 *head = (struct quehead_32 *) b;
114 element->qh_link = head->qh_link;
115 head->qh_link = (u_int32_t)element;
116 element->qh_rlink = (u_int32_t)head;
117 ((struct quehead_32 *)(element->qh_link))->qh_rlink
118 = (u_int32_t)element;
121 inline void
122 remque_32(a)
123 void *a;
125 register struct quehead_32 *element = (struct quehead_32 *) a;
126 ((struct quehead_32 *)(element->qh_link))->qh_rlink = element->qh_rlink;
127 ((struct quehead_32 *)(element->qh_rlink))->qh_link = element->qh_link;
128 element->qh_rlink = 0;
131 #endif /* SIZEOF_CHAR_P == 8 */
133 struct quehead {
134 struct quehead *qh_link;
135 struct quehead *qh_rlink;
138 inline void
139 insque(a, b)
140 void *a, *b;
142 register struct quehead *element = (struct quehead *) a;
143 register struct quehead *head = (struct quehead *) b;
144 element->qh_link = head->qh_link;
145 head->qh_link = (struct quehead *)element;
146 element->qh_rlink = (struct quehead *)head;
147 ((struct quehead *)(element->qh_link))->qh_rlink
148 = (struct quehead *)element;
151 inline void
152 remque(a)
153 void *a;
155 register struct quehead *element = (struct quehead *) a;
156 ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
157 ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
158 element->qh_rlink = NULL;
159 /* element->qh_link = NULL; TCP FIN1 crashes if you do this. Why ? */
162 /* #endif */
166 add_exec(ex_ptr, do_pty, exec, addr, port)
167 struct ex_list **ex_ptr;
168 int do_pty;
169 char *exec;
170 int addr;
171 int port;
173 struct ex_list *tmp_ptr;
175 /* First, check if the port is "bound" */
176 for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
177 if (port == tmp_ptr->ex_fport && addr == tmp_ptr->ex_addr)
178 return -1;
181 tmp_ptr = *ex_ptr;
182 *ex_ptr = (struct ex_list *)malloc(sizeof(struct ex_list));
183 (*ex_ptr)->ex_fport = port;
184 (*ex_ptr)->ex_addr = addr;
185 (*ex_ptr)->ex_pty = do_pty;
186 (*ex_ptr)->ex_exec = strdup(exec);
187 (*ex_ptr)->ex_next = tmp_ptr;
188 return 0;
191 #ifndef HAVE_STRERROR
194 * For systems with no strerror
197 extern int sys_nerr;
198 extern char *sys_errlist[];
200 char *
201 strerror(error)
202 int error;
204 if (error < sys_nerr)
205 return sys_errlist[error];
206 else
207 return "Unknown error.";
210 #endif
213 #ifdef _WIN32
216 fork_exec(struct socket *so, const char *ex, int do_pty)
218 /* not implemented */
219 return 0;
222 #else
224 #ifndef CONFIG_QEMU
226 slirp_openpty(amaster, aslave)
227 int *amaster, *aslave;
229 register int master, slave;
231 #ifdef HAVE_GRANTPT
232 char *ptr;
234 if ((master = open("/dev/ptmx", O_RDWR)) < 0 ||
235 grantpt(master) < 0 ||
236 unlockpt(master) < 0 ||
237 (ptr = ptsname(master)) == NULL) {
238 close(master);
239 return -1;
242 if ((slave = open(ptr, O_RDWR)) < 0 ||
243 ioctl(slave, I_PUSH, "ptem") < 0 ||
244 ioctl(slave, I_PUSH, "ldterm") < 0 ||
245 ioctl(slave, I_PUSH, "ttcompat") < 0) {
246 close(master);
247 close(slave);
248 return -1;
251 *amaster = master;
252 *aslave = slave;
253 return 0;
255 #else
257 static char line[] = "/dev/ptyXX";
258 register const char *cp1, *cp2;
260 for (cp1 = "pqrsPQRS"; *cp1; cp1++) {
261 line[8] = *cp1;
262 for (cp2 = "0123456789abcdefghijklmnopqrstuv"; *cp2; cp2++) {
263 line[9] = *cp2;
264 if ((master = open(line, O_RDWR, 0)) == -1) {
265 if (errno == ENOENT)
266 return (-1); /* out of ptys */
267 } else {
268 line[5] = 't';
269 /* These will fail */
270 (void) chown(line, getuid(), 0);
271 (void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
272 #ifdef HAVE_REVOKE
273 (void) revoke(line);
274 #endif
275 if ((slave = open(line, O_RDWR, 0)) != -1) {
276 *amaster = master;
277 *aslave = slave;
278 return 0;
280 (void) close(master);
281 line[5] = 'p';
285 errno = ENOENT; /* out of ptys */
286 return (-1);
287 #endif
289 #endif
292 * XXX This is ugly
293 * We create and bind a socket, then fork off to another
294 * process, which connects to this socket, after which we
295 * exec the wanted program. If something (strange) happens,
296 * the accept() call could block us forever.
298 * do_pty = 0 Fork/exec inetd style
299 * do_pty = 1 Fork/exec using slirp.telnetd
300 * do_ptr = 2 Fork/exec using pty
303 fork_exec(struct socket *so, const char *ex, int do_pty)
305 int s;
306 struct sockaddr_in addr;
307 socklen_t addrlen = sizeof(addr);
308 int opt;
309 int master = -1;
310 char *argv[256];
311 #if 0
312 char buff[256];
313 #endif
314 /* don't want to clobber the original */
315 char *bptr;
316 const char *curarg;
317 int c, i, ret;
319 DEBUG_CALL("fork_exec");
320 DEBUG_ARG("so = %lx", (long)so);
321 DEBUG_ARG("ex = %lx", (long)ex);
322 DEBUG_ARG("do_pty = %lx", (long)do_pty);
324 if (do_pty == 2) {
325 #if 0
326 if (slirp_openpty(&master, &s) == -1) {
327 lprint("Error: openpty failed: %s\n", strerror(errno));
328 return 0;
330 #else
331 return 0;
332 #endif
333 } else {
334 addr.sin_family = AF_INET;
335 addr.sin_port = 0;
336 addr.sin_addr.s_addr = INADDR_ANY;
338 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0 ||
339 bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
340 listen(s, 1) < 0) {
341 lprint("Error: inet socket: %s\n", strerror(errno));
342 closesocket(s);
344 return 0;
348 switch(fork()) {
349 case -1:
350 lprint("Error: fork failed: %s\n", strerror(errno));
351 close(s);
352 if (do_pty == 2)
353 close(master);
354 return 0;
356 case 0:
357 /* Set the DISPLAY */
358 if (do_pty == 2) {
359 (void) close(master);
360 #ifdef TIOCSCTTY /* XXXXX */
361 (void) setsid();
362 ioctl(s, TIOCSCTTY, (char *)NULL);
363 #endif
364 } else {
365 getsockname(s, (struct sockaddr *)&addr, &addrlen);
366 close(s);
368 * Connect to the socket
369 * XXX If any of these fail, we're in trouble!
371 s = socket(AF_INET, SOCK_STREAM, 0);
372 addr.sin_addr = loopback_addr;
373 do {
374 ret = connect(s, (struct sockaddr *)&addr, addrlen);
375 } while (ret < 0 && errno == EINTR);
378 #if 0
379 if (x_port >= 0) {
380 #ifdef HAVE_SETENV
381 sprintf(buff, "%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
382 setenv("DISPLAY", buff, 1);
383 #else
384 sprintf(buff, "DISPLAY=%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
385 putenv(buff);
386 #endif
388 #endif
389 dup2(s, 0);
390 dup2(s, 1);
391 dup2(s, 2);
392 for (s = getdtablesize() - 1; s >= 3; s--)
393 close(s);
395 i = 0;
396 bptr = strdup(ex); /* No need to free() this */
397 if (do_pty == 1) {
398 /* Setup "slirp.telnetd -x" */
399 argv[i++] = "slirp.telnetd";
400 argv[i++] = "-x";
401 argv[i++] = bptr;
402 } else
403 do {
404 /* Change the string into argv[] */
405 curarg = bptr;
406 while (*bptr != ' ' && *bptr != (char)0)
407 bptr++;
408 c = *bptr;
409 *bptr++ = (char)0;
410 argv[i++] = strdup(curarg);
411 } while (c);
413 argv[i] = 0;
414 execvp(argv[0], argv);
416 /* Ooops, failed, let's tell the user why */
418 char buff[256];
420 snprintf(buff, sizeof(buff),
421 "Error: execvp of %s failed: %s\n",
422 argv[0], strerror(errno));
423 write(2, buff, strlen(buff)+1);
425 close(0); close(1); close(2); /* XXX */
426 exit(1);
428 default:
429 if (do_pty == 2) {
430 close(s);
431 so->s = master;
432 } else {
434 * XXX this could block us...
435 * XXX Should set a timer here, and if accept() doesn't
436 * return after X seconds, declare it a failure
437 * The only reason this will block forever is if socket()
438 * of connect() fail in the child process
440 do {
441 so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
442 } while (so->s < 0 && errno == EINTR);
443 closesocket(s);
444 opt = 1;
445 setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
446 opt = 1;
447 setsockopt(so->s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
449 fd_nonblock(so->s);
451 /* Append the telnet options now */
452 if (so->so_m != 0 && do_pty == 1) {
453 sbappend(so, so->so_m);
454 so->so_m = 0;
457 return 1;
460 #endif
462 #ifndef HAVE_STRDUP
463 char *
464 strdup(str)
465 const char *str;
467 char *bptr;
469 bptr = (char *)malloc(strlen(str)+1);
470 strcpy(bptr, str);
472 return bptr;
474 #endif
476 #if 0
477 void
478 snooze_hup(num)
479 int num;
481 int s, ret;
482 #ifndef NO_UNIX_SOCKETS
483 struct sockaddr_un sock_un;
484 #endif
485 struct sockaddr_in sock_in;
486 char buff[256];
488 ret = -1;
489 if (slirp_socket_passwd) {
490 s = socket(AF_INET, SOCK_STREAM, 0);
491 if (s < 0)
492 slirp_exit(1);
493 sock_in.sin_family = AF_INET;
494 sock_in.sin_addr.s_addr = slirp_socket_addr;
495 sock_in.sin_port = htons(slirp_socket_port);
496 if (connect(s, (struct sockaddr *)&sock_in, sizeof(sock_in)) != 0)
497 slirp_exit(1); /* just exit...*/
498 sprintf(buff, "kill %s:%d", slirp_socket_passwd, slirp_socket_unit);
499 write(s, buff, strlen(buff)+1);
501 #ifndef NO_UNIX_SOCKETS
502 else {
503 s = socket(AF_UNIX, SOCK_STREAM, 0);
504 if (s < 0)
505 slirp_exit(1);
506 sock_un.sun_family = AF_UNIX;
507 strcpy(sock_un.sun_path, socket_path);
508 if (connect(s, (struct sockaddr *)&sock_un,
509 sizeof(sock_un.sun_family) + sizeof(sock_un.sun_path)) != 0)
510 slirp_exit(1);
511 sprintf(buff, "kill none:%d", slirp_socket_unit);
512 write(s, buff, strlen(buff)+1);
514 #endif
515 slirp_exit(0);
519 void
520 snooze()
522 sigset_t s;
523 int i;
525 /* Don't need our data anymore */
526 /* XXX This makes SunOS barf */
527 /* brk(0); */
529 /* Close all fd's */
530 for (i = 255; i >= 0; i--)
531 close(i);
533 signal(SIGQUIT, slirp_exit);
534 signal(SIGHUP, snooze_hup);
535 sigemptyset(&s);
537 /* Wait for any signal */
538 sigsuspend(&s);
540 /* Just in case ... */
541 exit(255);
544 void
545 relay(s)
546 int s;
548 char buf[8192];
549 int n;
550 fd_set readfds;
551 struct ttys *ttyp;
553 /* Don't need our data anymore */
554 /* XXX This makes SunOS barf */
555 /* brk(0); */
557 signal(SIGQUIT, slirp_exit);
558 signal(SIGHUP, slirp_exit);
559 signal(SIGINT, slirp_exit);
560 signal(SIGTERM, slirp_exit);
562 /* Fudge to get term_raw and term_restore to work */
563 if (NULL == (ttyp = tty_attach (0, slirp_tty))) {
564 lprint ("Error: tty_attach failed in misc.c:relay()\r\n");
565 slirp_exit (1);
567 ttyp->fd = 0;
568 ttyp->flags |= TTY_CTTY;
569 term_raw(ttyp);
571 while (1) {
572 FD_ZERO(&readfds);
574 FD_SET(0, &readfds);
575 FD_SET(s, &readfds);
577 n = select(s+1, &readfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0);
579 if (n <= 0)
580 slirp_exit(0);
582 if (FD_ISSET(0, &readfds)) {
583 n = read(0, buf, 8192);
584 if (n <= 0)
585 slirp_exit(0);
586 n = writen(s, buf, n);
587 if (n <= 0)
588 slirp_exit(0);
591 if (FD_ISSET(s, &readfds)) {
592 n = read(s, buf, 8192);
593 if (n <= 0)
594 slirp_exit(0);
595 n = writen(0, buf, n);
596 if (n <= 0)
597 slirp_exit(0);
601 /* Just in case.... */
602 exit(1);
604 #endif
606 #ifdef CONFIG_QEMU
607 extern void term_vprintf(const char *fmt, va_list ap);
609 void lprint(const char *format, ...)
611 va_list args;
613 va_start(args, format);
614 term_vprintf(format, args);
615 va_end(args);
617 #else
618 int (*lprint_print) _P((void *, const char *, va_list));
619 char *lprint_ptr, *lprint_ptr2, **lprint_arg;
621 void
622 #ifdef __STDC__
623 lprint(const char *format, ...)
624 #else
625 lprint(va_alist) va_dcl
626 #endif
628 va_list args;
630 #ifdef __STDC__
631 va_start(args, format);
632 #else
633 char *format;
634 va_start(args);
635 format = va_arg(args, char *);
636 #endif
637 #if 0
638 /* If we're printing to an sbuf, make sure there's enough room */
639 /* XXX +100? */
640 if (lprint_sb) {
641 if ((lprint_ptr - lprint_sb->sb_wptr) >=
642 (lprint_sb->sb_datalen - (strlen(format) + 100))) {
643 int deltaw = lprint_sb->sb_wptr - lprint_sb->sb_data;
644 int deltar = lprint_sb->sb_rptr - lprint_sb->sb_data;
645 int deltap = lprint_ptr - lprint_sb->sb_data;
647 lprint_sb->sb_data = (char *)realloc(lprint_sb->sb_data,
648 lprint_sb->sb_datalen + TCP_SNDSPACE);
650 /* Adjust all values */
651 lprint_sb->sb_wptr = lprint_sb->sb_data + deltaw;
652 lprint_sb->sb_rptr = lprint_sb->sb_data + deltar;
653 lprint_ptr = lprint_sb->sb_data + deltap;
655 lprint_sb->sb_datalen += TCP_SNDSPACE;
658 #endif
659 if (lprint_print)
660 lprint_ptr += (*lprint_print)(*lprint_arg, format, args);
662 /* Check if they want output to be logged to file as well */
663 if (lfd) {
665 * Remove \r's
666 * otherwise you'll get ^M all over the file
668 int len = strlen(format);
669 char *bptr1, *bptr2;
671 bptr1 = bptr2 = strdup(format);
673 while (len--) {
674 if (*bptr1 == '\r')
675 memcpy(bptr1, bptr1+1, len+1);
676 else
677 bptr1++;
679 vfprintf(lfd, bptr2, args);
680 free(bptr2);
682 va_end(args);
685 void
686 add_emu(buff)
687 char *buff;
689 u_int lport, fport;
690 u_int8_t tos = 0, emu = 0;
691 char buff1[256], buff2[256], buff4[128];
692 char *buff3 = buff4;
693 struct emu_t *emup;
694 struct socket *so;
696 if (sscanf(buff, "%256s %256s", buff2, buff1) != 2) {
697 lprint("Error: Bad arguments\r\n");
698 return;
701 if (sscanf(buff1, "%d:%d", &lport, &fport) != 2) {
702 lport = 0;
703 if (sscanf(buff1, "%d", &fport) != 1) {
704 lprint("Error: Bad first argument\r\n");
705 return;
709 if (sscanf(buff2, "%128[^:]:%128s", buff1, buff3) != 2) {
710 buff3 = 0;
711 if (sscanf(buff2, "%256s", buff1) != 1) {
712 lprint("Error: Bad second argument\r\n");
713 return;
717 if (buff3) {
718 if (strcmp(buff3, "lowdelay") == 0)
719 tos = IPTOS_LOWDELAY;
720 else if (strcmp(buff3, "throughput") == 0)
721 tos = IPTOS_THROUGHPUT;
722 else {
723 lprint("Error: Expecting \"lowdelay\"/\"throughput\"\r\n");
724 return;
728 if (strcmp(buff1, "ftp") == 0)
729 emu = EMU_FTP;
730 else if (strcmp(buff1, "irc") == 0)
731 emu = EMU_IRC;
732 else if (strcmp(buff1, "none") == 0)
733 emu = EMU_NONE; /* ie: no emulation */
734 else {
735 lprint("Error: Unknown service\r\n");
736 return;
739 /* First, check that it isn't already emulated */
740 for (emup = tcpemu; emup; emup = emup->next) {
741 if (emup->lport == lport && emup->fport == fport) {
742 lprint("Error: port already emulated\r\n");
743 return;
747 /* link it */
748 emup = (struct emu_t *)malloc(sizeof (struct emu_t));
749 emup->lport = (u_int16_t)lport;
750 emup->fport = (u_int16_t)fport;
751 emup->tos = tos;
752 emup->emu = emu;
753 emup->next = tcpemu;
754 tcpemu = emup;
756 /* And finally, mark all current sessions, if any, as being emulated */
757 for (so = tcb.so_next; so != &tcb; so = so->so_next) {
758 if ((lport && lport == ntohs(so->so_lport)) ||
759 (fport && fport == ntohs(so->so_fport))) {
760 if (emu)
761 so->so_emu = emu;
762 if (tos)
763 so->so_iptos = tos;
767 lprint("Adding emulation for %s to port %d/%d\r\n", buff1, emup->lport, emup->fport);
769 #endif
771 #ifdef BAD_SPRINTF
773 #undef vsprintf
774 #undef sprintf
777 * Some BSD-derived systems have a sprintf which returns char *
781 vsprintf_len(string, format, args)
782 char *string;
783 const char *format;
784 va_list args;
786 vsprintf(string, format, args);
787 return strlen(string);
791 #ifdef __STDC__
792 sprintf_len(char *string, const char *format, ...)
793 #else
794 sprintf_len(va_alist) va_dcl
795 #endif
797 va_list args;
798 #ifdef __STDC__
799 va_start(args, format);
800 #else
801 char *string;
802 char *format;
803 va_start(args);
804 string = va_arg(args, char *);
805 format = va_arg(args, char *);
806 #endif
807 vsprintf(string, format, args);
808 return strlen(string);
811 #endif
813 void
814 u_sleep(usec)
815 int usec;
817 struct timeval t;
818 fd_set fdset;
820 FD_ZERO(&fdset);
822 t.tv_sec = 0;
823 t.tv_usec = usec * 1000;
825 select(0, &fdset, &fdset, &fdset, &t);
829 * Set fd blocking and non-blocking
832 void
833 fd_nonblock(fd)
834 int fd;
836 #ifdef FIONBIO
837 int opt = 1;
839 ioctlsocket(fd, FIONBIO, &opt);
840 #else
841 int opt;
843 opt = fcntl(fd, F_GETFL, 0);
844 opt |= O_NONBLOCK;
845 fcntl(fd, F_SETFL, opt);
846 #endif
849 void
850 fd_block(fd)
851 int fd;
853 #ifdef FIONBIO
854 int opt = 0;
856 ioctlsocket(fd, FIONBIO, &opt);
857 #else
858 int opt;
860 opt = fcntl(fd, F_GETFL, 0);
861 opt &= ~O_NONBLOCK;
862 fcntl(fd, F_SETFL, opt);
863 #endif
867 #if 0
869 * invoke RSH
872 rsh_exec(so,ns, user, host, args)
873 struct socket *so;
874 struct socket *ns;
875 char *user;
876 char *host;
877 char *args;
879 int fd[2];
880 int fd0[2];
881 int s;
882 char buff[256];
884 DEBUG_CALL("rsh_exec");
885 DEBUG_ARG("so = %lx", (long)so);
887 if (pipe(fd)<0) {
888 lprint("Error: pipe failed: %s\n", strerror(errno));
889 return 0;
891 /* #ifdef HAVE_SOCKETPAIR */
892 #if 1
893 if (socketpair(PF_UNIX,SOCK_STREAM,0, fd0) == -1) {
894 close(fd[0]);
895 close(fd[1]);
896 lprint("Error: openpty failed: %s\n", strerror(errno));
897 return 0;
899 #else
900 if (slirp_openpty(&fd0[0], &fd0[1]) == -1) {
901 close(fd[0]);
902 close(fd[1]);
903 lprint("Error: openpty failed: %s\n", strerror(errno));
904 return 0;
906 #endif
908 switch(fork()) {
909 case -1:
910 lprint("Error: fork failed: %s\n", strerror(errno));
911 close(fd[0]);
912 close(fd[1]);
913 close(fd0[0]);
914 close(fd0[1]);
915 return 0;
917 case 0:
918 close(fd[0]);
919 close(fd0[0]);
921 /* Set the DISPLAY */
922 if (x_port >= 0) {
923 #ifdef HAVE_SETENV
924 sprintf(buff, "%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
925 setenv("DISPLAY", buff, 1);
926 #else
927 sprintf(buff, "DISPLAY=%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
928 putenv(buff);
929 #endif
932 dup2(fd0[1], 0);
933 dup2(fd0[1], 1);
934 dup2(fd[1], 2);
935 for (s = 3; s <= 255; s++)
936 close(s);
938 execlp("rsh","rsh","-l", user, host, args, NULL);
940 /* Ooops, failed, let's tell the user why */
942 sprintf(buff, "Error: execlp of %s failed: %s\n",
943 "rsh", strerror(errno));
944 write(2, buff, strlen(buff)+1);
945 close(0); close(1); close(2); /* XXX */
946 exit(1);
948 default:
949 close(fd[1]);
950 close(fd0[1]);
951 ns->s=fd[0];
952 so->s=fd0[0];
954 return 1;
957 #endif