Beispiel #1
0
int op_write(const int extra_flag)
{
  // open
  int ret = TFS_ERROR;
  int fd = 0;
  if (extra_flag == T_LARGE)
  {
    // 打开一个tfs文件写,为了断点续传,必须传入一个key参数,来标识此次大文件的写。
    // 一次写失败后,再次传入相同的key写,tfsclient会根据key找到前一次已经写完成的
    // 部分进行重用。
    // 为了确保key的唯一性,当前tfsclient接受的key必须是本地文件系统上存在的一个文件路径,
    // 该key文件的内容无所谓.

    fd = t_open(NULL, ".jpg", T_WRITE|extra_flag, key_file);
  }
  else
  {
    fd = t_open(NULL, ".jpg", T_WRITE|extra_flag, NULL);
  }

  if (fd <= 0)
  {
    printf("get fd for write fail, ret: %d \n", fd);
    return ret;
  }
  // lseek then write and pwrite not supported now

  // write
  int64_t len = 0;
  char buf[OP_SIZE];

  if ((len = t_write(fd, buf, OP_SIZE)) != OP_SIZE)
  {
    printf("write data to tfs fail, ret: %" PRI64_PREFIX "d\n", len);
    // write fail, just close fd
    t_close(fd, NULL, 0);
    return ret;
  }

  // write success, close fd and get tfs file name
  if ((ret = t_close(fd, tfs_name, TFS_FILE_LEN)) != TFS_SUCCESS)
  {
    printf("close tfs file fail, ret: %d\n", ret);
    return ret;
  }

  return ret;
}
Beispiel #2
0
void c_SQLite3::t___construct(CStrRef filename,
                       int64 flags /* = k_SQLITE3_OPEN_READWRITE |
                                      k_SQLITE3_OPEN_CREATE */,
                       CStrRef encryption_key /* = null_string */) {
  INSTANCE_METHOD_INJECTION_BUILTIN(SQLite3, SQLite3::__construct);
  t_open(filename, flags, encryption_key);
}
Beispiel #3
0
void tests()
{
    t_open("fname", PRATA, IF | FOR | STRING | VETOR);
    t_add(fname("olho", "mel") == "olhomel");
    t_add(fname("minhoca", "arvore") == "minhocrvore");
    t_add(fname("pegasus", "suspiro") == "pegapiro");
}
Beispiel #4
0
/*
 ****************************************************************
 *	Abre a conexão (parte do servidor)			*
 ****************************************************************
 */
int
tcp_open (int port)
{
	int		fd;
	T_BIND		req;
	INADDR		req_addr;

	if ((fd = t_open (tcpdevname, O_RDWR, (T_INFO *)NULL)) < 0)
		return (-1);

	/*
	 *	Preenche a estrutura T_BIND
	 */
	FILL_INADDR (req_addr, 0, port);
	FILL_NETBUF (req.addr, &req_addr, sizeof (req_addr));
	req.qlen = MAX_LISTEN_QLEN;

	if (t_bind (fd, &req, (T_BIND *)NULL) < 0)
		goto bad;

	if (set_param (fd) < 0)
		goto bad;

	listen_port = port;

	return (fd);

bad:	t_close (fd);
	return (-1);

}	/* end tcp_open */
Beispiel #5
0
int local_work(const int port, struct t_call **call)
{
	struct t_bind *req;
	struct sockaddr_in addr_in;
	int fd = -1;

	if ((fd = t_open(DEV_XTI, O_RDWR, NULL)) == -1)
		error("serwer->t_open", fd);

	if ((req = (struct t_bind *) t_alloc(fd, T_BIND, T_ADDR)) == NULL)
		error("serwer->t_alloc-bind", fd);

	req->qlen = 5;
	req->addr.len = sizeof(addr_in);
	req->addr.buf = (char *) & addr_in;

	addr_in.sin_family = AF_INET;
	addr_in.sin_addr.s_addr = INADDR_ANY;
	addr_in.sin_port = htons(port);

	if (t_bind(fd, req, req) < 0)
		error("serwer->t_bind", fd);

	if ((*call = (struct t_call *) t_alloc(fd, T_CALL, T_ALL)) == NULL)
		error("serwer->t_alloc-bind", fd);

	(*call)->addr.len = sizeof(addr_in);
	(*call)->udata.maxlen = 0;

	return fd;
}
Beispiel #6
0
/*
 ****************************************************************
 *	Inicia um cliente					*
 ****************************************************************
 */
int
tcp_connect (int port, const char *name)
{
	int		fd;
	T_BIND		bind;
	T_CALL		call;
	INADDR		bind_addr, addr;
	IPADDR		remote_ip_addr;

	if ((fd = t_open (tcpdevname, O_RDWR, (T_INFO *)NULL)) < 0)
		return (-1);

	/*
	 *	Associa um endereço local.
	 */
	FILL_INADDR (bind_addr, 0, 0);
	FILL_NETBUF (bind.addr, &bind_addr, sizeof (bind_addr));
	bind.qlen = 0;		/* Não vamos dar "t_listen" nesta conexão */

	if (t_bind (fd, &bind, (T_BIND *)NULL) < 0)
		goto bad;

	if (set_param (fd) < 0)
		goto bad;

	/*
	 *	Converte o nome da estação remota em um endereço IP.
	 */
	if (name != NOSTR && name[0] != '\0')
	{
		remote_ip_addr = t_node_to_addr (fd, name, NULL);
		if (remote_ip_addr == -1)
			goto bad;
	}
	else
	{
		remote_ip_addr = LOCAL_IP_ADDR;
	}

	/*
	 *	Preenche a estrutura T_CALL: só o membro addr é relevante.
	 */
	FILL_INADDR (addr, remote_ip_addr, port);

	FILL_NETBUF (call.addr,  &addr, sizeof (addr));
	FILL_NETBUF (call.opt,   NULL,       0);
	FILL_NETBUF (call.udata, NULL,       0);

	/*
	 *	Tenta estabeler a conexão com a estação remota.
	 */
	if (t_connect (fd, &call, (T_CALL *)NULL) < 0)
		goto bad;

	return (fd);

bad:	t_close (fd);
	return (-1);

}	/* end tcp_connect */
Beispiel #7
0
int t6b(char *name)
{
        char path[MAX_PATH_LENGTH] = "";
        char path2[MAX_PATH_LENGTH] = "";
        char cwd[MAX_PATH_LENGTH] = "";
        char *tmp;
        int fd;

        ENTER("symlink + chdir and open");
        snprintf(path, MAX_PATH_LENGTH, "%s/test_t6b", lustre_path);
        snprintf(path2, MAX_PATH_LENGTH, "%s/test_t6b_link", lustre_path);

        t_mkdir(path);
        t_symlink(path, path2);
        t_check_stat(path2, NULL);

        tmp = getcwd(cwd, MAX_PATH_LENGTH);
        if (tmp == NULL) {
                fprintf(stderr, "current path too long to fit in "
                        "MAX_PATH_LENGTH?\n");
                LEAVE();
        }
        t_chdir(path2);
        t_chdir(cwd);
        t_rmdir(path);
        t_touch(path);

        fd = t_open(path2);
        t_close(fd);

        t_unlink(path2);
        t_unlink(path);
        LEAVE();
}
Beispiel #8
0
int
main ()
{
	int	udp_fd;
	DNS	dns[2];

	memsetl (dns,    0, sizeof (dns)   / sizeof (long));

	if ((udp_fd = t_open (udp_dev, O_RDWR, (T_INFO *)NULL)) < 0)
		error ("$*Não consegui abrir \"%s\"", udp_dev);

	strcpy (dns[0].d_host_nm, "marte2.nce.ufrj.br");
        dns[0].d_host_addr = 0xC0A80002;

        dns[0].d_entry_code = 'C';
        dns[0].d_error_code = DNS_NO_ERROR;
        dns[0].d_server_index = 1;

        dns[0].d_expir_time = 12000000;
   /*** dns[0].d_preference = ...; ***/

        if (ioctl (udp_fd, I_DNS_PUT_INFO, dns) < 0)
                 error ("*Erro no \"ioctl\" I_DNS_PUT_INFO");

	return (0);
}
Beispiel #9
0
void t1()
{
        const int bufsize = 4096;
        char *path = "/mnt/lustre/rp_ost_t1_file";
        char buf[bufsize];
        int fd, i, j;
        ENTRY("open-write-close-open-failover-read (no ping involved)");

        printf("create/open file...\n");
        t_touch(path);
        fd = t_open(path);
        printf("write file...\n");
        for (i = 0; i < 20; i++) {
                memset(buf, i, bufsize);
                if (write(fd, buf, bufsize) != bufsize) {
                        perror("write error");
                        exit(-1);
                }
        }
        printf("close/reopen...\n");
        t_close(fd);
        fd = t_open(path);
        lseek(fd, 0, SEEK_SET);

        printf("OST failover...\n");
        replay_barrier();
        mds_failover();

        printf("read & verify...\n");
        for (i = 0; i < 20; i++) {
                memset(buf, -1, bufsize);
                if (read(fd, buf, bufsize) != bufsize) {
                        perror("read error after failover");
                        exit(-1);
                }
                for (j = 0; j < bufsize; j++) {
                        if (buf[j] != i) {
                                printf("verify error after failover\n");
                                exit(-1);
                        }
                }
        }

        t_close(fd);
        t_unlink(path);
        LEAVE();
}
Beispiel #10
0
long gethostid (void)
{
  struct sockaddr_in sin;
  int inet = t_open (TLI_TCP, O_RDWR, 0);
  if (inet < 0) return 0;
  getmyinaddr (inet,&sin,sizeof (sin));
  close (inet);
  return sin.sin_addr.s_addr;
}
Beispiel #11
0
value xti_cots_connect (value device, value addr) {
    CAMLparam2(device,addr);
    int fd;
    char *dev;
    struct t_call sndcall;
    char abuf[ABUFLEN];
    int k;

    dev = String_val(device);

    if ((fd = t_open(dev, O_RDWR, (struct t_info *) NULL))
	== -1) {
	xti_error(-1, "t_open");
    }

    if (t_bind(fd, (struct t_bind *) NULL, (struct t_bind *) NULL)
	== -1) {
	xti_error(fd, "t_bind");
    }

    sndcall.opt.buf = NULL;
    sndcall.opt.len = 0;
    sndcall.opt.maxlen = 0;
    sndcall.udata.buf = NULL;
    sndcall.udata.len = 0;
    sndcall.udata.maxlen = 0;
    sndcall.addr.buf = abuf;
    sndcall.addr.len = 0;
    sndcall.addr.maxlen = ABUFLEN;
    sndcall.sequence = 0;

    if (string_length(addr) > sndcall.addr.maxlen) {
	t_close(fd);
	invalid_argument("cots_connect: address too long");
    };

    sndcall.addr.len = string_length(addr);
    for (k=0; k<string_length(addr); k++) {
	sndcall.addr.buf[k] = Byte(addr,k);
    };
    if (t_connect( fd, &sndcall, (struct t_call *) NULL) == -1 ) {
	xti_error(fd, "t_connect");
    }

    if (ioctl(fd, I_PUSH, "tirdwr") == -1) {
	int e = errno;
	t_close(fd);
	unix_error(e, "ioctl(I_PUSH)", Nothing);
    }

    CAMLreturn(Val_int(fd));
}
Beispiel #12
0
int t15(char *name)
{
        char file[MAX_PATH_LENGTH] = "";
        int fd;
        ENTER("open-stat-close");
        snprintf(file, MAX_PATH_LENGTH, "%s/test_t15_file", lustre_path);

        t_touch(file);
        fd = t_open(file);
        t_check_stat(file, NULL);
        t_close(fd);
        t_unlink(file);
        LEAVE();
}
Beispiel #13
0
int
do_connect(const char *host, const char *serv)
{
	int					tfd, i;
	void				*handle;
	struct t_call		tcall;
	struct t_discon		tdiscon;
	struct netconfig	*ncp;
	struct nd_hostserv	hs;
	struct nd_addrlist	*alp;
	struct netbuf		*np;

	handle = Setnetpath();

	hs.h_host = (char *) host;
	hs.h_serv = (char *) serv;

	while ( (ncp = getnetpath(handle)) != NULL) {
		if (strcmp(ncp->nc_netid, "ticotsord") != 0)
			continue;

		if (netdir_getbyname(ncp, &hs, &alp) != 0)
			continue;

				/* try each server address */
		for (i = 0, np = alp->n_addrs; i < alp->n_cnt; i++, np++) {
			printf("device = %s\n", ncp->nc_device);
			if ( (tfd = t_open(ncp->nc_device, O_RDWR, NULL)) < 0)
				err_xti("t_open error for %s", ncp->nc_device);
		
			if (t_bind(tfd, NULL, NULL) < 0)
				err_xti("t_bind error");

			tcall.addr.len  = np->len;
			tcall.addr.buf  = np->buf;	/* pointer copy */
			printf("addr.len = %d\n", tcall.addr.len);
			printf("addr.buf = %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
					tcall.addr.buf[0], tcall.addr.buf[1],
					tcall.addr.buf[2], tcall.addr.buf[3],
					tcall.addr.buf[4], tcall.addr.buf[5],
					tcall.addr.buf[6], tcall.addr.buf[7],
					tcall.addr.buf[8], tcall.addr.buf[9],
					tcall.addr.buf[10]);
		}
		netdir_free(alp, ND_ADDRLIST);
	}
	endnetpath(handle);
	return(-1);
}
Beispiel #14
0
void t6()
{
        char *path="/mnt/lustre/f6";
        int fd;
        ENTRY("open |X| close");

        replay_barrier();
        t_create(path);
        fd = t_open(path);
        sleep(1);
        mds_failover();
        t_check_stat(path, NULL);
        t_close(fd);
        t_unlink(path);
        LEAVE();
}
Beispiel #15
0
enum proto_accept_error
proto_accept_connection(int listener_fd, int *read_fd, int *write_fd,
			const char **name)
{
    int timeout = server_int_option("name_lookup_timeout", 5);
    int fd;
    struct sockaddr_in *addr = (struct sockaddr_in *) call->addr.buf;
    static Stream *s = 0;

    if (!s)
	s = new_stream(100);

    fd = t_open((void *) "/dev/tcp", O_RDWR, 0);
    if (fd < 0) {
	if (t_errno == TSYSERR && errno == EMFILE)
	    return PA_FULL;
	else {
	    log_ti_error("Opening endpoint for new connection");
	    return PA_OTHER;
	}
    }
    if (t_bind(fd, 0, 0) < 0) {
	log_ti_error("Binding endpoint for new connection");
	t_close(fd);
	return PA_OTHER;
    }
    if (t_listen(listener_fd, call) < 0) {
	log_ti_error("Accepting new network connection");
	t_close(fd);
	return PA_OTHER;
    }
    if (t_accept(listener_fd, fd, call) < 0) {
	log_ti_error("Accepting new network connection");
	t_close(fd);
	return PA_OTHER;
    }
    if (!set_rw_able(fd)) {
	t_close(fd);
	return PA_OTHER;
    }
    *read_fd = *write_fd = fd;
    stream_printf(s, "%s, port %d",
		  lookup_name_from_addr(addr, timeout),
		  (int) ntohs(addr->sin_port));
    *name = reset_stream(s);
    return PA_OKAY;
}
Beispiel #16
0
void t0()
{
        const int bufsize = 4096;
        char *path = "/mnt/lustre/rp_ost_t0_file";
        char buf[bufsize];
        int fd, i, j, rc;
        ENTRY("open-failover-write-verification (no ping involved)");

        printf("create/open file...\n");
        t_touch(path);
        fd = t_open(path);
        printf("OST failover...\n");
        replay_barrier();
        mds_failover();

        printf("write file...\n");
        for (i = 0; i < 20; i++) {
                memset(buf, i, bufsize);
                if ((rc = write(fd, buf, bufsize)) != bufsize) {
                        perror("write error after failover");
                        printf("i = %d, rc = %d\n", i, rc);
                        exit(-1);
                }
        }

        /* verify */
        printf("read & verify...\n");
        lseek(fd, 0, SEEK_SET);
        for (i = 0; i < 20; i++) {
                memset(buf, -1, bufsize);
                if ((rc = read(fd, buf, bufsize)) != bufsize) {
                        perror("read error rc");
                        printf("i = %d, rc = %d\n", i, rc);
                        exit(-1);
                }
                for (j = 0; j < bufsize; j++) {
                        if (buf[j] != i) {
                                printf("verify error!\n");
                                exit(-1);
                        }
                }
        }
        t_close(fd);
        t_unlink(path);
        LEAVE();
}
Beispiel #17
0
/*
 * Create the client des authentication object. Obsoleted by
 * authdes_seccreate().
 */
AUTH *
authdes_create(char *servername, uint_t window, struct sockaddr_in *syncaddr,
	des_block *ckey)
{
	char *hostname = NULL;

	if (syncaddr) {
		/*
		 * Change addr to hostname, because that is the way
		 * new interface takes it.
		 */
		struct netconfig *nconf;
		struct netbuf nb_syncaddr;
		struct nd_hostservlist *hlist;
		AUTH *nauth;
		int fd;
		struct t_info tinfo;

		if ((nconf = __rpc_getconfip("udp")) == NULL &&
		    (nconf = __rpc_getconfip("tcp")) == NULL)
			goto fallback;

		/* Transform sockaddr_in to netbuf */
		if ((fd = t_open(nconf->nc_device, O_RDWR, &tinfo)) == -1) {
			(void) freenetconfigent(nconf);
			goto fallback;
		}
		(void) t_close(fd);
		nb_syncaddr.maxlen = nb_syncaddr.len =
			__rpc_get_a_size(tinfo.addr);
		nb_syncaddr.buf = (char *)syncaddr;
		if (netdir_getbyaddr(nconf, &hlist, &nb_syncaddr)) {
			(void) freenetconfigent(nconf);
			goto fallback;
		}
		if (hlist && hlist->h_cnt > 0 && hlist->h_hostservs)
			hostname = hlist->h_hostservs->h_host;
		nauth = authdes_seccreate(servername, window, hostname, ckey);
		(void) netdir_free((char *)hlist, ND_HOSTSERVLIST);
		(void) freenetconfigent(nconf);
		return (nauth);
	}
fallback:
	return (authdes_seccreate(servername, window, hostname, ckey));
}
Beispiel #18
0
/*
 * A common server create routine
 */
static SVCXPRT *
svc_com_create(int fd, uint_t sendsize, uint_t recvsize, char *netid)
{
	struct netconfig *nconf;
	SVCXPRT *svc;
	int madefd = FALSE;
	int port;
	int res;

	if ((nconf = __rpc_getconfip(netid)) == NULL) {
		(void) syslog(LOG_ERR, "Could not get %s transport", netid);
		return (NULL);
	}
	if (fd == RPC_ANYSOCK) {
		fd = t_open(nconf->nc_device, O_RDWR, NULL);
		if (fd == -1) {
			char errorstr[100];

			__tli_sys_strerror(errorstr, sizeof (errorstr),
					t_errno, errno);
			(void) syslog(LOG_ERR,
			"svc%s_create: could not open connection : %s", netid,
				    errorstr);
			(void) freenetconfigent(nconf);
			return (NULL);
		}
		madefd = TRUE;
	}

	res = __rpc_bindresvport(fd, NULL, &port, 8);
	svc = svc_tli_create(fd, nconf, NULL,
				sendsize, recvsize);
	(void) freenetconfigent(nconf);
	if (svc == NULL) {
		if (madefd)
			(void) t_close(fd);
		return (NULL);
	}
	if (res == -1)
		/* LINTED pointer cast */
		port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
	svc->xp_port = ntohs(port);
	return (svc);
}
Beispiel #19
0
int op_read(const int extra_flag)
{
  // open
  int fd = t_open(tfs_name, ".jpg", T_READ|extra_flag, NULL);

  if (fd <= 0)
  {
    printf("get fd for read fail, ret: %d \n", fd);
    return TFS_ERROR;
  }

  // read
  int64_t len = 0;
  char buf[OP_SIZE];

  if ((len = t_read(fd, buf, OP_SIZE/2)) < 0)
  {
    printf("read data fail, ret: %" PRI64_PREFIX "d\n", len);
    return TFS_ERROR;
  }

  // lseek then read
  t_lseek(fd, OP_SIZE/3, T_SEEK_CUR);
  // read
  if ((len = t_read(fd, buf, OP_SIZE/2)) < 0)
  {
    printf("lseek and read data fail, ret: %" PRI64_PREFIX "d\n", len);
    return TFS_ERROR;
  }

  // pread
  if ((len = t_pread(fd, buf, OP_SIZE/2, OP_SIZE/3)) < 0)
  {
    printf("pread data fail, ret: %" PRI64_PREFIX "d\n", len);
    return TFS_ERROR;
  }

  // close
  t_close(fd, NULL, 0);

  return TFS_SUCCESS;
}
Beispiel #20
0
static XtransConnInfo
TRANS(TLIOpen)(char *device)

{
    XtransConnInfo	ciptr;

    prmsg(3,"TLIOpen(%s)\n", device);

    if( (ciptr = calloc(1,sizeof(struct _XtransConnInfo))) == NULL )
    {
	prmsg(1, "TLIOpen: calloc failed\n");
	return NULL;
    }

    if( (ciptr->fd=t_open( device, O_RDWR, NULL )) < 0 )
    {
	prmsg(1, "TLIOpen: t_open failed for %s\n", device);
	free(ciptr);
	return NULL;
    }

    return ciptr;
}
Beispiel #21
0
int accept_client(int fd, struct t_call *call)
{
	int sd;

	if ((sd = t_open(DEV_XTI, O_RDWR, NULL)) < 0)
		error("serwer->accept_client->t_open", fd, sd);

	if (t_bind(sd, NULL, NULL) < 0)
		error("serwer->accept_client->t_bind", fd, sd);

	if (t_accept(fd, sd, call) < 0) {
		if (t_errno == TLOOK) {
			if (t_rcvdis(fd, NULL) < 0)
				error("serwer->accept_client->t_rcvdis", fd, sd);

			if (t_close(sd) < 0)
				error("serwer->accept_client->t_close", fd, sd);

			return -1;
		}
		error("t_accept failed", fd, sd);
	}
	return(sd);
}
Beispiel #22
0
int op_stat(const int extra_flag)
{
  int fd = t_open(tfs_name, NULL, T_STAT|extra_flag, NULL);

  if (fd <= 0)
  {
    printf("get fd for stat fail, ret: %d\n", fd);
    return TFS_ERROR;
  }

  TfsFileStat stat_info;
  int ret = t_fstat(fd, &stat_info, NORMAL_STAT);
  if (ret != TFS_SUCCESS)
  {
    printf("stat tfs file, ret: %d\n", ret);
  }
  else
  {
    printf("stat tfs file success:\n"
           "filename: %s\n"
           "fileid: %" PRI64_PREFIX "u\n"
           "offset: %d\n"
           "size: %" PRI64_PREFIX "d\n"
           "usize: %" PRI64_PREFIX "d\n"
           "modify_time: %d\n"
           "create_time: %d\n"
           "status: %d\n"
           "crc: %u\n",
           tfs_name, stat_info.file_id_, stat_info.offset_, stat_info.size_, stat_info.usize_,
           stat_info.modify_time_, stat_info.create_time_, stat_info.flag_, stat_info.crc_);
  }

  t_close(fd, NULL, 0);

  return TFS_SUCCESS;
}
Beispiel #23
0
/*
 * This routine is designed to be able to "ping"
 * a list of hosts and create a list of responding
 * hosts sorted by response time.
 * This must be done without any prior
 * contact with the host - therefore the "ping"
 * must be to a "well-known" address.  The outstanding
 * candidate here is the address of "rpcbind".
 *
 * A response to a ping is no guarantee that the host
 * is running NFS, has a mount daemon, or exports
 * the required filesystem.  If the subsequent
 * mount attempt fails then the host will be marked
 * "ignore" and the host list will be re-pinged
 * (sans the bad host). This process continues
 * until a successful mount is achieved or until
 * there are no hosts left to try.
 */
enum clnt_stat
nfs_cast(struct mapfs *mfs_in, struct mapfs **mfs_out, int timeout)
{
	enum clnt_stat stat;
	AUTH *sys_auth = authsys_create_default();
	XDR xdr_stream;
	register XDR *xdrs = &xdr_stream;
	int outlen;
	int if_inx;
	int tsec;
	int flag;
	int sent, addr_cnt, rcvd, if_cnt;
	fd_set readfds, mask;
	register ulong_t xid;		/* xid - unique per addr */
	register int i;
	struct rpc_msg msg;
	struct timeval t, rcv_timeout;
	char outbuf[UDPMSGSIZE], inbuf[UDPMSGSIZE];
	struct t_unitdata t_udata, t_rdata;
	struct nd_hostserv hs;
	struct nd_addrlist *retaddrs;
	struct transp *tr_head;
	struct transp *trans, *prev_trans;
	struct addrs *a, *prev_addr;
	struct tstamps *ts, *prev_ts;
	NCONF_HANDLE *nc = NULL;
	struct netconfig *nconf;
	struct rlimit rl;
	int dtbsize;
	struct mapfs *mfs;

	/*
	 * For each connectionless transport get a list of
	 * host addresses.  Any single host may have
	 * addresses on several transports.
	 */
	addr_cnt = sent = rcvd = 0;
	tr_head = NULL;
	FD_ZERO(&mask);

	/*
	 * Set the default select size to be the maximum FD_SETSIZE, unless
	 * the current rlimit is lower.
	 */
	dtbsize = FD_SETSIZE;
	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
		if (rl.rlim_cur < FD_SETSIZE)
			dtbsize = rl.rlim_cur;
	}

	prev_trans = NULL;
	prev_addr = NULL;
	prev_ts = NULL;
	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {

		if (trace > 2)
			trace_prt(1, "nfs_cast: host=%s\n", mfs->mfs_host);

		nc = setnetconfig();
		if (nc == NULL) {
			stat = RPC_CANTSEND;
			goto done_broad;
		}
		while (nconf = getnetconfig(nc)) {
			if (!(nconf->nc_flag & NC_VISIBLE) ||
			    nconf->nc_semantics != NC_TPI_CLTS ||
			    (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0))
				continue;
			trans = (struct transp *)malloc(sizeof (*trans));
			if (trans == NULL) {
				syslog(LOG_ERR, "no memory");
				stat = RPC_CANTSEND;
				goto done_broad;
			}
			(void) memset(trans, 0, sizeof (*trans));
			if (tr_head == NULL)
				tr_head = trans;
			else
				prev_trans->tr_next = trans;
			prev_trans = trans;

			trans->tr_fd = t_open(nconf->nc_device, O_RDWR, NULL);
			if (trans->tr_fd < 0) {
				syslog(LOG_ERR, "nfscast: t_open: %s:%m",
					nconf->nc_device);
				stat = RPC_CANTSEND;
				goto done_broad;
			}
			if (t_bind(trans->tr_fd, (struct t_bind *)NULL,
				(struct t_bind *)NULL) < 0) {
				syslog(LOG_ERR, "nfscast: t_bind: %m");
				stat = RPC_CANTSEND;
				goto done_broad;
			}
			trans->tr_taddr =
				/* LINTED pointer alignment */
			(struct t_bind *)t_alloc(trans->tr_fd, T_BIND, T_ADDR);
			if (trans->tr_taddr == (struct t_bind *)NULL) {
				syslog(LOG_ERR, "nfscast: t_alloc: %m");
				stat = RPC_SYSTEMERROR;
				goto done_broad;
			}

			trans->tr_device = nconf->nc_device;
			FD_SET(trans->tr_fd, &mask);

			if_inx = 0;
			hs.h_host = mfs->mfs_host;
			hs.h_serv = "rpcbind";
			if (netdir_getbyname(nconf, &hs, &retaddrs) == ND_OK) {

				/*
				 * If mfs->ignore is previously set for
				 * this map, clear it. Because a host can
				 * have either v6 or v4 address
				 */
				if (mfs->mfs_ignore == 1)
					mfs->mfs_ignore = 0;

				a = (struct addrs *)malloc(sizeof (*a));
				if (a == NULL) {
					syslog(LOG_ERR, "no memory");
					stat = RPC_CANTSEND;
					goto done_broad;
				}
				(void) memset(a, 0, sizeof (*a));
				if (trans->tr_addrs == NULL)
					trans->tr_addrs = a;
				else
					prev_addr->addr_next = a;
				prev_addr = a;
				a->addr_if_tstamps = NULL;
				a->addr_mfs = mfs;
				a->addr_addrs = retaddrs;
				if_cnt = retaddrs->n_cnt;
				while (if_cnt--) {
					ts = (struct tstamps *)
						malloc(sizeof (*ts));
					if (ts == NULL) {
						syslog(LOG_ERR, "no memory");
						stat = RPC_CANTSEND;
						goto done_broad;
					}
					(void) memset(ts, 0, sizeof (*ts));
					ts->ts_penalty = mfs->mfs_penalty;
					if (a->addr_if_tstamps == NULL)
						a->addr_if_tstamps = ts;
					else
						prev_ts->ts_next = ts;
					prev_ts = ts;
					ts->ts_inx = if_inx++;
					addr_cnt++;
				}
				break;
			} else {
				mfs->mfs_ignore = 1;
				if (verbose)
					syslog(LOG_ERR,
				"%s:%s address not known",
				mfs->mfs_host,
				strcmp(nconf->nc_proto, NC_INET)?"IPv6":"IPv4");
			}
		} /* while */

		endnetconfig(nc);
		nc = NULL;
	} /* for */
	if (addr_cnt == 0) {
		syslog(LOG_ERR, "nfscast: couldn't find addresses");
		stat = RPC_CANTSEND;
		goto done_broad;
	}

	(void) gettimeofday(&t, (struct timezone *)0);
	xid = (getpid() ^ t.tv_sec ^ t.tv_usec) & ~0xFF;
	t.tv_usec = 0;

	/* serialize the RPC header */

	msg.rm_direction = CALL;
	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
	msg.rm_call.cb_prog = RPCBPROG;
	/*
	 * we can not use RPCBVERS here since it doesn't exist in 4.X,
	 * the fix to bug 1139883 has made the 4.X portmapper silent to
	 * version mismatches. This causes the RPC call to the remote
	 * portmapper to simply be ignored if it's not Version 2.
	 */
	msg.rm_call.cb_vers = PMAPVERS;
	msg.rm_call.cb_proc = NULLPROC;
	if (sys_auth == (AUTH *)NULL) {
		stat = RPC_SYSTEMERROR;
		goto done_broad;
	}
	msg.rm_call.cb_cred = sys_auth->ah_cred;
	msg.rm_call.cb_verf = sys_auth->ah_verf;
	xdrmem_create(xdrs, outbuf, sizeof (outbuf), XDR_ENCODE);
	if (! xdr_callmsg(xdrs, &msg)) {
		stat = RPC_CANTENCODEARGS;
		goto done_broad;
	}
	outlen = (int)xdr_getpos(xdrs);
	xdr_destroy(xdrs);

	t_udata.opt.len = 0;
	t_udata.udata.buf = outbuf;
	t_udata.udata.len = outlen;

	/*
	 * Basic loop: send packet to all hosts and wait for response(s).
	 * The response timeout grows larger per iteration.
	 * A unique xid is assigned to each address in order to
	 * correctly match the replies.
	 */
	for (tsec = 4; timeout > 0; tsec *= 2) {

		timeout -= tsec;
		if (timeout <= 0)
			tsec += timeout;

		rcv_timeout.tv_sec = tsec;
		rcv_timeout.tv_usec = 0;

		sent = 0;
		for (trans = tr_head; trans; trans = trans->tr_next) {
			for (a = trans->tr_addrs; a; a = a->addr_next) {
				struct netbuf *if_netbuf =
					a->addr_addrs->n_addrs;
				ts = a->addr_if_tstamps;
				if_cnt = a->addr_addrs->n_cnt;
				while (if_cnt--) {

					/*
					 * xid is the first thing in
					 * preserialized buffer
					 */
					/* LINTED pointer alignment */
					*((ulong_t *)outbuf) =
						htonl(xid + ts->ts_inx);
					(void) gettimeofday(&(ts->ts_timeval),
						(struct timezone *)0);
					/*
					 * Check if already received
					 * from a previous iteration.
					 */
					if (ts->ts_rcvd) {
						sent++;
						ts = ts->ts_next;
						continue;
					}

					t_udata.addr = *if_netbuf++;

					if (t_sndudata(trans->tr_fd,
							&t_udata) == 0) {
						sent++;
					}

					ts = ts->ts_next;
				}
			}
		}
		if (sent == 0) {		/* no packets sent ? */
			stat = RPC_CANTSEND;
			goto done_broad;
		}

		/*
		 * Have sent all the packets.  Now collect the responses...
		 */
		rcvd = 0;
	recv_again:
		msg.acpted_rply.ar_verf = _null_auth;
		msg.acpted_rply.ar_results.proc = xdr_void;
		readfds = mask;

		switch (select(dtbsize, &readfds,
			(fd_set *)NULL, (fd_set *)NULL, &rcv_timeout)) {

		case 0: /* Timed out */
			/*
			 * If we got at least one response in the
			 * last interval, then don't wait for any
			 * more.  In theory we should wait for
			 * the max weighting (penalty) value so
			 * that a very slow server has a chance to
			 * respond but this could take a long time
			 * if the admin has set a high weighting
			 * value.
			 */
			if (rcvd > 0)
				goto done_broad;

			stat = RPC_TIMEDOUT;
			continue;

		case -1:  /* some kind of error */
			if (errno == EINTR)
				goto recv_again;
			syslog(LOG_ERR, "nfscast: select: %m");
			if (rcvd == 0)
				stat = RPC_CANTRECV;
			goto done_broad;

		}  /* end of select results switch */

		for (trans = tr_head; trans; trans = trans->tr_next) {
			if (FD_ISSET(trans->tr_fd, &readfds))
				break;
		}
		if (trans == NULL)
			goto recv_again;

	try_again:
		t_rdata.addr = trans->tr_taddr->addr;
		t_rdata.udata.buf = inbuf;
		t_rdata.udata.maxlen = sizeof (inbuf);
		t_rdata.udata.len = 0;
		t_rdata.opt.len = 0;
		if (t_rcvudata(trans->tr_fd, &t_rdata, &flag) < 0) {
			if (errno == EINTR)
				goto try_again;
			syslog(LOG_ERR, "nfscast: t_rcvudata: %s:%m",
				trans->tr_device);
			stat = RPC_CANTRECV;
			continue;
		}
		if (t_rdata.udata.len < sizeof (ulong_t))
			goto recv_again;
		if (flag & T_MORE) {
			syslog(LOG_ERR,
				"nfscast: t_rcvudata: %s: buffer overflow",
				trans->tr_device);
			goto recv_again;
		}

		/*
		 * see if reply transaction id matches sent id.
		 * If so, decode the results.
		 * Note: received addr is ignored, it could be
		 * different from the send addr if the host has
		 * more than one addr.
		 */
		xdrmem_create(xdrs, inbuf, (uint_t)t_rdata.udata.len,
								XDR_DECODE);
		if (xdr_replymsg(xdrs, &msg)) {
		    if (msg.rm_reply.rp_stat == MSG_ACCEPTED &&
			(msg.rm_xid & ~0xFF) == xid) {
			struct addrs *curr_addr;

			i = msg.rm_xid & 0xFF;
			for (curr_addr = trans->tr_addrs; curr_addr;
			    curr_addr = curr_addr->addr_next) {
			    for (ts = curr_addr->addr_if_tstamps; ts;
				ts = ts->ts_next)
				if (ts->ts_inx == i && !ts->ts_rcvd) {
					ts->ts_rcvd = 1;
					calc_resp_time(&ts->ts_timeval);
					stat = RPC_SUCCESS;
					rcvd++;
					break;
				}
			}
		    } /* otherwise, we just ignore the errors ... */
		}
		xdrs->x_op = XDR_FREE;
		msg.acpted_rply.ar_results.proc = xdr_void;
		(void) xdr_replymsg(xdrs, &msg);
		XDR_DESTROY(xdrs);
		if (rcvd == sent)
			goto done_broad;
		else
			goto recv_again;
	}
	if (!rcvd)
		stat = RPC_TIMEDOUT;

done_broad:
	if (rcvd) {
		*mfs_out = sort_responses(tr_head);
		stat = RPC_SUCCESS;
	}
	if (nc)
		endnetconfig(nc);
	free_transports(tr_head);
	AUTH_DESTROY(sys_auth);
	return (stat);
}
Beispiel #24
0
static void *udpfs_open(vfs_handler_t * vfs, const char *fn, int flags) {
	return (void*)t_open(fn, flags, 1);
}
Beispiel #25
0
/*
 * Create the autofs service for amd
 */
int
create_autofs_service(void)
{
  struct t_bind *tbp = 0;
  int fd = -1, err = 1;		/* assume failed */

  plog(XLOG_INFO, "creating autofs service listener");
  autofs_ncp = getnetconfigent(autofs_conftype);
  if (autofs_ncp == NULL) {
    plog(XLOG_ERROR, "create_autofs_service: cannot getnetconfigent for %s", autofs_conftype);
    goto out;
  }

  fd = t_open(autofs_ncp->nc_device, O_RDWR, NULL);
  if (fd < 0) {
    plog(XLOG_ERROR, "create_autofs_service: t_open failed (%s)",
	 t_errlist[t_errno]);
    goto out;
  }

  tbp = (struct t_bind *) t_alloc(fd, T_BIND, T_ADDR);
  if (!tbp) {
    plog(XLOG_ERROR, "create_autofs_service: t_alloca failed");
    goto out;
  }

  if (get_autofs_address(autofs_ncp, tbp) != 0) {
    plog(XLOG_ERROR, "create_autofs_service: get_autofs_address failed");
    goto out;
  }

  autofs_xprt = svc_tli_create(fd, autofs_ncp, tbp, 0, 0);
  if (autofs_xprt == NULL) {
    plog(XLOG_ERROR, "cannot create autofs tli service for amd");
    goto out;
  }

  rpcb_unset(AUTOFS_PROG, AUTOFS_VERS, autofs_ncp);
  if (svc_reg(autofs_xprt, AUTOFS_PROG, AUTOFS_VERS, autofs_program_1, autofs_ncp) == FALSE) {
    plog(XLOG_ERROR, "could not register amd AUTOFS service");
    goto out;
  }
  err = 0;
  goto really_out;

out:
  if (autofs_ncp)
    freenetconfigent(autofs_ncp);
  if (autofs_xprt)
    SVC_DESTROY(autofs_xprt);
  else {
    if (fd > 0)
      t_close(fd);
  }

really_out:
  if (tbp)
    t_free((char *) tbp, T_BIND);

  dlog("create_autofs_service: returning %d\n", err);
  return err;
}
Beispiel #26
0
enum error
proto_open_connection(Var arglist, int *read_fd, int *write_fd,
		      const char **local_name, const char **remote_name)
{
    /* These are `static' rather than `volatile' because I can't cope with
     * getting all those nasty little parameter-passing rules right.  This
     * function isn't recursive anyway, so it doesn't matter.
     */
    struct sockaddr_in rec_addr;
    struct t_bind received;
    static const char *host_name;
    static int port;
    static Timer_ID id;
    int fd, result;
    int timeout = server_int_option("name_lookup_timeout", 5);
    static struct sockaddr_in addr;
    static Stream *st1 = 0, *st2 = 0;

    if (!st1) {
	st1 = new_stream(20);
	st2 = new_stream(50);
    }
    if (arglist.v.list[0].v.num != 2)
	return E_ARGS;
    else if (arglist.v.list[1].type != TYPE_STR ||
	     arglist.v.list[2].type != TYPE_INT)
	return E_TYPE;

    host_name = arglist.v.list[1].v.str;
    port = arglist.v.list[2].v.num;

    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = lookup_addr_from_name(host_name, timeout);
    if (addr.sin_addr.s_addr == 0)
	return E_INVARG;

    /* Cast to (void *) here to workaround const-less decls on some systems. */
    fd = t_open((void *) "/dev/tcp", O_RDWR, 0);
    if (fd < 0) {
	if (t_errno != TSYSERR || errno != EMFILE)
	    log_ti_error("Making endpoint in proto_open_connection");
	return E_QUOTA;
    }
    received.addr.maxlen = sizeof(rec_addr);
    received.addr.len = sizeof(rec_addr);
    received.addr.buf = (void *) &rec_addr;

    if (t_bind(fd, 0, &received) < 0) {
	log_ti_error("Binding outbound endpoint");
	t_close(fd);
	return E_QUOTA;
    }
    call->addr.maxlen = sizeof(addr);
    call->addr.len = sizeof(addr);
    call->addr.buf = (void *) &addr;

    TRY
	id = set_timer(server_int_option("outbound_connect_timeout", 5),
		       timeout_proc, 0);
    result = t_connect(fd, call, 0);
    cancel_timer(id);
    EXCEPT(timeout_exception)
	result = -1;
    errno = ETIMEDOUT;
    t_errno = TSYSERR;
    reenable_timers();
    ENDTRY

	if (result < 0) {
	t_close(fd);
	log_ti_error("Connecting in proto_open_connection");
	return E_QUOTA;
    }
    if (!set_rw_able(fd)) {
	t_close(fd);
	return E_QUOTA;
    }
    *read_fd = *write_fd = fd;

    stream_printf(st1, "port %d", (int) ntohs(rec_addr.sin_port));
    *local_name = reset_stream(st1);

    stream_printf(st2, "%s, port %d", host_name, port);
    *remote_name = reset_stream(st2);

    return E_NONE;
}
Beispiel #27
0
enum error
proto_make_listener(Var desc, int *fd, Var * canon, const char **name)
{
    struct sockaddr_in req_addr, rec_addr;
    struct t_bind requested, received;
    int s, port;
    static Stream *st = 0;

    if (!st)
	st = new_stream(20);

    if (desc.type != TYPE_INT)
	return E_TYPE;

    port = desc.v.num;
    s = t_open((void *) "/dev/tcp", O_RDWR, 0);
    if (s < 0) {
	log_ti_error("Creating listening endpoint");
	return E_QUOTA;
    }
    req_addr.sin_family = AF_INET;
    req_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    req_addr.sin_port = htons(port);

    requested.addr.maxlen = sizeof(req_addr);
    requested.addr.len = sizeof(req_addr);
    requested.addr.buf = (void *) &req_addr;
    requested.qlen = 5;

    received.addr.maxlen = sizeof(rec_addr);
    received.addr.len = sizeof(rec_addr);
    received.addr.buf = (void *) &rec_addr;

    if (t_bind(s, &requested, &received) < 0) {
	enum error e = E_QUOTA;

	log_ti_error("Binding to listening address");
	t_close(s);
	if (t_errno == TACCES || (t_errno == TSYSERR && errno == EACCES))
	    e = E_PERM;
	return e;
    } else if (port != 0 && rec_addr.sin_port != port) {
	errlog("Can't bind to requested port!\n");
	t_close(s);
	return E_QUOTA;
    }
    if (!call)
	call = (struct t_call *) t_alloc(s, T_CALL, T_ADDR);
    if (!call) {
	log_ti_error("Allocating T_CALL structure");
	t_close(s);
	return E_QUOTA;
    }
    canon->type = TYPE_INT;
    canon->v.num = ntohs(rec_addr.sin_port);

    stream_printf(st, "port %d", canon->v.num);
    *name = reset_stream(st);

    *fd = s;
    return E_NONE;
}
Beispiel #28
0
/*
 * Called to create and prepare a transport descriptor for in-kernel
 * RPC service.
 * Returns -1 on failure and a valid descriptor on success.
 */
int
nfslib_transport_open(struct netconfig *nconf)
{
	int fd;
	struct strioctl	strioc;

	if ((nconf == (struct netconfig *)NULL) ||
	    (nconf->nc_device == (char *)NULL)) {
		syslog(LOG_ERR, "no netconfig device");
		return (-1);
	}

	/*
	 * Open the transport device.
	 */
	fd = t_open(nconf->nc_device, O_RDWR, (struct t_info *)NULL);
	if (fd == -1) {
		if (t_errno == TSYSERR && errno == EMFILE &&
		    (nofile_increase(0) == 0)) {
			/* Try again with a higher NOFILE limit. */
			fd = t_open(nconf->nc_device, O_RDWR,
			    (struct t_info *)NULL);
		}
		if (fd == -1) {
			syslog(LOG_ERR, "t_open %s failed:  t_errno %d, %m",
			    nconf->nc_device, t_errno);
			return (-1);
		}
	}

	/*
	 * Pop timod because the RPC module must be as close as possible
	 * to the transport.
	 */
	if (ioctl(fd, I_POP, 0) < 0) {
		syslog(LOG_ERR, "I_POP of timod failed: %m");
		(void) t_close(fd);
		return (-1);
	}

	/*
	 * Common code for CLTS and COTS transports
	 */
	if (ioctl(fd, I_PUSH, "rpcmod") < 0) {
		syslog(LOG_ERR, "I_PUSH of rpcmod failed: %m");
		(void) t_close(fd);
		return (-1);
	}

	strioc.ic_cmd = RPC_SERVER;
	strioc.ic_dp = (char *)0;
	strioc.ic_len = 0;
	strioc.ic_timout = -1;

	/* Tell rpcmod to act like a server stream. */
	if (ioctl(fd, I_STR, &strioc) < 0) {
		syslog(LOG_ERR, "rpcmod set-up ioctl failed: %m");
		(void) t_close(fd);
		return (-1);
	}

	/*
	 * Re-push timod so that we will still be doing TLI
	 * operations on the descriptor.
	 */
	if (ioctl(fd, I_PUSH, "timod") < 0) {
		syslog(LOG_ERR, "I_PUSH of timod failed: %m");
		(void) t_close(fd);
		return (-1);
	}

	/*
	 * Enable options of returning the ip's for udp.
	 */
	if (strcmp(nconf->nc_netid, "udp6") == 0)
		__rpc_tli_set_options(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, 1);
	else if (strcmp(nconf->nc_netid, "udp") == 0)
		__rpc_tli_set_options(fd, IPPROTO_IP, IP_RECVDSTADDR, 1);

	return (fd);
}
Beispiel #29
0
void c_SQLite3::t___construct(const String& filename,
                       int64_t flags /* = k_SQLITE3_OPEN_READWRITE |
                                      k_SQLITE3_OPEN_CREATE */,
                       const String& encryption_key /* = null_string */) {
  t_open(filename, flags, encryption_key);
}
Beispiel #30
0
static void
get_xdmcp_sock(void)
{
#ifdef STREAMSCONN
    struct netconfig *nconf;

    if ((xdmcpSocket = t_open("/dev/udp", O_RDWR, 0)) < 0) {
        XdmcpWarning("t_open() of /dev/udp failed");
        return;
    }

    if (t_bind(xdmcpSocket, NULL, NULL) < 0) {
        XdmcpWarning("UDP socket creation failed");
        t_error("t_bind(xdmcpSocket) failed");
        t_close(xdmcpSocket);
        return;
    }

    /*
     * This part of the code looks contrived. It will actually fit in nicely
     * when the CLTS part of Xtrans is implemented.
     */

    if ((nconf = getnetconfigent("udp")) == NULL) {
        XdmcpWarning("UDP socket creation failed: getnetconfigent()");
        t_unbind(xdmcpSocket);
        t_close(xdmcpSocket);
        return;
    }

    if (netdir_options(nconf, ND_SET_BROADCAST, xdmcpSocket, NULL)) {
        XdmcpWarning("UDP set broadcast option failed: netdir_options()");
        freenetconfigent(nconf);
        t_unbind(xdmcpSocket);
        t_close(xdmcpSocket);
        return;
    }

    freenetconfigent(nconf);
#else
    int soopts = 1;

#if defined(IPv6) && defined(AF_INET6)
    if ((xdmcpSocket6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
        XdmcpWarning("INET6 UDP socket creation failed");
#endif
    if ((xdmcpSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
        XdmcpWarning("UDP socket creation failed");
#ifdef SO_BROADCAST
    else if (setsockopt(xdmcpSocket, SOL_SOCKET, SO_BROADCAST, (char *) &soopts,
                        sizeof(soopts)) < 0)
        XdmcpWarning("UDP set broadcast socket-option failed");
#endif                          /* SO_BROADCAST */
    if (xdmcpSocket >= 0 && xdm_from != NULL) {
        if (bind(xdmcpSocket, (struct sockaddr *) &FromAddress,
                 FromAddressLen) < 0) {
            FatalError("Xserver: failed to bind to -from address: %s\n",
                       xdm_from);
        }
    }
#endif                          /* STREAMSCONN */
}