示例#1
0
int
main(int argc, char *argv[])
{
	int 	i, fd;

	for (i = 1; i < argc; i++)
	{
		if ((fd = open(argv[i], O_RDONLY)) < 0)
		{
			err_ret("%s: can't open", argv[i]);
			continue;
		}

		if (isastream(fd) == 0)
		{
			err_ret("%s: not a stream", argv[i]);
		}
		else
		{
			err_msg("%s: stream device",argv[i]);
		}
	}
	exit(0);

}
示例#2
0
文件: strlist.c 项目: yogmk/prog
int
main(int argc, char *argv[])
{
	int					fd, i, nmods;
	struct str_list		list;

	if (argc != 2)
		err_quit("usage: a.out <pathname>");

	if ( (fd = open(argv[1], O_RDONLY)) < 0)
		err_sys("can't open %s", argv[1]);
	if (isastream(fd) == 0)
		err_quit("%s is not a stream", argv[1]);

			/* fetch number of modules */
	if ( (nmods = ioctl(fd, I_LIST, (void *) 0)) < 0)
		err_sys("I_LIST error for nmods");
	printf("#modules = %d\n", nmods);

			/* allocate storage for all the module names */
	list.sl_modlist = calloc(nmods, sizeof(struct str_mlist));
	if (list.sl_modlist == NULL)
		err_sys("calloc error");
	list.sl_nmods = nmods;

			/* and fetch the module names */
	if (ioctl(fd, I_LIST, &list) < 0)
		err_sys("I_LIST error for list");

			/* print the module names */
	for (i = 1; i <= nmods; i++)
		printf("  %s: %s\n", (i == nmods) ? "driver" : "module",
								list.sl_modlist++);
	exit(0);
}
示例#3
0
文件: ndd.c 项目: andreiw/polaris
/* ARGSUSED */
int
main(int argc, char **argv)
{
	char	*cp, *value;
	int	cmd;
	int	fd;

	if (!(cp = *++argv)) {
		while ((fd = open_device()) != -1) {
			getset_interactive(fd);
			(void) close(fd);
		}
		return (EXIT_SUCCESS);
	}

	cmd = ND_GET;
	if (cp[0] == '-') {
		if (strncmp(&cp[1], "set", 3) == 0)
			cmd = ND_SET;
		else if (strncmp(&cp[1], "get", 3) != 0)
			fatal(usage_str);
		if (!(cp = *++argv))
			fatal(usage_str);
	}
	if ((fd = open(cp, O_RDWR)) == -1)
		fatal("open of %s failed: %s", cp, errmsg(errno));

	if (!isastream(fd))
		fatal("%s is not a streams device", cp);

	if (!(cp = *++argv)) {
		getset_interactive(fd);
		(void) close(fd);
		return (EXIT_SUCCESS);
	}

	if (cmd == ND_SET) {
		if (!(value = *++argv))
			fatal(usage_str);
		(void) snprintf(gbuf, sizeof (gbuf), "%s%c%s%c", cp, '\0',
		    value, '\0');
		if (!do_getset(fd, cmd, gbuf, sizeof (gbuf)))
			return (EXIT_FAILURE);
	} else {
		do {
			(void) memset(gbuf, '\0', sizeof (gbuf));
			(void) strlcpy(gbuf, cp, sizeof (gbuf));
			if (!do_getset(fd, cmd, gbuf, sizeof (gbuf)))
				return (EXIT_FAILURE);
			if (cp = *++argv)
				(void) putchar('\n');
		} while (cp);
	}

	(void) close(fd);
	return (EXIT_SUCCESS);
}
示例#4
0
int cli_connect (const char *path)
{
	int fd;

	if ((fd = open (path, O_RDWR)) == -1)
		return (-1);

	return ((isastream (fd)) ? fd : -1);
}
示例#5
0
文件: sigio.c 项目: Agnesa/xserver
int
xf86RemoveSIGIOHandler(int fd)
{
    struct sigaction sa;
    struct sigaction osa;
    int i;
    int max;
    int maxfd;
    int ret;

    if (!xf86Info.useSIGIO)
        return 0;

    max = 0;
    maxfd = -1;
    ret = 0;
    for (i = 0; i < MAX_FUNCS; i++) {
        if (xf86SigIOFuncs[i].f) {
            if (xf86SigIOFuncs[i].fd == fd) {
                xf86SigIOFuncs[i].f = 0;
                xf86SigIOFuncs[i].fd = 0;
                xf86SigIOFuncs[i].closure = 0;
                FD_CLR(fd, &xf86SigIOMask);
                ret = 1;
            }
            else {
                max = i + 1;
                if (xf86SigIOFuncs[i].fd >= maxfd)
                    maxfd = xf86SigIOFuncs[i].fd + 1;
            }
        }
    }
    if (ret) {
#ifdef O_ASYNC
        fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_ASYNC);
#endif
#ifdef I_SETSIG
        if (isastream(fd)) {
            if (ioctl(fd, I_SETSIG, 0) == -1) {
                xf86Msg(X_WARNING, "fcntl(%d, I_SETSIG, 0): %s\n",
                        fd, strerror(errno));
            }
        }
#endif
        xf86SigIOMax = max;
        xf86SigIOMaxFd = maxfd;
        if (!max) {
            sigemptyset(&sa.sa_mask);
            sigaddset(&sa.sa_mask, SIGIO);
            sa.sa_flags = 0;
            sa.sa_handler = SIG_IGN;
            sigaction(SIGIO, &sa, &osa);
        }
    }
    return ret;
}
示例#6
0
int            /* returns fd if all OK, <0 on error */
cli_conn(const char *name)
{
    int        fd;

                /* open the mounted stream */
    if ( (fd = open(name, O_RDWR)) < 0)
        return(-1);
    if (isastream(fd) == 0)
        return(-2);

    return(fd);
}
示例#7
0
/**
 * @fn int socket(int domain, int type, int protocol)
 * @brief Create a socket.
 * @param domain the protocol domain (address or protocol family).
 * @param type the socket type (SOCK_STREAM, SOCK_DGRAM, etc.)
 * @param protocol the protocol within the domain, or zero (0) for default.
 *
 * NOTICES: This function cannot contain a thread cancellation point (according to XNS 5.2).
 */
int
__sock_socket(int domain, int type, int protocol)
{
	int err, sd;
	struct _s_user *user;
	struct socksysreq args = {
		{SO_SOCKET, domain, type, protocol,}
	};

	if (!(user = (struct _s_user *) malloc(sizeof(*user))))
		goto enomem;
	memset(user, 0, sizeof(*user));
	if ((sd = __sock_ioctl(&args)) == -1)
		goto badsock;
	/* try to convert it into a socket */
	if (isastream(sd)) {
		struct strioctl ioc;

		if (ioctl(sd, I_PUSH, "sockmod") == -1)
			goto badioctl;
		ioc.ic_cmd = O_SI_GETUDATA;
		ioc.ic_dp = (char *) &user->info;
		ioc.ic_len = sizeof(struct o_si_udata);
		ioc.ic_timout = SOCKMOD_TIMEOUT;
		if (ioctl(sd, I_STR, &ioc) == -1)
			goto badgetuser;
		user->info.sockparams.sp_family = domain;
		user->info.sockparams.sp_type = type;
		user->info.sockparams.sp_protocol = protocol;
		_s_fds[sd] = user;
	} else
		_s_fds[sd] = NULL;
	return (0);
      badgetuser:
      badioctl:
	err = errno;
	_s_fds[sd] = NULL;
	close(sd);
	free(user);
	errno = err;
	return (-1);
      badsock:
	err = errno;
	free(user);
	errno = err;
	return (-1);
      enomem:
	errno = ENOMEM;
	return (-1);
}
示例#8
0
/* connect to server on the given path: */
int cli_conn(const char *path)
{
     int fd;

     if ((fd=open(path,O_RDWR)) < 0) {
	  perror("open");
	  exit(1);
     }

     if (isastream(fd)==0) {
	  fprintf(stderr,"cli_conn: path \"%s\" is not a stream\n",path);
	  exit(1);
     }
     return fd;
}
示例#9
0
/**
 * @fn int accept(int fd, struct sockaddr *addr, socklen_t *len)
 * @brief Accept a socket connection.
 * @param fd the socket from which to accept the connection.
 * @param addr a pointer to a sockaddr structure to receive the connecting address.
 * @param len the length of the sockaddr.
 *
 * NOTICES: This function contains a thread cancellation point (according to XNS 5.2).
 *
 * IMPLEMENTATION: When Streams are created by the library in user space, it is necessary to open a
 * new Stream, possibly bind it to the same address as the Stream specified by fd, and then pass
 * this newly created Stream to the ioctl call.  When Streams are created by socksys(4) or
 * sockmod(4), the module or driver will open the Stream, however, there is a problem with LiS in
 * this regard in that the top module's put procedure does not necessarily execute with caller user
 * context, there is no way of hooking STREAMS ioctl (e.g. strioctl) and a Stream cannot be opened
 * without the danger of putting the STREAMS scheduler kernel thread to sleep.
 */
int
__sock_accept(int fd, struct sockaddr *addr, socklen_t * len)
{
	int err, sd;
	struct _s_user *user;
	struct socksysreq args = {
		{SO_ACCEPT, fd, (long) addr, (long) len,}
	};

	if (!(user = (struct _s_user *) malloc(sizeof(*user))))
		goto enomem;
	memset(user, 0, sizeof(*user));
	if ((sd = __sock_ioctl(&args)) == -1)
		goto badsock;
	/* try to convert it into a socket */
	if (isastream(sd)) {
		struct strioctl ioc;

		if (ioctl(sd, I_PUSH, "sockmod") == -1)
			goto badioctl;
		ioc.ic_cmd = SI_GETUDATA;
		ioc.ic_dp = (char *) &user->info;
		ioc.ic_len = sizeof(struct si_udata);
		ioc.ic_timout = SOCKMOD_TIMEOUT;
		if (ioctl(sd, I_STR, &ioc) == -1)
			goto badgetuser;
		_s_fds[sd] = user;
	} else
		_s_fds[sd] = NULL;
	return (0);
      badgetuser:
      badioctl:
	err = errno;
	_s_fds[sd] = NULL;
	close(sd);
	free(user);
	errno = err;
	return (-1);
      badsock:
	err = errno;
	free(user);
	errno = err;
	return (-1);
      enomem:
	errno = ENOMEM;
	return (-1);
}
示例#10
0
int
fattach(int fildes, const char *path)
{
	struct namefd  namefdp;
	struct door_info dinfo;
	int	s;
	char buf[MAXPATHLEN];

	/* Only STREAMS and doors allowed to be mounted */
	if ((s = isastream(fildes)) == 1 || __door_info(fildes, &dinfo) == 0) {
		namefdp.fd = fildes;
		if (path == NULL || *path == '\0') {
			errno = ENOENT;
			return (-1);
		} else if (*path != '/') {
			/*
			 * The mount point must be an absolute path.
			 */
			if (getcwd(buf, sizeof (buf)) == NULL) {
				/* errno already set */
				return (-1);
			}
			/*
			 * The kernel will truncate the path if it would have
			 * turned into something more than MAXPATHLEN bytes.
			 * So we do the same here.
			 */
			if (strlcat(buf, "/",  sizeof (buf)) >= sizeof (buf) ||
			    strlcat(buf, path, sizeof (buf)) >= sizeof (buf)) {
				errno = ENAMETOOLONG;
				return (-1);
			}
			path = buf;
		}
		return (mount((char *)NULL, path, MS_DATA|MS_NOMNTTAB,
		    (const char *)"namefs", (char *)&namefdp,
		    sizeof (struct namefd), NULL, 0));
	} else if (s == 0) {
		/* Not a STREAM */
		errno = EINVAL;
		return (-1);
	} else {
		/* errno already set */
		return (-1);
	}
}
示例#11
0
int main(int argc, char* argv[])
{
	if (argc != 2) {
		printf("usage: %s <pathname>\n", argv[0]);
		exit(1);
	}

	int fd = open(argv[1], O_RDONLY);
	if (fd < 0) {
		printf("can't open %s\n", argv[1]);
		exit(1);
	}
	if (isastream(fd) == 0) {
		printf("%s is not a stream\n", argv[1]);
		exit(1);
	}

	/* get nmods */
	int nmods = ioctl(fd, I_LIST, 0);
	if (nmods < 0) {
		printf("I_LIST error for nmods\n");
		exit(1);
	}
	printf("#modules = %d\n", nmods);

	struct str_list list;
	list.sl_modlist = calloc(nmods, sizeof(struct str_mlist));
	if (!list.sl_modlist) {
		printf("calloc error\n");
		exit(1);
	}
	list.sl_nmods = nmods;

	if (ioctl(fd, I_LIST, &list) < 0) {
		printf("I_LIST error for list\n");
		exit(1);
	}

	int i;
	for(i = 1; i <= nmods; i++) printf("%s %s\n", (i==nmods) ? "driver" : "module", list.sl_modlist++->l_name);

	return 0;
}
示例#12
0
int main (int argc, char **argv)
{
	int fd;
	int i;

	if (argc < 2)
		err_quit ("Usage: isastream file ...");

	for (i = 1; i < argc; i++) {
		if ((fd = open (argv [i], O_RDONLY)) == -1) {
			err_ret ("Can't open %s", argv [i]);
			continue;
		}

		printf ("%s %s a STREAMS device\n", argv [i],
			(isastream (fd)) ? "is" : "is not");
	}

	return (0);
}
示例#13
0
void
getslave ()
{
  (void) setsid ();
  /* grantpt( master);
     unlockpt(master);
     if ((slave = open((const char *)ptsname(master), O_RDWR)) < 0) {
     perror((const char *)ptsname(master));
     fail();
     perror("open(fd, O_RDWR)");
     fail();
     } */
#ifndef NOSTREAMS
  if (isastream (slave))
    {
      if (ioctl (slave, I_PUSH, "ptem") < 0)
        {
          perror ("ioctl(fd, I_PUSH, ptem)");
          fail ();
        }
      if (ioctl (slave, I_PUSH, "ldterm") < 0)
        {
          perror ("ioctl(fd, I_PUSH, ldterm)");
          fail ();
        }
#ifndef _HPUX_SOURCE
      if (ioctl (slave, I_PUSH, "ttcompat") < 0)
        {
          perror ("ioctl(fd, I_PUSH, ttcompat)");
          fail ();
        }
#endif
    }
#endif /* !NOSTREAMS */
  (void) ioctl (0, TIOCGWINSZ, (char *) &win);
}
示例#14
0
s48_ref_t pty2tty(s48_call_t call, s48_ref_t sch_fd) {
    int fd = s48_extract_long_2(call, sch_fd);

#if defined (HAVE_ISASTREAM) && defined (I_PUSH)
    if (isastream (fd))
    {
# if defined (I_FIND)
#  define stream_module_pushed(fd, module) (ioctl (fd, I_FIND, module) == 1)
# else
#  define stream_module_pushed(fd, module) 0
# endif
        if ((! stream_module_pushed (fd, "ptem")) &&
            (ioctl (fd, I_PUSH, "ptem") < 0))
            s48_os_error_2(call, "pty2tty", errno, 1, sch_fd);
        if ((! stream_module_pushed (fd, "ldterm")) &&
            (ioctl (fd, I_PUSH, "ldterm") < 0))
            s48_os_error_2(call, "pty2tty", errno, 1, sch_fd);
        if ((! stream_module_pushed (fd, "ttcompat")) &&
            (ioctl (fd, I_PUSH, "ttcompat") < 0))
            s48_os_error_2(call, "pty2tty", errno, 1, sch_fd);
    }
#endif /* defined (HAVE_ISASTREAM) && defined (I_PUSH) */
    return s48_unspecific_2(call);
}
示例#15
0
文件: ndd.c 项目: andreiw/polaris
static int
open_device(void)
{
	char	name[80];
	int	fd, len;

	for (;;) {
		len = get_value("module to query ? ", name, sizeof (name));
		if (len <= 1 ||
		    (len == 2 && (name[0] == 'q' || name[0] == 'Q')))
			return (-1);

		if ((fd = open(name, O_RDWR)) == -1) {
			printe(B_TRUE, "open of %s failed", name);
			continue;
		}

		if (isastream(fd))
			return (fd);

		(void) close(fd);
		printe(B_FALSE, "%s is not a streams device", name);
	}
}
示例#16
0
int
main(int argc, char *argv[])
{
	int					fd, i, nmods;
	struct str_list		list;

	if (argc != 2)
		err_quit("usage: a.out <pathname>");

	fd = T_open(argv[1], O_RDWR, NULL);
	if (isastream(fd) == 0)
		err_quit("%s is not a stream", argv[1]);

	list.sl_nmods = nmods = Ioctl(fd, I_LIST, (void *) 0);
	printf("%d modules\n", nmods);
	list.sl_modlist = Calloc(nmods, sizeof(struct str_mlist));

	Ioctl(fd, I_LIST, &list);

	for (i = 1; i <= nmods; i++)
		printf("  %s: %s\n", (i == nmods) ? "driver" : "module",
								list.sl_modlist++);
	exit(0);
}
示例#17
0
/**
 * @internal
 * @brief Get a locked socket user structure.
 * @param fd the socket descriptor for which to get the associated user structure.
 *
 * This is a range-checked array lookup of the library user structure associated
 * with the specific file descriptor.  Also, this function takes the necessary
 * locks for thread-safe operation.
 */
static __hot struct _s_user *
__sock_getuser(int fd)
{
	struct _s_user *user;
	int err;

	if (unlikely((err = __sock_list_rdlock())))
		goto list_lock_error;
	if (unlikely(0 > fd) || unlikely(fd >= OPEN_MAX))
		goto badf;
	if (unlikely(!(user = _s_fds[fd])))
		goto badf;
	if (unlikely((err = __sock_user_wrlock(user))))
		goto user_lock_error;
	return (user);
      user_lock_error:
	errno = err;
	__sock_list_unlock(NULL);
	goto error;
      badf:
	/* At this point we check if we have a stream and if we do, we need to perform an
	   SI_GETUDATA to see if we can syncrhonize the file descriptor with the socket
	   information.  If we can, this file descriptor was created by some other mechanism, such
	   as open, t_open, or dup. */
	if (isastream(fd)) {
		struct strioctl ioc;

		if (!(user = (struct _s_user *) malloc(sizeof(*user))))
			goto enomem;
		memset(user, 0, sizeof(*user));
		switch (ioctl(fd, I_FIND, "sockmod")) {
		case -1:
		default:
			goto badfind;
		case 0:
			/* XXX: maybe we shouldn't push it if it does not already exist on the
			   emodule stack (i.e. it is a stream by not a socket. */
			if (ioctl(fd, I_PUSH, "sockmod") == -1)
				goto badpush;
			break;
		case 1:
			break;
		}
		ioc.ic_cmd = SI_GETUDATA;
		ioc.ic_dp = (char *) &user->info;
		ioc.ic_len = sizeof(struct si_udata);
		ioc.ic_timout = SOCKMOD_TIMEOUT;
		if (ioctl(fd, I_STR, &ioc) == -1)
			goto badgetuser;
		_s_fds[fd] = user;
		return (user);
	      enomem:
		errno = ENOMEM;
		goto error;
	      badgetuser:
		ioctl(fd, I_POP, NULL);
	      badfind:
	      badpush:
		free(user);
	}
	errno = EBADF;
	goto error;
      list_lock_error:
	errno = err;
	goto error;
      error:
	return (NULL);
}
示例#18
0
文件: pty.c 项目: milisarge/termrec
int forkpty(int *amaster,char *dummy,struct termios *termp, struct winsize *wp)
{
    int master,slave;
    int pid;

#ifdef HAVE__GETPTY
    int filedes[2];
    char *line;

    line = _getpty(&filedes[0], O_RDWR|O_NDELAY, 0600, 0);
    if (0 == line)
        return -1;
    if (0 > (filedes[1] = open(line, O_RDWR)))
    {
        close(filedes[0]);
        return -1;
    }
    master=filedes[0];
    slave=filedes[1];
#elif defined(HAVE_GRANTPT) && (defined(HAVE_GETPT) || defined(HAVE_DEV_PTMX) || defined(HAVE_POSIX_OPENPT))
# ifdef HAVE_PTSNAME
    char *name;
# else
    char name[80];
# endif

# ifdef HAVE_GETPT
    master=getpt();
# elif HAVE_POSIX_OPENPT
    master=posix_openpt(O_RDWR);
# else
    master=open("/dev/ptmx", O_RDWR);
# endif

    if (master<0)
        return -1;

    if (grantpt(master)<0||unlockpt(master)<0)
        goto close_master;

# ifdef HAVE_PTSNAME
    if (!(name=ptsname(master)))
        goto close_master;
# else
    if (ptsname_r(master,name,80))
        goto close_master;
# endif

    slave=open(name,O_RDWR);
    if (slave==-1)
        goto close_master;

# ifdef HAVE_STROPTS_H
    if (isastream(slave))
        if (ioctl(slave, I_PUSH, "ptem")<0
                ||ioctl(slave, I_PUSH, "ldterm")<0)
            goto close_slave;
# endif

    goto ok;

//close_slave:
    close (slave);

close_master:
    close (master);
    return -1;

ok:
#else
  char *p, *q, *l, *d;
  char PtyName[32], TtyName[32];

  strcpy(PtyName, PtyProto);
  strcpy(TtyName, TtyProto);
  for (p = PtyName; *p != 'X'; p++)
    ;
  for (q = TtyName; *q != 'X'; q++)
    ;
  for (l = PTYRANGE0; (*p = *l) != '\0'; l++)
    {
      for (d = PTYRANGE1; (p[1] = *d) != '\0'; d++)
        {
//        tintin_printf(0,"OpenPTY tries '%s'", PtyName);
          if ((master = open(PtyName, O_RDWR | O_NOCTTY)) == -1)
            continue;
          q[0] = *l;
          q[1] = *d;
          if (access(TtyName, R_OK | W_OK))
            {
              close(master);
              continue;
            }
          if ((slave=open(TtyName, O_RDWR|O_NOCTTY))==-1)
	  {
	  	close(master);
	  	continue;
	  }
          goto ok;
        }
    }
  return -1;
  ok:
#endif

    if (termp)
        tcsetattr(master, TCSANOW, termp);
    if (wp)
        ioctl(master,TIOCSWINSZ,wp);
    // let's ignore errors on this ioctl silently

    pid=fork();
    switch (pid)
    {
    case -1:
        close(master);
        close(slave);
        return -1;
    case 0:
        close(master);
        setsid();
        dup2(slave,0);
        dup2(slave,1);
        dup2(slave,2);
        close(slave);
        return 0;
    default:
        close(slave);
        *amaster=master;
        return pid;
    }
}
示例#19
0
int
open_direct(char *ptype, PRINTER *pp)
{
	short bufsz = -1, cps = -1;
	int open_mode, fd;
	register unsigned int oldalarm, newalarm = 0;
	char *device;

	struct ecpp_transfer_parms ecpp_params;	/* for ECPP port checking */
	char **modules = NULL;

	struct flock		lck;
	struct stat		buf;

	register void		(*oldsig)() = signal(SIGALRM, sigalrm);


	/*
	 * Set an alarm to wake us from trying to open the port.
	 * We'll try at least 60 seconds, or more if the printer
	 * has a huge buffer that, in the worst case, would take
	 * a long time to drain.
	 */
	tidbit(ptype, "bufsz", &bufsz);
	tidbit(ptype, "cps", &cps);
	if (bufsz > 0 && cps > 0)
		newalarm = (((long)bufsz * 1100) / cps) / 1000;
	if (newalarm < 60)
		newalarm = 60;
	oldalarm = alarm(newalarm);

	device = pp->device;
	if (is_printer_uri(device) == 0) {
		/*
		 * if it's a device uri and the endpoint contains a valid
		 * path, that path should be opened/locked by lpsched for
		 * the backend.  If not, the uri isn't associated with a
		 * local device, so use /dev/null.
		 */
		device = strstr(device, "://");
		if (device != NULL)
			device = strchr(device + 3, '/');

		if ((device == NULL) || (access(device, F_OK) < 0))
			device = "/dev/null";
	}

	/*
	 * The following open must be interruptable.
	 * O_APPEND is set in case the ``port'' is a file.
	 * O_RDWR is set in case the interface program wants
	 * to get input from the printer. Don't fail, though,
	 * just because we can't get read access.
	 */

	open_mode = O_WRONLY;
	if (access(device, R_OK) == 0)
		open_mode = O_RDWR;
	open_mode |= O_APPEND;

	SigAlrm = 0;

	while ((fd = open(device, open_mode, 0)) == -1) {
		if (errno != EINTR)
			return (EXEC_EXIT_NPORT);
		else if (SigAlrm)
			return (EXEC_EXIT_TMOUT);
	}

	alarm(oldalarm);
	signal(SIGALRM, oldsig);

	/*
	 * Lock the file in case two "printers" are defined on the
	 * same port.  Don't lock /dev/null.
	 */

	lck.l_type = F_WRLCK;
	lck.l_whence = 0;
	lck.l_start = 0L;
	lck.l_len = 0L;

	if (strcmp(device, "/dev/null") && Fcntl(fd, F_SETLKW, &lck) < 0) {
		execlog("lock error: %s\n", pp->device);
		return (EXEC_EXIT_NPORT);
	}

	/*
	 * We should get the correct channel number (1), but just
	 * in case....
	 */
	if (fd != 1) {
		dup2(fd, 1);
		Close(fd);
	}

	/*
	 * Handle streams modules:
	 */
	if (fstat(1, &buf))
		buf.st_mode = 0;

	/*
	 * for some unknown reason, lpsched appears to pop the streams
	 * modules off the device and push back some "default" ones,
	 * unless a specific set were specified with the printer configuration.
	 * This behaviour causes problems with the ECPP port, so if we have
	 * an ECPP port, and nobody specified a set of modules to use, we
	 * should leave it alone.  Normally, we would not bother to play with
	 * the streams modules, but it is possible that someone has come
	 * to rely on this behaviour for other devices.
	 */
	if ((pp->modules != NULL) && (pp->modules[0] != NULL) &&
	    (strcmp(pp->modules[0], "default") != 0))
		modules = pp->modules;

	if ((modules == NULL) && (ioctl(1, ECPPIOC_GETPARMS, &ecpp_params) < 0))
		modules = getlist(DEFMODULES, LP_WS, LP_SEP);

	/* if "nopush" is supplied, leave the modules alone */
	if ((modules != NULL) && (modules[0] != NULL) &&
	    (strcasecmp(modules[0], "nopush") == 0))
		modules = NULL;

	/*
	 * If we have a stream and a list of modules to use, then pop the old
	 * modules and push the new ones.
	 */
	if ((modules != NULL) && !S_ISFIFO(buf.st_mode) && isastream(1)) {
		/*
		 * First, pop all current modules off, unless
		 * instructed not to.
		 */
		while (ioctl(1, I_POP, 0) == 0)
			;

		/*
		 * Now push either the administrator specified modules
		 * or the standard modules, unless instructed to push
		 * nothing.
		 */

		if ((modules[1] == NULL) &&
		    (strcasecmp(modules[0], "none") == 0))
			return (0);

		while (*modules)
			if (push_module(1, device, *modules++) == -1)
				return (EXEC_EXIT_NPUSH);
	}

	return (0);
}
示例#20
0
int 
readvar(unsigned char **names)
{
	struct fileblk	fb;
	register struct fileblk *f = &fb;
	unsigned char	c[MULTI_BYTE_MAX+1];
	register int	rc = 0;
	struct namnod *n;
	unsigned char	*rel;
	unsigned char *oldstak;
	register unsigned char *pc, *rest;
	int		d;
	unsigned int	(*newwc)(void);
	extern const char	badargs[];

	if (eq(*names, "-r")) {
		if (*++names == NULL)
			error(badargs);
		newwc = readwc;
	} else
		newwc = nextwc;
	n = lookup(*names++);	/* done now to avoid storage mess */
	rel = (unsigned char *)relstak();
	push(f);
	initf(dup(0));

	/*
	 * If stdin is a pipe then this lseek(2) will fail with ESPIPE, so
	 * the read buffer size is set to 1 because we will not be able
	 * lseek(2) back towards the beginning of the file, so we have
	 * to read a byte at a time instead
	 *
	 */
	if (lseek(0, (off_t)0, SEEK_CUR) == -1)
		f->fsiz = 1;

#ifdef	__sun
	/*
	 * If stdin is a socket then this isastream(3C) will return 1, so
	 * the read buffer size is set to 1 because we will not be able
	 * lseek(2) back towards the beginning of the file, so we have
	 * to read a byte at a time instead
	 *
	 */
	if (isastream(0) == 1)
		f->fsiz = 1;
#endif

	/*
	 * strip leading IFS characters
	 */
	for (;;) 
	{
		d = newwc();
		if(eolchar(d))
			break;
		rest = readw(d);
		pc = c;
		while(*pc++ = *rest++);
		if(!anys(c, ifsnod.namval))
			break;
	}

	oldstak = curstak();
	for (;;)
	{
		if ((*names && anys(c, ifsnod.namval)) || eolchar(d))
		{
			if (staktop >= brkend)
				growstak(staktop);
			zerostak();
			assign(n, absstak(rel));
			setstak(rel);
			if (*names)
				n = lookup(*names++);
			else
				n = 0;
			if (eolchar(d))
			{
				break;
			}
			else		/* strip imbedded IFS characters */
				while(1) {
					d = newwc();
					if(eolchar(d))
						break;
					rest = readw(d);
					pc = c;
					while(*pc++ = *rest++);
					if(!anys(c, ifsnod.namval))
						break;
				}
		}
		else
		{
			if(d == '\\' && newwc == nextwc) {
				d = newwc();
				rest = readw(d);
				while(d = *rest++) {
					if (staktop >= brkend)
						growstak(staktop);
					pushstak(d);
				}
				oldstak = staktop;
			}
			else
			{
				pc = c;
				while(d = *pc++) {
					if (staktop >= brkend)
						growstak(staktop); 
					pushstak(d);
				}
				if(!anys(c, ifsnod.namval))
					oldstak = staktop;
			}
			d = newwc();

			if (eolchar(d))
				staktop = oldstak;
			else 
			{
				rest = readw(d);
				pc = c;
				while(*pc++ = *rest++);
			}
		}
	}
	while (n)
	{
		assign(n, nullstr);
		if (*names)
			n = lookup(*names++);
		else
			n = 0;
	}

	if (eof)
		rc = 1;

#ifdef	__sun
	if (isastream(0) != 1)
#endif
		/*
		 * If we are reading on a stream do not attempt to
		 * lseek(2) back towards the start because this is
		 * logically meaningless, but there is nothing in
		 * the standards to pervent the stream implementation
		 * from attempting it and breaking our code here
		 *
		 */
		lseek(0, (off_t)(f->nxtoff - f->endoff), SEEK_CUR);

	pop();
	return(rc);
}
示例#21
0
文件: sigio.c 项目: Agnesa/xserver
int
xf86InstallSIGIOHandler(int fd, void (*f) (int, void *), void *closure)
{
    struct sigaction sa;
    struct sigaction osa;
    int i;
    int installed = FALSE;

    if (!xf86Info.useSIGIO)
        return 0;

    for (i = 0; i < MAX_FUNCS; i++) {
        if (!xf86SigIOFuncs[i].f) {
            if (xf86IsPipe(fd))
                return 0;
            OsBlockSIGIO();
#ifdef O_ASYNC
            if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_ASYNC) == -1) {
                xf86Msg(X_WARNING, "fcntl(%d, O_ASYNC): %s\n",
                        fd, strerror(errno));
            }
            else {
                if (fcntl(fd, F_SETOWN, getpid()) == -1) {
                    xf86Msg(X_WARNING, "fcntl(%d, F_SETOWN): %s\n",
                            fd, strerror(errno));
                }
                else {
                    installed = TRUE;
                }
            }
#endif
#ifdef I_SETSIG                 /* System V Streams - used on Solaris for input devices */
            if (!installed && isastream(fd)) {
                if (ioctl(fd, I_SETSIG, S_INPUT | S_ERROR | S_HANGUP) == -1) {
                    xf86Msg(X_WARNING, "fcntl(%d, I_SETSIG): %s\n",
                            fd, strerror(errno));
                }
                else {
                    installed = TRUE;
                }
            }
#endif
            if (!installed) {
                OsReleaseSIGIO();
                return 0;
            }
            sigemptyset(&sa.sa_mask);
            sigaddset(&sa.sa_mask, SIGIO);
            sa.sa_flags = 0;
            sa.sa_handler = xf86SIGIO;
            sigaction(SIGIO, &sa, &osa);
            xf86SigIOFuncs[i].fd = fd;
            xf86SigIOFuncs[i].closure = closure;
            xf86SigIOFuncs[i].f = f;
            if (i >= xf86SigIOMax)
                xf86SigIOMax = i + 1;
            if (fd >= xf86SigIOMaxFd)
                xf86SigIOMaxFd = fd + 1;
            FD_SET(fd, &xf86SigIOMask);
            OsReleaseSIGIO();
            return 1;
        }
        /* Allow overwriting of the closure and callback */
        else if (xf86SigIOFuncs[i].fd == fd) {
            xf86SigIOFuncs[i].closure = closure;
            xf86SigIOFuncs[i].f = f;
            return 1;
        }
    }
    return 0;
}
示例#22
0
/**
 * @fn int socketpair(int domain, int type, int protocol, int socket_vector[2])
 * @brief Create a pair of connected sockets.
 * @param domain protocol family.
 * @param type socket type.
 * @param protocol protocol within family.
 * @param socket_vector vector into which to receive two integer file descriptors for the connected sockets.
 * 
 * NOTICES: This function cannot contain a thread cancellation point (according to XNS 5.2).
 */
int
__sock_socketpair(int domain, int type, int protocol, int socket_vector[2])
{
	int err, socks[2] = { -1, -1 };
	struct _s_user *user1, *user2;
	struct socksysreq args = {
		{SO_SOCKPAIR, domain, type, protocol, (long) socks,}
	};

	if (!(user1 = (struct _s_user *) malloc(sizeof(*user1))))
		goto enomem1;
	memset(user1, 0, sizeof(*user1));
	if (!(user2 = (struct _s_user *) malloc(sizeof(*user2))))
		goto enomem2;
	memset(user2, 0, sizeof(*user2));
	if (__sock_ioctl(&args) == -1)
		goto badpair;
	/* try to convert them to sockets */
	if (isastream(socks[0])) {
		struct strioctl ioc;

		if (ioctl(socks[0], I_PUSH, "sockmod") == -1)
			goto badioctl;
		ioc.ic_cmd = O_SI_GETUDATA;
		ioc.ic_dp = (char *) &user1->info;
		ioc.ic_len = sizeof(struct o_si_udata);
		ioc.ic_timout = SOCKMOD_TIMEOUT;
		if (ioctl(socks[0], I_STR, &ioc) == -1)
			goto badgetuser;
		user1->info.sockparams.sp_family = domain;
		user1->info.sockparams.sp_type = type;
		user1->info.sockparams.sp_protocol = protocol;
		_s_fds[socks[0]] = user1;
	} else
		_s_fds[socks[0]] = NULL;
	if (isastream(socks[1])) {
		struct strioctl ioc;

		if (ioctl(socks[1], I_PUSH, "sockmod") == -1)
			goto badioctl;
		ioc.ic_cmd = O_SI_GETUDATA;
		ioc.ic_dp = (char *) &user1->info;
		ioc.ic_len = sizeof(struct o_si_udata);
		ioc.ic_timout = SOCKMOD_TIMEOUT;
		if (ioctl(socks[1], I_STR, &ioc) == -1)
			goto badgetuser;
		user2->info.sockparams.sp_family = domain;
		user2->info.sockparams.sp_type = type;
		user2->info.sockparams.sp_protocol = protocol;
		_s_fds[socks[1]] = user2;
	} else
		_s_fds[socks[1]] = NULL;
	socket_vector[0] = socks[0];
	socket_vector[1] = socks[1];
	return (0);
      badgetuser:
      badioctl:
	err = errno;
	_s_fds[socks[1]] = NULL;
	_s_fds[socks[0]] = NULL;
	close(socks[1]);
	close(socks[0]);
	free(user2);
	free(user1);
	errno = err;
	return (-1);
      badpair:
	err = errno;
	free(user2);
	free(user1);
	errno = err;
	return (-1);
      enomem2:
	free(user1);
      enomem1:
	errno = ENOMEM;
	return (-1);

}
示例#23
0
文件: pty.c 项目: gbl/vte
/**
 * vte_pty_child_setup:
 * @pty: a #VtePty
 *
 * FIXMEchpe
 */
void
vte_pty_child_setup (VtePty *pty)
{
        VtePtyPrivate *priv = pty->priv;
	VtePtyChildSetupData *data = &priv->child_setup_data;
	int fd = -1;
	const char *tty = NULL;
        char version[7];

        if (priv->foreign) {
                fd = priv->pty_fd;
        } else {
                /* Save the name of the pty -- we'll need it later to acquire
                * it as our controlling terminal.
                */
                switch (data->mode) {
                        case TTY_OPEN_BY_NAME:
                                tty = data->tty.name;
                                break;
                        case TTY_OPEN_BY_FD:
                                fd = data->tty.fd;
                                tty = ttyname(fd);
                                break;
                }

                _vte_debug_print (VTE_DEBUG_PTY,
                                "Setting up child pty: name = %s, fd = %d\n",
                                        tty ? tty : "(none)", fd);


                /* Try to reopen the pty to acquire it as our controlling terminal. */
                /* FIXMEchpe: why not just use the passed fd in TTY_OPEN_BY_FD mode? */
                if (tty != NULL) {
                        int i = open(tty, O_RDWR);
                        if (i != -1) {
                                if (fd != -1){
                                        close(fd);
                                }
                                fd = i;
                        }
                }
        }

	if (fd == -1)
		_exit (127);

	/* Start a new session and become process-group leader. */
#if defined(HAVE_SETSID) && defined(HAVE_SETPGID)
	_vte_debug_print (VTE_DEBUG_PTY, "Starting new session\n");
	setsid();
	setpgid(0, 0);
#endif

#ifdef TIOCSCTTY
	/* TIOCSCTTY is defined?  Let's try that, too. */
	ioctl(fd, TIOCSCTTY, fd);
#endif

#ifdef HAVE_STROPTS_H
	if (isastream (fd) == 1) {
		if ((ioctl(fd, I_FIND, "ptem") == 0) &&
				(ioctl(fd, I_PUSH, "ptem") == -1)) {
			_exit (127);
		}
		if ((ioctl(fd, I_FIND, "ldterm") == 0) &&
				(ioctl(fd, I_PUSH, "ldterm") == -1)) {
			_exit (127);
		}
		if ((ioctl(fd, I_FIND, "ttcompat") == 0) &&
				(ioctl(fd, I_PUSH, "ttcompat") == -1)) {
			perror ("ioctl (fd, I_PUSH, \"ttcompat\")");
			_exit (127);
		}
	}
#endif

	/* now setup child I/O through the tty */
	if (fd != STDIN_FILENO) {
		if (dup2(fd, STDIN_FILENO) != STDIN_FILENO){
			_exit (127);
		}
	}
	if (fd != STDOUT_FILENO) {
		if (dup2(fd, STDOUT_FILENO) != STDOUT_FILENO){
			_exit (127);
		}
	}
	if (fd != STDERR_FILENO) {
		if (dup2(fd, STDERR_FILENO) != STDERR_FILENO){
			_exit (127);
		}
	}

	/* Close the original slave descriptor, unless it's one of the stdio
	 * descriptors. */
	if (fd != STDIN_FILENO &&
			fd != STDOUT_FILENO &&
			fd != STDERR_FILENO) {
		close(fd);
	}

	/* Reset our signals -- our parent may have done any number of
	 * weird things to them. */
	_vte_pty_reset_signal_handlers();

        /* Now set the TERM environment variable */
        /* FIXME: Setting environment here seems to have no effect, the merged envp2 will override on exec.
         * By the way, we'd need to set the one from there, if any. */
        g_setenv("TERM", VTE_DEFAULT_TERM, TRUE);

        g_snprintf (version, sizeof (version), "%u", VTE_VERSION_NUMERIC);
        g_setenv ("VTE_VERSION", version, TRUE);

	/* Finally call an extra child setup */
	if (data->extra_child_setup) {
		data->extra_child_setup (data->extra_child_setup_data);
	}
}