Example #1
0
File: print.c Project: y-c/APUE
int
main(int argc, char *argv[])
{
	int				fd, sockfd, err, text, c;
	struct stat		sbuf;
	char			*host;
	struct addrinfo	*ailist, *aip;

	err = 0;
	text = 0;
	while ((c = getopt(argc, argv, "t")) != -1) {
		switch (c) {
		case 't':
			text = 1;
			break;

		case '?':
			err = 1;
			break;
		}
	}
	if (err || (optind != argc - 1))
		err_quit("usage: print [-t] filename");
	if ((fd = open(argv[optind], O_RDONLY)) < 0)
		err_sys("print: can't open %s", argv[1]);
	if (fstat(fd, &sbuf) < 0)
		err_sys("print: can't stat %s", argv[1]);
	if (!S_ISREG(sbuf.st_mode))
		err_quit("print: %s must be a regular file\n", argv[1]);

	/*
	 * Get the hostname of the host acting as the print server.
	 */
	if ((host = get_printserver()) == NULL)
		err_quit("print: no print server defined");
	if ((err = getaddrlist(host, "print", &ailist)) != 0)
		err_quit("print: getaddrinfo error: %s", gai_strerror(err));

	for (aip = ailist; aip != NULL; aip = aip->ai_next) {
		if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
			err = errno;
		} else if (connect_retry(sockfd, aip->ai_addr,
		  aip->ai_addrlen) < 0) {
			err = errno;
		} else {
			submit_file(fd, sockfd, argv[1], sbuf.st_size, text);
			exit(0);
		}
	}
	errno = err;
	err_ret("print: can't contact %s", host);
	exit(1);
}
Example #2
0
/*
 * Return the address of the network printer or NULL on error.
 *
 * LOCKING: none.
 */
struct addrinfo *
get_printaddr(void)
{
	int				err;
	char			*p;
	struct addrinfo	*ailist;

	if ((p = scan_configfile("printer")) != NULL) {
		if ((err = getaddrlist(p, "ipp", &ailist)) != 0) {
			log_msg("no address information for %s", p);
			return(NULL);
		}
		return(ailist);
	}
	log_msg("no printer address specified");
	return(NULL);
}
Example #3
0
/*
 * Main print server thread.  Accepts connect requests from
 * clients and spawns additional threads to service requests.
 *
 * LOCKING: none.
 */
int
main(int argc, char *argv[])
{
	pthread_t			tid;
	struct addrinfo		*ailist, *aip;
	int					sockfd, err, i, n, maxfd;
	char				*host;
	fd_set				rendezvous, rset;
	struct sigaction	sa;
	struct passwd		*pwdp;

	if (argc != 1)
		err_quit("usage: printd");
	daemonize("printd");

	sigemptyset(&sa.sa_mask);
	sa.sa_flags = 0;
	sa.sa_handler = SIG_IGN;
	if (sigaction(SIGPIPE, &sa, NULL) < 0)
		log_sys("sigaction failed");
	sigemptyset(&mask);
	sigaddset(&mask, SIGHUP);
	sigaddset(&mask, SIGTERM);
	if ((err = pthread_sigmask(SIG_BLOCK, &mask, NULL)) != 0)
		log_sys("pthread_sigmask failed");

	n = sysconf(_SC_HOST_NAME_MAX);
	if (n < 0)	/* best guess */
		n = HOST_NAME_MAX;
	if ((host = malloc(n)) == NULL)
		log_sys("malloc error");
	if (gethostname(host, n) < 0)
		log_sys("gethostname error");

	if ((err = getaddrlist(host, "print", &ailist)) != 0) {
		log_quit("getaddrinfo error: %s", gai_strerror(err));
		exit(1);
	}
	FD_ZERO(&rendezvous);
	maxfd = -1;
	for (aip = ailist; aip != NULL; aip = aip->ai_next) {
		if ((sockfd = initserver(SOCK_STREAM, aip->ai_addr,
		  aip->ai_addrlen, QLEN)) >= 0) {
			FD_SET(sockfd, &rendezvous);
			if (sockfd > maxfd)
				maxfd = sockfd;
		}
	}
	if (maxfd == -1)
		log_quit("service not enabled");

	pwdp = getpwnam(LPNAME);
	if (pwdp == NULL)
		log_sys("can't find user %s", LPNAME);
	if (pwdp->pw_uid == 0)
		log_quit("user %s is privileged", LPNAME);
	if (setgid(pwdp->pw_gid) < 0 || setuid(pwdp->pw_uid) < 0)
		log_sys("can't change IDs to user %s", LPNAME);

	init_request();
	init_printer();

	err = pthread_create(&tid, NULL, printer_thread, NULL);
	if (err == 0)
		err = pthread_create(&tid, NULL, signal_thread, NULL);
	if (err != 0)
		log_exit(err, "can't create thread");
	build_qonstart();

	log_msg("daemon initialized");

	for (;;) {
		rset = rendezvous;
		if (select(maxfd+1, &rset, NULL, NULL, NULL) < 0)
			log_sys("select failed");
		for (i = 0; i <= maxfd; i++) {
			if (FD_ISSET(i, &rset)) {
				/*
				 * Accept the connection and handle the request.
				 */
				if ((sockfd = accept(i, NULL, NULL)) < 0)
					log_ret("accept failed");
				pthread_create(&tid, NULL, client_thread,
				  (void *)((long)sockfd));
			}
		}
	}
	exit(1);
}
Example #4
0
int main(int argc, char *argv[])
{
	int fd, sfd, err, c;
	struct stat sbuf;
	char *host, *file;
	struct addrinfo	*ailist, *aip;
	char *orient;
	long jobid;
	int text;
	int sides;		/* 0=default 1=one-sided */
				/* 2=two-sided-long-edge  3=two-sided-short-edge */
	
	sides = 0;
	text = 0;
	err = 0;
	jobid = -1;
	while ((c = getopt(argc, argv, "is:to:c:h")) != -1) {
		switch (c) {
		case 'h':
			usage();
			exit(0);
		case 'c':
			jobid = atol(optarg);
			break;
		case 'i':
			job_status();
			exit(0);
		case 's':
			sides = atol(optarg);
			if (sides < 1 || sides > 3)
				err_sys("print: invalid sides option");
			break;
		case 'o':
			orient = optarg;
			break;
		case 't':
			text = 1;
			break;

		case '?':
			err = 1;
			break;
		}
	}
	if (err || (optind != argc - 1))
		err_quit("usage: print [-t] filename");
	file = argv[optind];
	if ((fd = open(file, O_RDONLY)) < 0)
		err_sys("print: can't open %s", file);
	if (fstat(fd, &sbuf) < 0)
		err_sys("print: can't stat %s", file);
	if (!S_ISREG(sbuf.st_mode))
		err_quit("print: %s must be a regular file", file);
	/*
	 * Get the hostname of the host acting as the print server.
	 */
	if ((host = get_printserver()) == NULL)
		err_quit("print: no print server defined");
	if ((err = getaddrlist(host, "print", &ailist)) != 0)
		err_quit("print: getaddrinfo error: %s", gai_strerror(err));

	for (aip = ailist; aip != NULL; aip = aip->ai_next) {
		if ((sfd = connect_retry(AF_INET, SOCK_STREAM, 0,
		  aip->ai_addr, aip->ai_addrlen)) < 0) {
			err = errno;
		} else {
			if (jobid != -1) /* cancel job */
				cancel_job(sfd, jobid);
			else 
				submit_file(fd, sfd, file, sbuf.st_size,
					    text, orient, sides);
			exit(0);
		}
	}
	err_exit(err, "print: can't contact %s", host);
}
Example #5
0
int main(int argc, char **argv) {

	int 	fd;
	//time_t	ticks;
	//char	buff[MAXLINE];
	struct addrinfo	*ai;
	int socktype;
	char port[5];
	/*char *filename;
	out = FALSE;
	verbose = 0; */

	char *server_name = argv[1];


	time_record_tail = &time_record_head; /* tail points to the first element,
						 i.e. the head of the list */
	seqlist_tail = &seqlist_head;
	seqlist_tail->next = NULL;

	/* default values for port and socktype */
	strncpy(port, "5001", 4);
	socktype = SOCK_STREAM;

	if (argc < 2) {
		usage();
	}



	argc--; argv++; /* skip program name */
	argc--; argv++; /* skip source address or name */

	while( argc > 0 && argv[0][0] == '-' )  {
		switch (argv[0][1]) {
			case 'u': socktype = SOCK_DGRAM;
				break;
			case 't': socktype = SOCK_STREAM;
				break;
			case 'p': strncpy(port, &argv[0][2], 5);
				break;
			case 'l': //strncpy(port, &argv[0][2], 5);
				buflen = atoi(&argv[0][2]);
				break;
			/*
			case 'v': verbose++;
				break;
			case 'o': // next string specifies a filename
				strncpy(filename,&argv[1][0] , MAXFILENAME);
				argv++; argc--; // skip filename
				out = TRUE;
				break;*/
			default:
				usage();
		};
		argv++; argc--;
	}

	/* open output file if specified */
	/*
	if(out) {
		output = fopen(filename, "w");
		if(!output) {
			fprintf(stderr, "Failed opening file for output %s: ", filename);
			perror("");
		}

	}*/

	ai = getaddrlist(server_name, port, AI_PASSIVE, AF_UNSPEC, socktype);


	fd = init_sock(ai);
	check("init_socket", fd);


	if(socktype == SOCK_STREAM) {
		printf("Waiting for TCP connection on port %s\n", port);
		read_tcp(fd);
	}
	else if(socktype == SOCK_DGRAM) {
		printf("Waiting for UDP connection on port %s\n", port);
		read_udp(fd);
	}



	/*
	if(output) {
		if(fclose(output)) {
			fprintf(stderr, "Failed closing file %s\n", filename);
			perror("");
		}
	}*/

	if(time_record_head.next != NULL)
		free_time_record(time_record_head.next);
	if(seqlist_head.next != NULL)
		free_seqlist(seqlist_head.next);

	return 0;
}