Exemplo n.º 1
0
int main(int argc,char* argv[])
{

	int usfd=unix_socket("soc.socket");
	 
	int sfd=inet_socket(atoi(argv[1]));
	
	sockaddr_in cliaddr;
	sockaddr_un cliuaddr;
	socklen_t clen,cen;
	int pid;
	int nsfd,nusfd;
	
	int flag=1;
	
	while(1)
	{
		//sleep(2);
		printf("accepting\n");
		 nsfd=accept(sfd,NULL,NULL);
		
		if(flag==1)
		{
			flag=0;
			pid=fork();
			if(pid==0)
			{
				close(sfd);
				close(usfd);
				execlp("./s1","./s1",(char*)0);
			}
			else
			{
				 nusfd=accept(usfd,NULL,NULL);
	 			printf("nusfd%d\n",nusfd);
	 			
				sendfd(nusfd,nsfd);
				close(nsfd);
			}

		}
		else
		{
			
			sendfd(nusfd,nsfd);
			close(nsfd);
		}
		
	}
	
}
Exemplo n.º 2
0
static void
lmtp_open(struct deliver *deliver)
{
	FILE		*fp;
	char		*buf = NULL, hn[HOST_NAME_MAX + 1],
			*rcpt = deliver->to, *to = deliver->to;
	size_t		 sz = 0;
	ssize_t		 len;
	int		 s;

	strsep(&rcpt, " ");
	s = (to[0] == '/') ? unix_socket(to) : inet_socket(to);
	if ((fp = fdopen(s, "r+")) == NULL)
		err(1, "fdopen");

	if (lmtp_banner(&buf, &sz, '2', fp) != 0)
		errx(1, "Invalid LHLO reply: %s", buf);

	if (gethostname(hn, sizeof hn) == -1)
		err(1, "gethostname");

	if (lmtp_cmd(&buf, &sz, '2', fp, "LHLO %s", hn) != 0)
		errx(1, "Invalid LHLO reply: %s", buf);

	if (lmtp_cmd(&buf, &sz, '2', fp, "MAIL FROM:<%s>", deliver->from) != 0)
		errx(1, "MAIL FROM rejected: %s", buf);

	if (lmtp_cmd(&buf, &sz, '2', fp, "RCPT TO:<%s>",
	    rcpt ? deliver->dest : deliver->user) != 0)
		errx(1, "RCPT TO rejected: %s", buf);

	if (lmtp_cmd(&buf, &sz, '3', fp, "DATA") != 0)
		errx(1, "Invalid DATA reply: %s", buf);

	while ((len = getline(&buf, &sz, stdin)) != -1) {
		if (buf[len - 1] == '\n')
			buf[len - 1] = '\0';

		if (fprintf(fp, "%s%s\r\n", buf[0] == '.' ? "." : "", buf) < 0)
			errx(1, "fprintf failed");
	}

	if (lmtp_cmd(&buf, &sz, '2', fp, ".") != 0)
		errx(1, "Delivery error: %s", buf);

	if (lmtp_cmd(&buf, &sz, '2', fp, "QUIT") != 0)
		errx(1, "Error on QUIT: %s", buf);

	exit(0);
}
Exemplo n.º 3
0
int main(int argc,char* argv[])
{
		n=0;limit=1;
		
		pthread_mutex_init(&mut,NULL);
		int sfd=inet_socket(atoi(argv[1]));
		int usfd1,usfd2;
		
		 usfd1=unix_socket("soc2");//serv sock
		 int nusfd=accept(usfd1,NULL,NULL);
		 
		 usfd2=socket(AF_UNIX,SOCK_STREAM,0); //cli sock
		 struct sockaddr_un servaddr;
		 servaddr.sun_family=AF_UNIX;
		 strcpy(servaddr.sun_path,"soc3");
		 int len=sizeof(sa_family_t)+strlen(servaddr.sun_path);
		 int re=connect(usfd2,(struct sockaddr*)&servaddr,len);
		 printf("res %d\n",re);
		// sleep(2);
		 pthread_t t;
		 fds f;
		 f.usfd1=nusfd;
		  f.usfd2=usfd2;
		 
		pthread_create(&t,NULL,fdreceiver,&f);

		while(1)
		{
			
			int nsfd=accept(sfd,NULL,NULL);
			pthread_mutex_lock(&mut);
			printf("one entering mall 2\n");
			n++;
			pthread_mutex_unlock(&mut);
			if(n>limit)
			{
				printf("m2 full\n");
				sendfd(usfd2,nsfd);
				close(nsfd);
			}
			else
			{
				data td;
				td.nsfd=nsfd;
				pthread_create(&t,NULL,&service,&td);
			}
			
		}
}
Exemplo n.º 4
0
int main(int argc,char* argv[])
{
	
	pthread_mutex_init(&mut,NULL);
	 n=0;limit=1;
	 int usfd;
	 usfd=socket(AF_UNIX,SOCK_STREAM,0);
	 struct sockaddr_un servaddr;
	 servaddr.sun_family=AF_UNIX;
	 strcpy(servaddr.sun_path,"soc2");
	 int len=sizeof(sa_family_t)+strlen(servaddr.sun_path);
	 int re=-1;
	
	 
	
	
	int sfd=inet_socket(atoi(argv[1]));
	
	re=connect(usfd,(struct sockaddr*)&servaddr,len);
	 printf("res1 %d\n",re);
	 if(re==-1)
	 {
	 	re=connect(usfd,(struct sockaddr*)&servaddr,len);
	 printf("res1 %d\n",re);
	 	
	 }
		while(1)
		{
			
			int nsfd=accept(sfd,NULL,NULL);
			pthread_mutex_lock(&mut);
			n++;
			pthread_mutex_unlock(&mut);
			printf("one is entring mall1\n");
			if(n>limit)
			{
				printf("m1 full\n");
				sendfd(usfd,nsfd);
				close(nsfd);
			}
			else
			{
				data td;
				td.nsfd=nsfd;
				pthread_t t;
				pthread_create(&t,NULL,&service,&td);
			}
		}
}
Exemplo n.º 5
0
static void logger_open(struct logger_ctl *ctl)
{
	if (ctl->server) {
		ctl->fd = inet_socket(ctl->server, ctl->port, ctl->socket_type);
		if (!ctl->syslogfp)
			ctl->syslogfp = syslog_rfc5424;
		return;
	}
	if (ctl->unix_socket) {
		ctl->fd = unix_socket(ctl->unix_socket, ctl->socket_type);
		if (!ctl->syslogfp)
			ctl->syslogfp = syslog_rfc5424;
		return;
	}
	ctl->fd = unix_socket("/dev/log", ctl->socket_type);
	ctl->syslogfp = syslog_local;
}
Exemplo n.º 6
0
int main()
{
		int id=atoi(argv[1]);
		limit=atoi(argv[2]);
		int sfd=inet_socket(atoi(argv[3]));
		int usfd1,usfd2;
		if(id>1)
		 usfd1=unix_socket(argv[4]);//serv sock
		 
		 if(id==1)
		 {
		 usfd2=socket(AF_UNIX,SOCK_STREAM,0);
		 struct sockaddr_un servaddr;
		 servaddr.sun_family=AF_UNIX;
		 strcpy(servaddr.sun_path,argv[5]);
		 int len=sizeof(sa_familt_t)+strlen(servaddr.sun_path);
		 int re=connect(usfd2,(struct sockaddr*)&servaddr,len);
		 printf("res %d\n",re);
		 
		 }
		 
		 
		 nusfd=accept(usfd1,NULL,NULL);
		pthread_create(&t,NULL,fdreceiver,&usfd);

		while(1)
		{
			
			nsfd=accept(nsfd,NULL,NULL);
			n++;
			if(n>=limit)
			{
				sendfd(nusfd,nsfd);
				close(nsfd);
			}
			if(id==3 && n==limit)
			{
				kill(getppid(),SIGUSR1);
			}
			else
			{
				sendfd(usfd,nsfd);
			}
		}
}
Exemplo n.º 7
0
Arquivo: logger.c Projeto: alisw/uuid
static void logger_open(struct logger_ctl *ctl)
{
	if (ctl->server) {
		ctl->fd = inet_socket(ctl->server, ctl->port, &ctl->socket_type);
		if (!ctl->syslogfp)
			ctl->syslogfp = syslog_rfc5424_header;
	} else {
		if (!ctl->unix_socket)
			ctl->unix_socket = _PATH_DEVLOG;

		ctl->fd = unix_socket(ctl, ctl->unix_socket, &ctl->socket_type);
		if (!ctl->syslogfp)
			ctl->syslogfp = syslog_local_header;
	}
	if (!ctl->tag)
		ctl->tag = xgetlogin();
	generate_syslog_header(ctl);
}
Exemplo n.º 8
0
static void
delivery_lmtp_open(struct deliver *deliver)
{
	 char *buffer, *lbuf, *rcpt = deliver->to;
	 char lhloname[255];
	 int s;
	 FILE *fp = NULL;
	 enum lmtp_state state = LMTP_BANNER;
	 size_t	len;

	 strsep(&rcpt, " ");
	 if (deliver->to[0] == '/')
		 s = unix_socket(deliver->to);
	 else
		 s = inet_socket(deliver->to);

	 if (s == -1 || (fp = fdopen(s, "r+")) == NULL)
		 err(1, "couldn't establish connection");

	 while (!feof(fp) && !ferror(fp) && state != LMTP_BYE) {
		 buffer = lmtp_getline(fp);
		 if (buffer == NULL)
			 errx(1, "No input received");

		 switch (state) {
		 case LMTP_BANNER:
			 if (strncmp("220 ", buffer, 4) != 0)
				 errx(1, "Invalid LMTP greeting: %s\n", buffer);
			 gethostname(lhloname, sizeof lhloname );
			 fprintf(fp, "LHLO %s\r\n", lhloname);
			 state = LMTP_LHLO;
			 break;

		 case LMTP_LHLO:
			 if (buffer[0] != '2')
				 errx(1, "LHLO rejected: %s\n", buffer);
			 if (strlen(buffer) < 4)
				 errx(1, "Invalid LMTP LHLO answer: %s\n", buffer);
			 if (buffer[3] == '-')
				 continue; /* multi-line */
			 fprintf(fp, "MAIL FROM:<%s>\r\n", deliver->from);
			 state = LMTP_MAIL_FROM;
			 break;

		 case LMTP_MAIL_FROM:
			 if (buffer[0] != '2')
				 errx(1, "MAIL FROM rejected: %s\n", buffer);
			 fprintf(fp, "RCPT TO:<%s>\r\n",
			     rcpt ? deliver->dest : deliver->user);
			 state = LMTP_RCPT_TO;
			 break;
			 
		 case LMTP_RCPT_TO:
			 if (buffer[0] != '2')
				 errx(1, "RCPT TO rejected: %s\n", buffer);
			 fprintf(fp, "DATA\r\n");
			 state = LMTP_DATA;
			 break;
			 
		 case LMTP_DATA:
			 if (buffer[0] != '3')
				 errx(1, "DATA rejected: %s\n", buffer);
			 lbuf = NULL;
			 while ((buffer = fgetln(stdin, &len))) {
				 if (buffer[len- 1] == '\n')
					 buffer[len - 1] = '\0';
				 else {
					 if ((lbuf = malloc(len + 1)) == NULL)
						 err(1, NULL);
					 memcpy(lbuf, buffer, len);
					 lbuf[len] = '\0';
					 buffer = lbuf;
				 }
				 fprintf(fp, "%s%s\r\n",
				     *buffer == '.' ? "." : "", buffer);
			 }
			 free(lbuf);
			 fprintf(fp, ".\r\n");
			 state = LMTP_QUIT;
			 break;

		 case LMTP_QUIT:
			 if (buffer[0] != '2')
				 errx(1, "Delivery error: %s\n", buffer);
			 fprintf(fp, "QUIT\r\n");
			 state = LMTP_BYE;
			 break;

		 default:
			errx(1, "Bogus state %d", state);
		 }
	 }

	 _exit(0);
}
Exemplo n.º 9
0
/*
 * logger -- read and log utility
 *
 *	Reads from an input and arranges to write the result on the system
 *	log.
 */
int main(int argc, char **argv)
{
	int ch, logflags, pri, prio_prefix;
	char *tag, buf[1024];
	char *usock = NULL;
	char *server = NULL;
	char *port = NULL;
	int LogSock = -1, socket_type = ALL_TYPES;
#ifdef HAVE_LIBSYSTEMD
	FILE *jfd = NULL;
#endif
	static const struct option longopts[] = {
		{ "id",		no_argument,	    0, 'i' },
		{ "stderr",	no_argument,	    0, 's' },
		{ "file",	required_argument,  0, 'f' },
		{ "priority",	required_argument,  0, 'p' },
		{ "tag",	required_argument,  0, 't' },
		{ "socket",	required_argument,  0, 'u' },
		{ "udp",	no_argument,	    0, 'd' },
		{ "tcp",	no_argument,	    0, 'T' },
		{ "server",	required_argument,  0, 'n' },
		{ "port",	required_argument,  0, 'P' },
		{ "version",	no_argument,	    0, 'V' },
		{ "help",	no_argument,	    0, 'h' },
		{ "prio-prefix", no_argument, 0, OPT_PRIO_PREFIX },
#ifdef HAVE_LIBSYSTEMD
		{ "journald",   optional_argument,  0, OPT_JOURNALD },
#endif
		{ NULL,		0, 0, 0 }
	};

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	atexit(close_stdout);

	tag = NULL;
	pri = LOG_NOTICE;
	logflags = 0;
	prio_prefix = 0;
	while ((ch = getopt_long(argc, argv, "f:ip:st:u:dTn:P:Vh",
					    longopts, NULL)) != -1) {
		switch (ch) {
		case 'f':		/* file to log */
			if (freopen(optarg, "r", stdin) == NULL)
				err(EXIT_FAILURE, _("file %s"),
				    optarg);
			break;
		case 'i':		/* log process id also */
			logflags |= LOG_PID;
			break;
		case 'p':		/* priority */
			pri = pencode(optarg);
			break;
		case 's':		/* log to standard error */
			logflags |= LOG_PERROR;
			break;
		case 't':		/* tag */
			tag = optarg;
			break;
		case 'u':		/* unix socket */
			usock = optarg;
			break;
		case 'd':
			socket_type = TYPE_UDP;
			break;
		case 'T':
			socket_type = TYPE_TCP;
			break;
		case 'n':
			server = optarg;
			break;
		case 'P':
			port = optarg;
			break;
		case 'V':
			printf(UTIL_LINUX_VERSION);
			exit(EXIT_SUCCESS);
		case 'h':
			usage(stdout);
		case OPT_PRIO_PREFIX:
			prio_prefix = 1;
			break;
#ifdef HAVE_LIBSYSTEMD
		case OPT_JOURNALD:
			if (optarg) {
				jfd = fopen(optarg, "r");
				if (!jfd)
					err(EXIT_FAILURE, _("cannot open %s"),
					    optarg);
			} else
				jfd = stdin;
			break;
#endif
		case '?':
		default:
			usage(stderr);
		}
	}
	argc -= optind;
	argv += optind;

	/* setup for logging */
#ifdef HAVE_LIBSYSTEMD
	if (jfd) {
		int ret = journald_entry(jfd);
		if (stdin != jfd)
			fclose(jfd);
		return ret ? EXIT_FAILURE : EXIT_SUCCESS;
	}
#endif
	if (server)
		LogSock = inet_socket(server, port, socket_type);
	else if (usock)
		LogSock = unix_socket(usock, socket_type);
	else
		openlog(tag ? tag : getlogin(), logflags, 0);

	/* log input line if appropriate */
	if (argc > 0) {
		register char *p, *endp;
		size_t len;

		for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
			len = strlen(*argv);
			if (p + len > endp && p > buf) {
			    if (!usock && !server)
				syslog(pri, "%s", buf);
			    else
				mysyslog(LogSock, logflags, pri, tag, buf);
				p = buf;
			}
			if (len > sizeof(buf) - 1) {
			    if (!usock && !server)
				syslog(pri, "%s", *argv++);
			    else
				mysyslog(LogSock, logflags, pri, tag, *argv++);
			} else {
				if (p != buf)
					*p++ = ' ';
				memmove(p, *argv++, len);
				*(p += len) = '\0';
			}
		}
		if (p != buf) {
		    if (!usock && !server)
			syslog(pri, "%s", buf);
		    else
			mysyslog(LogSock, logflags, pri, tag, buf);
		}
	} else {
		char *msg;
		int default_priority = pri;
		while (fgets(buf, sizeof(buf), stdin) != NULL) {
		    /* glibc is buggy and adds an additional newline,
		       so we have to remove it here until glibc is fixed */
		    int len = strlen(buf);

		    if (len > 0 && buf[len - 1] == '\n')
			    buf[len - 1] = '\0';

			msg = buf;
			pri = default_priority;
			if (prio_prefix && msg[0] == '<')
				msg = get_prio_prefix(msg, &pri);

		    if (!usock && !server)
			syslog(pri, "%s", msg);
		    else
			mysyslog(LogSock, logflags, pri, tag, msg);
		}
	}
	if (!usock && !server)
		closelog();
	else
		close(LogSock);

	return EXIT_SUCCESS;
}
Exemplo n.º 10
0
/*-------------------------------------------------------------------*/
int bind_device_ex (DEVBLK* dev, char* spec, ONCONNECT fn, void* arg )
{
    bind_struct* bs;
    int was_list_empty;

    if (!init_done) init_sockdev();

    if (sysblk.shutdown) return 0;

    logdebug("bind_device (%4.4X, %s)\n", dev->devnum, spec);

    /* Error if device already bound */
    if (dev->bs)
    {
        logmsg (_("HHCSD001E Device %4.4X already bound to socket %s\n"),
            dev->devnum, dev->bs->spec);
        return 0;   /* (failure) */
    }

    /* Create a new bind_struct entry */
    bs = malloc(sizeof(bind_struct));

    if (!bs)
    {
        logmsg (_("HHCSD002E bind_device malloc() failed for device %4.4X\n"),
            dev->devnum);
        return 0;   /* (failure) */
    }

    memset(bs,0,sizeof(bind_struct));

    bs->fn  = fn;
    bs->arg = arg;

    if (!(bs->spec = strdup(spec)))
    {
        logmsg (_("HHCSD003E bind_device strdup() failed for device %4.4X\n"),
            dev->devnum);
        free (bs);
        return 0;   /* (failure) */
    }

    /* Create a listening socket */
    if (bs->spec[0] == '/') bs->sd = unix_socket (bs->spec);
    else                    bs->sd = inet_socket (bs->spec);
    if (bs->sd == -1)
    {
        /* (error message already issued) */
        free( bs->spec );
        free( bs );
        return 0; /* (failure) */
    }

    /* Chain device and bind_struct to each other */
    dev->bs = bs;
    bs->dev = dev;

    /* Add the new entry to our list of bound devices
       and create the socket thread that will listen
       for connections (if it doesn't already exist) */

    obtain_lock( &bind_lock );

    was_list_empty = IsListEmpty( &bind_head );

    InsertListTail( &bind_head, &bs->bind_link );

    if ( was_list_empty )
    {
        if ( create_thread( &sysblk.socktid, JOINABLE,
                            socket_thread, NULL, "socket_thread" ) )
            {
                logmsg( _( "HHCSD023E Cannot create socketdevice thread: errno=%d: %s\n" ),
                        errno, strerror( errno ) );
                RemoveListEntry( &bs->bind_link );
                close_socket(bs->sd);
                free( bs->spec );
                free( bs );
                release_lock( &bind_lock );
                return 0; /* (failure) */
            }
    }

    SIGNAL_SOCKDEV_THREAD();

    release_lock( &bind_lock );

    logmsg (_("HHCSD004I Device %4.4X bound to socket %s\n"),
        dev->devnum, dev->bs->spec);

    return 1;   /* (success) */
}
Exemplo n.º 11
0
/*
 * logger -- read and log utility
 *
 *	Reads from an input and arranges to write the result on the system
 *	log.
 */
int main(int argc, char **argv)
{
	int LogSock;
	int ch, pri = LOG_NOTICE;
	char *tag = NULL;
	uint16_t port = 0;

	char buf[MAX_LNE_LEN];
	int prilen, hdrlen;

	static const struct option longopts[] = {
		{ "priority",	required_argument,  0, 'p' },
		{ "tag",	required_argument,  0, 't' },
		{ "port",	required_argument,  0, 'P' },
		{ "help",	no_argument,	    0, 'h' },
		{ NULL,		0, 0, 0 }
	};

	while ((ch = getopt_long(argc, argv, "p:st:P:Vh", longopts, NULL)) != -1) {
		int iport = 0;
		switch (ch) {
		case 'p':		/* priority */
			pri = pencode(optarg);
			break;
		case 't':		/* tag */
			tag = optarg;
			if(strlen(tag) > MAX_TAG_LEN)
				errx(EXIT_FAILURE,
				     "tag must be no longer than %d", MAX_TAG_LEN);
			break;
		case 'P':
			iport = atoi(optarg);
			if(iport > (uint16_t)-1 || iport <= 0)
				errx(EXIT_FAILURE, "Invalid port");
			port = iport;
			break;
		case 'h':
			usage(stdout);
			exit(EXIT_SUCCESS);
		case '?':
		default:
			usage(stderr);
			exit(EXIT_FAILURE);
		}
	}
	argc -= optind;
	argv += optind;

	if(port == 0)
		errx(EXIT_FAILURE, "port is required.");

	if(tag == NULL)
		errx(EXIT_FAILURE, "tag is required.");

	if (argc > 0)
		errx(EXIT_FAILURE, "extra command line arguments.");

	/* setup for logging */
	LogSock = inet_socket(port);
	gen_header(buf, pri, tag, &prilen, &hdrlen);
	log_from_stdin(LogSock, buf, prilen, hdrlen);
	close(LogSock);
	return EXIT_SUCCESS;
}
Exemplo n.º 12
0
/*-------------------------------------------------------------------*/
int bind_device_ex (DEVBLK* dev, char* spec, ONCONNECT fn, void* arg )
{
    bind_struct* bs;
    int was_list_empty;
    int rc;

    if (!init_done) init_sockdev();

    if (sysblk.shutdown) return 0;

    logdebug("bind_device (%4.4X, %s)\n", dev->devnum, spec);

    /* Error if device already bound */
    if (dev->bs)
    {
        WRMSG (HHC01041, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, dev->bs->spec);
        return 0;   /* (failure) */
    }

    /* Create a new bind_struct entry */
    bs = malloc(sizeof(bind_struct));

    if (!bs)
    {
        char buf[40];
        MSGBUF( buf, "malloc(%d)", (int)sizeof(bind_struct));
        WRMSG (HHC01000, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, buf, strerror(errno));
        return 0;   /* (failure) */
    }

    memset(bs, 0, sizeof(bind_struct));

    bs->fn  = fn;
    bs->arg = arg;

    if (!(bs->spec = strdup(spec)))
    {
        WRMSG (HHC01000, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, "strdup()", strerror(errno) );
        free (bs);
        return 0;   /* (failure) */
    }

    /* Create a listening socket */
    if (bs->spec[0] == '/') bs->sd = unix_socket (bs->spec);
    else                    bs->sd = inet_socket (bs->spec);
    if (bs->sd == -1)
    {
        /* (error message already issued) */
        free( bs->spec );
        free( bs );
        return 0; /* (failure) */
    }

    /* Chain device and bind_struct to each other */
    dev->bs = bs;
    bs->dev = dev;

    /* Add the new entry to our list of bound devices
       and create the socket thread that will listen
       for connections (if it doesn't already exist) */

    obtain_lock( &bind_lock );

    was_list_empty = IsListEmpty( &bind_head );

    InsertListTail( &bind_head, &bs->bind_link );

    if ( was_list_empty )
    {
        rc = create_thread( &sysblk.socktid, JOINABLE,
                            socket_thread, NULL, "socket_thread" );
        if (rc)
        {
            WRMSG(HHC00102, "E", strerror( rc ) );
            RemoveListEntry( &bs->bind_link );
            close_socket(bs->sd);
            free( bs->spec );
            free( bs );
            release_lock( &bind_lock );
            return 0; /* (failure) */
        }
    }

    SIGNAL_SOCKDEV_THREAD();

    release_lock( &bind_lock );

    WRMSG (HHC01042, "I", SSID_TO_LCSS(dev->ssid), dev->devnum, dev->bs->spec);

    return 1;   /* (success) */
}