Пример #1
0
int tcp_conn_send(struct tcp_connection *c)
{
	long response[2];
	int n;

	/* inform TCP main about this new connection */
	if (c->state==S_CONN_CONNECTING) {
		response[0]=(long)c;
		response[1]=ASYNC_CONNECT;
		n=send_fd(unix_tcp_sock, response, sizeof(response), c->s);
		if (n<=0) {
			LM_ERR("Failed to send the socket to main for async connection\n");
			goto error;
		}
		close(c->s);
	} else {
		response[0]=(long)c;
		response[1]=CONN_NEW;
		n=send_fd(unix_tcp_sock, response, sizeof(response), c->s);
		if (n<=0){
			LM_ERR("failed send_fd: %s (%d)\n", strerror(errno), errno);
			goto error;
		}
	}

	return 0;
error:
	_tcpconn_rm(c);
	tcp_connections_no--;
	return -1;
}
Пример #2
0
int main(int argc, char *argv[])
{
    while (1) {
        pid_t child;

        child = fork();
        if (child == -1)
            exit(EXIT_FAILURE);

        if (child == 0) {
            int fd[2];
            int i;

            if (socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fd) == -1)
                goto out_error;

            for (i = 0; i < 100; ++i) {
                if (send_fd(fd[0], fd[0]) == -1)
                    goto out_error;

                if (send_fd(fd[1], fd[1]) == -1)
                    goto out_error;
            }

            close(fd[0]);
            close(fd[1]);
            goto out;

out_error:
            fprintf(stderr, "error: %s\n", strerror(errno));
out:
            exit(EXIT_SUCCESS);
        }

        while (1) {
            pid_t kid;
            int status;

            kid = wait(&status);
            if (kid == -1) {
                if (errno == ECHILD)
                    break;
                if (errno == EINTR)
                    continue;

                exit(EXIT_FAILURE);
            }

            if (WIFEXITED(status)) {
                if (WEXITSTATUS(status))
                    exit(WEXITSTATUS(status));
                break;
            }
        }
    }

    return EXIT_SUCCESS;
}
Пример #3
0
int kdbus_test_fd_passing(struct kdbus_test_env *env)
{
	struct kdbus_conn *conn_src, *conn_dst;
	const char *str = "stackenblocken";
	const struct kdbus_item *item;
	struct kdbus_msg *msg;
	unsigned int i;
	int fds[2];
	int ret;

	/* create two connections */
	conn_src = kdbus_hello(env->buspath, 0, NULL, 0);
	conn_dst = kdbus_hello(env->buspath, 0, NULL, 0);
	ASSERT_RETURN(conn_src && conn_dst);

	/*
	 * Try to ass the handle of a connection as message payload.
	 * This must fail.
	 */
	ret = send_fd(conn_src, conn_dst->id, conn_src->fd);
	ASSERT_RETURN(ret == -ELOOP);

	ret = send_fd(conn_src, conn_dst->id, conn_dst->fd);
	ASSERT_RETURN(ret == -ELOOP);

	ret = pipe(fds);
	ASSERT_RETURN(ret == 0);

	i = write(fds[1], str, strlen(str));
	ASSERT_RETURN(i == strlen(str));

	ret = send_fd(conn_src, conn_dst->id, fds[0]);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_msg_recv(conn_dst, &msg);
	ASSERT_RETURN(ret == 0);

	KDBUS_ITEM_FOREACH(item, msg, items) {
		if (item->type == KDBUS_ITEM_FDS) {
			char tmp[14];
			int nfds = (item->size - KDBUS_ITEM_HEADER_SIZE) /
					sizeof(int);

			ASSERT_RETURN(nfds == 1);

			i = read(item->fds[0], tmp, sizeof(tmp));
			ASSERT_RETURN(i == sizeof(tmp));
			ASSERT_RETURN(memcmp(tmp, str, sizeof(tmp)) == 0);

			close(item->fds[0]);
			break;
		}
	}

	return TEST_OK;
}
Пример #4
0
/*
 * sends stdin and stdout over the socket
 * in that order, returns 0 on success
 */
static int socket_send_sdtio( int fd ) {
    if ( send_fd ( fd, STDIN_FILENO ) < 0  ) {
        LOGE ( "sending STDIN failed\n" );
        return -1;
    }

    if ( send_fd ( fd, STDOUT_FILENO ) < 0 ) {
        LOGE ( "sending STDOUT failed\n" );
        return -1;
    }
    return 0;
}
Пример #5
0
void
handle_request(char *buf, int nread, int fd)
{
	int		newfd;

	if (buf[nread-1] != 0) {
		snprintf(errmsg, MAXLINE-1,
		  "request not null terminated: %*.*s\n", nread, nread, buf);
		send_err(fd, -1, errmsg);
		return;
	}
	if (buf_args(buf, cli_args) < 0) {	/* 将客户进程请求分解成标准argv型的参数表,调用cli_args处理客户进程的参数*/
		send_err(fd, -1, errmsg);
		return;
	}
	if ((newfd = open(pathname, oflag)) < 0) { // 打开相应文件
		snprintf(errmsg, MAXLINE-1, "can't open %s: %s\n", pathname,
		  strerror(errno));
		send_err(fd, -1, errmsg);
		return;
	}
	if (send_fd(fd, newfd) < 0)		/* 经由fd管道将描述符回送给客户进程*/
		err_sys("send_fd error");
	close(newfd);		/* we're done with descriptor */
}
Пример #6
0
int
do_snapshot(int parent_fd)
{
	int fds[2];
	struct command_response resp;
	long child;
	int r;

	r = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
	if (r < 0)
		VG_(tool_panic)((Char *)"error creating socket pair");
	child = my_fork();
	if (child < 0)
		VG_(tool_panic)((Char *)"forking snapshot");
	if (child == 0) {
		VG_(close)(fds[1]);
		VG_(close)(parent_fd);
		return fds[0];
	}
	VG_(close)(fds[0]);
	resp.res = 0;
	safeish_write(parent_fd, &resp, sizeof(resp));
	send_fd(parent_fd, fds[1]);
	VG_(close)(fds[1]);
	return parent_fd;
}
Пример #7
0
void on_set(int sfd){
	int client_sfd = _accept(sfd);
	int index = get_service_index(sfd);
	printf("Client requested for service %s\n", services[index].name);
	// if (services[index].started == 0){
	// 	int pid = fork();
	// 	if (pid == 0){
	// 		char *cap = (char *)malloc(sizeof(char)*10);
	// 		sprintf(cap, "%d", services[index].capacity);
	// 		if (index == 0){
	// 			execl(services[index].name, services[index].name, services[index].name, cap,"server", services[index+1].name,(char*)0);

	// 		}
	// 		else if (index == MAX_SERVICES -1){
	// 			execl(services[index].name, services[index].name, services[index].name, cap,services[index-1].name, "server",(char*)0);

	// 		}
	// 		else{
	// 			execl(services[index].name, services[index].name, services[index].name, cap,services[index-1].name, services[index+1].name,(char*)0);

	// 		}
	// 	}
	// 	else{
	// 		services[index].unsfd = _accept(services[index].usfd);
	// 		services[index].started = 1;
	// 	}
		
	// }
	send_fd(services[index].usfd, client_sfd); // sending the accepted client sfd to the service.
	// printf("Sent the fd\n");
}
int conjuration_service_ipc_server(conjuration_diy_tmp *tmp,int socket) {
	int fd;
	int err = 0;
	int flags;
	mode_t mode;
	ucred creds;
	
	get_local_socket_credentials(socket,&creds);
	
	if(read(socket,&flags,sizeof(flags)) != sizeof(flags) && !err) {
		err = errno;
	}
	if(read(socket,&mode,sizeof(mode)) != sizeof(mode) && !err) {
		err = errno;
	}
	
	fd = conjuration_diy(flags,mode,creds.uid,creds.gid,tmp);
	if(fd < 0) {
		err = errno;
	}
	
	if(send_fd(socket,fd) < 0 && !err) {
		err = errno;
	}
	if(write(socket,&err,sizeof(err)) != sizeof(err) && !err) {
		err = errno;
	}
	if(fd > 0) {
		close(fd);
	}
	return err;
}
Пример #9
0
int main(){
    int pipefd[2];
	int fd_to_pass=0;

	//创建父子进程之间的管道,文件描述符pipefd[0]和pipefd[1]都是UNIX域套接字
	int ret=socketpair(PF_UNIX,SOCK_STREAM,0,pipefd);
	assert(ret!=-1);

	pid_t pid=fork();
	assert(pid>=0);

	if(pid==0){
		close(pipefd[0]);
		fd_to_pass=open("select.c",O_RDONLY);
		send_fd(pipefd[1],fd_to_pass);
		close(fd_to_pass);
		exit(EXIT_SUCCESS);
	}

	else{
		sleep(3);
		close(pipefd[1]);
		fd_to_pass=recv_fd(pipefd[0]);
		char buf[1024];
		memset(buf,0,1024);
		read(fd_to_pass,buf,1024);
		printf("I got fd %d and data %s\n",fd_to_pass,buf);
		close(fd_to_pass);
	}
}
Пример #10
0
void request(char *buf, int nread, int fd)
{
	int newfd;

	if (buf[nread-1] != 0)
	{
		sprintf(errmsg, "request not null terminated:%*.*s\n", nread, nread, buf);
		send_err(fd, -1, errmsg);
		return;
	}

	if (buf_args(buf, cli_args) < 0) {
		send_err(fd, -1, errmsg);
		return;
	}

	if ( (newfd = open(pathname, oflag)) < 0)
	{
		sprintf(errmsg, "can't open %s: %s\n", pathname, strerror(errno));
		send_err(fd, -1, errmsg);
		return;
	}
	
	if (send_fd(fd, newfd) < 0)
		err_sys("send_fd error", errno);
	close(newfd);	
}
Пример #11
0
void handle_request(char *buf, int nread, int fd)
{
	int	newfd;

	if (buf[nread - 1] != 0) {
		/*
		 * "%*.*s", m, n, "string"
		 * total field width is m
		 * precision is n.
		 * this manner can limit print max(m, n) chars up to 
		 * max(n, m)
		 */
		snprintf(errmsg, MAXLINE - 1,
				"request not null terminated: %*.*s\n",
				nread, nread, buf);
		send_err(fd, -1, errmsg);
		return;
	}
	if (buf_args(buf, cli_args) < 0) { /* parse args & set options */
		send_err(fd, -1, errmsg);
		return;
	}
	if ((newfd = open(pathname, oflag)) < 0) {
		snprintf(errmsg, MAXLINE - 1, "can't open %s: %s\n", pathname,
				strerror(errno));
		send_err(fd, -1, errmsg);
		return;
	}
	if (send_fd(fd, newfd) < 0) /* send the descriptor */
		err_sys("send_fd error");
	close(newfd); /* we're done with descriptor */
}
Пример #12
0
int main()
{
    int socket_fd, client_fd;
    struct sockaddr_un server_address;

    if ((socket_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
        perror("socket()");
        return -1;
    }

    memset(&server_address, 0, sizeof(server_address));
    server_address.sun_family = AF_UNIX;
    strcpy(server_address.sun_path, "#hax");
    server_address.sun_path[0] = 0;             // make it abstract

    if (bind(socket_fd, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) {
        perror("bind()");
        return -1;
    }

    if (listen(socket_fd, 1) != 0) {
        perror("listen()");
        return -1;
    }

    if ((client_fd = accept(socket_fd, 0, 0)) == -1) {
        perror("accept()");
        return -1;
    }

    int dir_fd = open("/", O_RDONLY);
    send_fd(client_fd, dir_fd);

    return 0;
}
Пример #13
0
int main(int argc,char* argv[])
{
	if(argc != 2)
	{
		printf("error args\n");
		return -1;
	}
	int fds[2];
	int ret=socketpair(AF_LOCAL,SOCK_STREAM,0,fds);
	if(-1==ret)
	{
		perror("socketpair");
		return -1;
	}	
	if(!fork())
	{
		close(fds[1]);
		int fd;
		recv_fd(fds[0],&fd);
		printf("i am child,fd is %d\n",fd);
		char buf[10]={0};
		read(fd,buf,sizeof(buf));
		printf("the buf is %s\n",buf);
		return 0;
	}else{
		close(fds[0]);
		int fd=open(argv[1],O_RDWR);
		printf("i am parent,fd is %d\n",fd);
		send_fd(fds[1],fd);  //把fd这个描述符 写到fds[1]写描述符中
		wait(NULL);
		return 0;
	}

}
Пример #14
0
void handle_request(char *buf, int nread, int clifd, uid_t uid)
{
	int	newfd;

	if (buf[nread - 1] != 0) {
		snprintf(errmsg, MAXLINE - 1,
				"request from uid %d not null terminated: %*.*s\n",
				uid, nread, nread, buf);
		send_err(clifd, -1, errmsg);
		return;
	}
	log_msg("request: %s, from uid %d", buf, uid);
	/* parse the arguments, set options */
	if (buf_args(buf, cli_args) < 0) {
		send_err(clifd, -1, errmsg);
		log_msg(errmsg);
		return;
	}

	if ((newfd = open(pathname, oflag)) < 0) {
		snprintf(errmsg, MAXLINE - 1, "can't open %s: %s\n",
				pathname, strerror(errno));
		send_err(clifd, -1, errmsg);
		log_msg(errmsg);
		return;
	}

	/* send the desrciptor */
	if (send_fd(clifd, newfd) < 0)
		log_sys("send_fd error");
	log_msg("send fd %d over fd %d for %s", newfd, clifd, pathname);
	close(newfd);	/* we're done with desrciptor */
}
Пример #15
0
void
handle_request(char *buf, int nread, int fd)
{
	int		newfd;

	if (buf[nread-1] != 0) {
		snprintf(errmsg, MAXLINE-1,
		  "request not null terminated: %*.*s\n", nread, nread, buf);
		send_err(fd, -1, errmsg);
		return;
	}
	if (buf_args(buf, cli_args) < 0) {	/* parse args & set options */
		send_err(fd, -1, errmsg);
		return;
	}
	if ((newfd = open(pathname, oflag)) < 0) {
		snprintf(errmsg, MAXLINE-1, "can't open %s: %s\n", pathname,
		  strerror(errno));
		send_err(fd, -1, errmsg);
		return;
	}
	if (send_fd(fd, newfd) < 0)		/* send the descriptor */
		err_sys("send_fd error");
	close(newfd);		/* we're done with descriptor */
}
Пример #16
0
static int send2child(struct tcp_connection* tcpconn)
{
	int i;
	int min_busy;
	int idx;
	
	min_busy=tcp_children[0].busy;
	idx=0;
	for (i=0; i<tcp_children_no; i++){
		if (!tcp_children[i].busy){
			idx=i;
			min_busy=0;
			break;
		}else if (min_busy>tcp_children[i].busy){
			min_busy=tcp_children[i].busy;
			idx=i;
		}
	}
	
	tcp_children[idx].busy++;
	tcp_children[idx].n_reqs++;
	if (min_busy){
		LM_INFO("no free tcp receiver, connection passed to the least"
				" busy one (%d)\n", min_busy);
	}
	LM_DBG("to tcp child %d %d(%d), %p\n", idx, tcp_children[idx].proc_no,
					tcp_children[idx].pid, tcpconn);
	if (send_fd(tcp_children[idx].unix_sock, &tcpconn, sizeof(tcpconn),
			tcpconn->s)<=0){
		LM_ERR("send_fd failed\n");
		return -1;
	}
	
	return 0;
}
Пример #17
0
void
asroot_open()
{
	const char* authorized[] = {
		"/proc/sys/net/ipv4/ip_forward",
		"/proc/net/bonding/[^.][^/]*",
		"/proc/self/net/bonding/[^.][^/]*",
#ifdef ENABLE_OLDIES
		SYSFS_CLASS_NET "[^.][^/]*/brforward",
		SYSFS_CLASS_NET "[^.][^/]*/brport",
		SYSFS_CLASS_NET "[^.][^/]*/brif/[^.][^/]*/port_no",
#endif
		SYSFS_CLASS_DMI "product_version",
		SYSFS_CLASS_DMI "product_serial",
		SYSFS_CLASS_DMI "product_name",
		SYSFS_CLASS_DMI "bios_version",
		SYSFS_CLASS_DMI "sys_vendor",
		SYSFS_CLASS_DMI "chassis_asset_tag",
		NULL
	};
	const char **f;
	char *file;
	int fd, len, rc;
	regex_t preg;

	must_read(PRIV_PRIVILEGED, &len, sizeof(len));
	if ((file = (char *)malloc(len + 1)) == NULL)
		fatal("privsep", NULL);
	must_read(PRIV_PRIVILEGED, file, len);
	file[len] = '\0';

	for (f=authorized; *f != NULL; f++) {
		if (regcomp(&preg, *f, REG_NOSUB) != 0)
			/* Should not happen */
			fatal("privsep", "unable to compile a regex");
		if (regexec(&preg, file, 0, NULL, 0) == 0) {
			regfree(&preg);
			break;
		}
		regfree(&preg);
	}
	if (*f == NULL) {
		log_warnx("privsep", "not authorized to open %s", file);
		rc = -1;
		must_write(PRIV_PRIVILEGED, &rc, sizeof(int));
		free(file);
		return;
	}
	if ((fd = open(file, O_RDONLY)) == -1) {
		rc = -1;
		must_write(PRIV_PRIVILEGED, &rc, sizeof(int));
		free(file);
		return;
	}
	free(file);
	must_write(PRIV_PRIVILEGED, &fd, sizeof(int));
	send_fd(PRIV_PRIVILEGED, fd);
	close(fd);
}
Пример #18
0
int parasite_send_fd(struct parasite_ctl *ctl, int fd)
{
	if (send_fd(ctl->tsock, NULL, 0, fd) < 0) {
		pr_perror("Can't send file descriptor");
		return -1;
	}
	return 0;
}
Пример #19
0
/**
 * eva_pass_fd_send:
 * @sender_fd: the file-descriptor allocated with
 * eva_pass_fd_make_pair().
 * @pass_fd: the file-descriptor to pass.
 * @aux_info_len: length of auxiliary information to send with the fd.
 * must be nonzero!
 * @aux_info_data: auxiliary information to send with the fd.
 * @error: optional location to store error at.
 *
 * Pass a file-descriptor from one process to another
 * using a file-descriptor that does not require a path.
 *
 * This function can fail transiently (if there is no space
 * in the message-queue), in which case it returns FALSE
 * but does NOT set *error.
 *
 * If this function returns TRUE, you should close pass_fd immediately.
 *
 * returns: whether the file-descriptor was passed to the subprocess.
 * If this fails, it may set *error, if the error was a serious one
 * (for example if the remote side shuts down).
 */
gboolean eva_pass_fd_send         (int           sender_fd,
                                   int           pass_fd,
                                   guint         aux_info_len,
                                   const guint8 *aux_info_data,
                                   GError      **error)
{
  return send_fd (sender_fd, NULL, pass_fd, aux_info_len, aux_info_data, error);
}
Пример #20
0
int on_accept(int epoll_fd, int listen_sock)
{
	int conn_sock;
	int channel_fd;
	struct sockaddr_in client_addr;
	socklen_t client_len = 0;
	channel ch;

	memset(&client_addr, 0, sizeof(struct sockaddr_in));

	while(1) {
		if ((conn_sock = accept(listen_sock, (struct sockaddr*) &client_addr, &client_len)) < 0) {
			if (errno == EINTR)
				continue;		
			else {
				perror("accept failed: ");
				return L_HTTP_FAIL;
			}
		}
		break;
	}

	set_nonblocking(conn_sock);

	ch.cmd = HTTP_COMMAND_TRANS_FD;
	ch.fd = conn_sock;

	if((channel_fd = get_worker_fd()) == L_HTTP_FAIL) {
		printf("no worker to use\n");
		return L_HTTP_FAIL;
	}

	if(send_fd(channel_fd, &ch) == L_HTTP_FAIL)
		return L_HTTP_FAIL;
	
	close(conn_sock);
	return L_HTTP_SUCCESS;

	/*sb = generate_socket_buf(conn_sock);
	if(sb == NULL) {
		perror("not enough memory");
		return L_HTTP_FAIL;
	}			

	sb->state = STATE_HTTP_READ;
	if(sb_array.size() < MAX_CONNECTION 
			&& find_socket_buf(conn_sock) == NULL)
		sb_array[conn_sock] = sb;
	else {
		printf("to many connections or using same fd");
		return L_HTTP_FAIL;
	}

	//ev.events = EPOLLIN;
	//set_timer(sb->timer, DEFAULT_TIME_LENGTH);	

	//return epoll_add_event(epoll_fd, EPOLLIN, conn_sock);*/
}
Пример #21
0
/**
 * eva_pass_fd_sendto:
 * @sender_fd: the file-descriptor allocated with
 * eva_pass_fd_make_sender().
 * @path: the location to send to file-descriptor to.
 * @pass_fd: the file-descriptor to pass.
 * @aux_info_len: length of auxiliary information to send with the fd.
 * must be nonzero!
 * @aux_info_data: auxiliary information to send with the fd.
 * @error: optional location to store error at.
 *
 * Pass a file-descriptor from one process to a named location.
 * The name should match a location where some other process
 * has called eva_pass_fd_bind_receiver().
 *
 * This function can fail transiently (if there is no space
 * in the message-queue), in which case it returns FALSE
 * but does NOT set *error.
 *
 * If this function returns TRUE, you should close pass_fd immediately.
 *
 * returns: whether the file-descriptor was passed to the subprocess.
 * If this fails, it may set *error, if the error was a serious one
 * (for example if the remote side shuts down).
 */
gboolean eva_pass_fd_sendto       (int           sender_fd,
                                   const char   *path,
                                   int           pass_fd,
                                   guint         aux_info_len,
                                   const guint8 *aux_info_data,
                                   GError      **error)
{
  g_return_val_if_fail (path != NULL, FALSE);
  return send_fd (sender_fd, path, pass_fd, aux_info_len, aux_info_data, error);
}
Пример #22
0
/**
 * Sends a newly connected socket to the receiver process
 * @param sock
 * @return
 */
int receiver_send_socket(int sock, peer *p)
{
	int pipe_fd;
	if (p)
		pipe_fd = p->fd_exchange_pipe;
	else
		pipe_fd = fd_exchange_pipe_unknown;

	return send_fd(pipe_fd,sock,p);
}
Пример #23
0
void serve(int sockfd, size_t cpunum)
{
	pid_t *pid;
	int  clfd;
	size_t cpu;
	size_t loadserver = 0;
	int (*channels)[2];
	
	channels = (int (*)[2])malloc(cpunum*sizeof(int)*2);
	pid      = (pid_t *)malloc(cpunum*sizeof(pid_t));

	for (cpu = 0; cpu < cpunum; ++cpu){
		if (socketpair(AF_UNIX, SOCK_STREAM, 0, channels[cpu]) == -1){
			fprintf(stderr, "socketpair error\n");
			exit(1);
		}
		if ((pid[cpu] = fork()) == -1){
			fprintf(stderr, "fore error!\n");
			exit(1);
		} 
		
		if(pid[cpu] == 0){               /* child */
			close(channels[cpu][0]);
			if (channels[cpu][1] != STDIN_FILENO &&
				dup2(channels[cpu][1], STDIN_FILENO) != STDIN_FILENO)
				err_sys("dup2 to stdin error");
			/*
			if (channels[cpu][1] != STDOUT_FILENO &&
				dup2(channels[cpu][1], STDOUT_FILENO) != STDOUT_FILENO)
				err_sys("dup2 to stdout error");
			*/
			if (execl("./load_server", "load_server", (char*)0) < 0)
				err_sys("execl error");
		}
		close(channels[cpu][1]);    /* parent */
	}
	
	for ( ; ; ){
		clfd = accept(sockfd, NULL, NULL);
		if (clfd < 0){
			fprintf(stderr, "tecentd: accept error: %s", strerror(errno));
			break;;
		}
		if (send_fd(channels[loadserver++][0], clfd) == -1){
			fprintf(stderr, "send_fd : sending file descriptor error");
			break;
		}
		loadserver %= cpunum;
		close(clfd);
	}

	for (cpu = 0; cpu < cpunum; ++cpu){
		kill(pid[cpu], SIGKILL);
	}
}
Пример #24
0
int main(int argc, char **argv)
{
	int sk[2], p[2];
#define MSG "HELLO"
	char buf[8]; /* bigger than the MSG to check boundaries */

	test_init(argc, argv);

	if (socketpair(PF_UNIX, SOCK_DGRAM, 0, sk) < 0) {
		pr_perror("Can't make unix pair");
		exit(1);
	}

	if (pipe(p) < 0) {
		pr_perror("Can't make pipe");
		exit(1);
	}

	if (send_fd(sk[0], p[0], p[1]) < 0) {
		pr_perror("Can't send descriptor");
		exit(1);
	}

	close(p[0]);
	close(p[1]);
	p[0] = p[1] = -1;

	test_daemon();
	test_waitsig();

	if (recv_fd(sk[1], &p[0], &p[1]) < 0) {
		fail("Can't recv pipes back");
		goto out;
	}

	if (write(p[1], MSG, sizeof(MSG)) != sizeof(MSG)) {
		fail("Pipe write-broken");
		goto out;
	}

	if (read(p[0], buf, sizeof(buf)) != sizeof(MSG)) {
		fail("Pipe read-broken");
		goto out;
	}

	if (strcmp(buf, MSG)) {
		buf[sizeof(buf) - 1] = '\0';
		fail("Pipe read-broken (%s)", buf);
		goto out;
	}

	pass();
out:
	return 0;
}
Пример #25
0
int main ()
{
  int fd[2], ff[2];

  int target;
  if (socketpair (PF_UNIX, SOCK_SEQPACKET, 0, fd)==-1)
    return 1;
  for (;;)
  {
    if (socketpair (PF_UNIX, SOCK_SEQPACKET, 0, ff)==-1)
        return 2;
    send_fd (ff[0], fd[0]);
    send_fd (ff[0], fd[1]);

    close (fd[1]);
    close (fd[0]);
    fd[0] = ff[0];
    fd[1] = ff[1];
  }
}
Пример #26
0
int main(int argc, char const *argv[]) {
	int usfd, nusfd, nsfd, sfd, i, j, len;
	pthread_t pid;
	pthread_mutex_init(&lock, NULL);
	usfd = init_sockbind("/tmp/sockpath");
	if(usfd < 0) {
		perror("init_sockbind() ");
		exit(1);
	}
	sfd = tcpsocket_bind(8001);
	if(sfd < 0) {
		perror("tcpsocket_bind() ");
		exit(2);
	}
	nusfd = sock_accept(usfd);
	if(nusfd < 0) {
		perror("sock_accept() ");
		exit(3);
	}

	struct ucred cred;
	len = sizeof(struct ucred);
	
	if(getsockopt(nusfd, SOL_SOCKET, SO_PEERCRED, &cred, &len) < 0) {
		perror("getsockopt() ");
		exit(4);
	}
	// printf("%ld\n", (long) cred.pid); // backup server pid
	pthread_create(&pid, NULL, &runner, NULL);
	while(1) {
		nsfd = tcp_accept(sfd);
		if(nsfd < 0) {
			perror("tcp_accept() ");
			continue;
		}
		if(nsfd_cnt == MAXF) {
			for (i = 0; i < MAXF; ++i) {
				pthread_mutex_lock(&lock);
				if(send_fd(nusfd, nsfds[i]) < 0) {
					perror("send_fd() ");
				}
				pthread_mutex_unlock(&lock);
				// close(nsfds[i]);
			}
			nsfd_cnt = 0;
			sleep(2);
			kill(cred.pid, SIGUSR1);
		}
		pthread_mutex_lock(&lock);
		nsfds[nsfd_cnt++] = nsfd;
		pthread_mutex_unlock(&lock);
	}
	return 0;
}
Пример #27
0
static PyObject* py_send_fd(PyObject *self, PyObject *args)
{
    int sock, fd;
    int returncode;
	
	if (!PyArg_ParseTuple(args, "ii:send_fd", &sock, &fd))
		return NULL;
	
	returncode = send_fd(sock, fd);
	return Py_BuildValue("i", returncode);
}
Пример #28
0
int main(){
  struct sockaddr_un  userv_addr,local,remote;
  int usfd;
  usfd = socket(AF_UNIX,SOCK_STREAM,0);
      bzero((struct sockaddr_un*)&userv_addr,sizeof(userv_addr));
    userv_addr.sun_family = AF_UNIX;
    char socks[15];
    sprintf(socks,"%s","skservcs");
    strcpy(userv_addr.sun_path,socks);
    int len = strlen(userv_addr.sun_path) + sizeof(userv_addr.sun_family);
    if(connect(usfd,(struct sockaddr*)&userv_addr,len)<0){
      printf("connect error!!\n");
      exit(0);
    }
    printf("connected to Multiplex C\n");
    int csfd= socket (AF_UNIX,SOCK_STREAM,0);
     bzero((struct sockaddr_un*)&local,sizeof(local));
    local.sun_family = AF_UNIX;
     bzero(socks,15);
    sprintf(socks,"%s","sksep");
    strcpy(local.sun_path,socks);
    unlink(local.sun_path);
    len = strlen(local.sun_path) + sizeof(local.sun_family);
  if(bind(csfd,(struct sockaddr*)&local,sizeof(local))<0){
    printf("bind error!!\n");
    exit(0);
  }   
  int cllen=0;
  listen(csfd,5);
  pthread_t pd;
  pthread_create(&pd,NULL,serv,(void*)&cllen);
  printf("please start Multiplex E\n");
  int x =0;
  int ncsfd = accept(csfd,(struct sockaddr*)&remote,&x);
  if(ncsfd <0){
    perror("accpet error!!\n");
    exit(0);
  }
      printf("connected to Multiplex E\n");
    while(1){
      int sfd = recv_fd(usfd);
      printf("client recieved");
      if(l>=1){
        printf("Transferring client\n");
        send_fd(ncsfd,sfd);
      }
      else
      {
        fds[l++]=sfd;
        printf("servicing the client\n");
      }
    }
}
Пример #29
0
int send_err(int fd,int errcode,const char *msg)
{
	int n;
	if((n=strlen(msg))>0)
		if(write(fd,msg,n)!=n)
			return magicnum::FAILIED;
	if(errcode>=0)
		errcode=-1;
	if(send_fd(fd,errcode)<0)
		return magicnum::FAILIED;
	return magicnum::SUCCESS;
}
Пример #30
0
pid_t LightProcess::proc_open(const char *cmd, const std::vector<int> &created,
                              const std::vector<int> &desired,
                              const char *cwd,
                              const std::vector<std::string> &env) {
  int id = GetId();
  Lock lock(g_procs[id].m_procMutex);
  always_assert(Available());
  always_assert(created.size() == desired.size());

  auto fout = g_procs[id].m_afdt_fd;
  lwp_write(fout, "proc_open");
  lwp_write(fout, cmd);
  lwp_write(fout, cwd ? cwd : "");
  lwp_write_int32(fout, (int)env.size());
  for (unsigned int i = 0; i < env.size(); i++) {
    lwp_write(fout, env[i]);
  }

  lwp_write_int32(fout, (int)created.size());
  for (unsigned int i = 0; i < desired.size(); i++) {
    lwp_write_int32(fout, desired[i]);
  }

  bool error_send = false;
  int save_errno = 0;
  for (unsigned int i = 0; i < created.size(); i++) {
    if (!send_fd(g_procs[id].m_afdt_fd, created[i])) {
      error_send = true;
      save_errno = errno;
      break;
    }
  }

  std::string buf;
  auto fin = g_procs[id].m_afdt_fd;
  lwp_read(fin, buf);
  if (buf == "error") {
    lwp_read_int32(fin, errno);
    if (error_send) {
      // On this error, the receiver side returns dummy errno,
      // use the sender side errno here.
      errno = save_errno;
    }
    return -1;
  }
  always_assert_flog(buf == "success",
                     "Unexpected message from light process: `{}'", buf);
  int64_t pid = -1;
  lwp_read_int64(fin, pid);
  always_assert(pid);
  return (pid_t)pid;
}