Example #1
0
int gettermios(struct termios *termp) { 
   int fd;
   int firsterrno = 0;
   char termbuf[L_ctermid];
             
   if (ctermid(termbuf) == NULL) {                /* find the terminal name */
      errno = ENODEV;
      return -1;
   }
   if ((fd = r_open2(termbuf, O_RDONLY)) == -1)        /* open the terminal */
      return -1;
   if (tcgetattr(fd, termp) == -1)                       /* get its termios */
      firsterrno = errno;
   if ((r_close(fd) == -1) && !firsterrno)
      firsterrno = errno;
   if (firsterrno) {
      errno = firsterrno;
      return -1;
   }
   return 0;
}
Example #2
0
int settermios(struct termios *termp) { 
   int error;
   int fd;
   int firsterrno = 0;
   char termbuf[L_ctermid];
   
   if (ctermid(termbuf) == NULL) {                /* find the terminal name */
      errno = ENODEV;
      return -1;
   }
   if ((fd = r_open2(termbuf, O_RDONLY)) == -1)        /* open the terminal */
      return -1;
   while (((error = tcsetattr(fd, TCSAFLUSH, termp)) == -1) && 
           (errno == EINTR)) ;
   if (error)
      firsterrno = errno;
   if ((r_close(fd) == -1) && !firsterrno)
      firsterrno = errno;
   if (firsterrno) {
      errno = firsterrno;
      return -1;
   }
   return 0;
}
int main (int argc, char * argv[]){
	
	int fd, i, j;
	char *req, *rel; 
	char buf[256];
	int n;
	long name_max;
	ssize_t amount_read;
	
	// Check for the proper number of arguments
	
	if (argc != 3){
		fprintf(stderr, "Usage: %s [barrier name] [barrier size]\n", argv[0]);
		return 1;
	}
	
	// The atoi() function has been deprecated by strtol().
	// strtol is mulithread safe on the lab's target machine.
	n = (int)strtol(argv[2], NULL, 10);  
	
	
	if ((name_max = allocate_filename_strings(&req, &rel)) < 2){
		return 1;
	}
	// Decrease name_max to allow room for ".request" and ".release" extensions
	name_max = name_max-9;
	// Make sure the filesystem can support the argument.
	if (strlen(argv[1]) > name_max){
		fprintf(stderr, "File name too long.");
		return 1;
	}
	
	strncpy(req, argv[1], name_max);
	strncpy(rel, argv[1], name_max);
	
	if (mkfifo(strcat(req,".request"), FIFO_PERMS) == -1){
		fprintf(stderr, "%s cannot be created.  May already exist!\n", req);
		//exit(0);
	}
	
	if (mkfifo(strcat(rel,".release"), FIFO_PERMS) == -1){
		fprintf(stderr, "%s cannot be created.  May already exist!\n", rel);
		//exit(0);
	}
	
	for (j = 0; j < 2; j++){
		// READING FROM NAME.REQUEST
		printf("Attempting to open request FIFO.\n");	
		// Open name.request
		if ((fd = r_open2(req, O_RDONLY)) == -1){ 
			fprintf(stderr, "Cannot open %s for reading\n", req);
			return 1;
		}else{
			printf("Opened %s for reading.\n", req);
		}
		for (i = 0; i < n; i++){
			// Read from name.request - put into buf, read n chars/bytes
			
			if ((amount_read = r_read(fd, buf, 1)) != 1){
				printf("Failed to read a byte from request FIFO.\n");
				//exit(0);
			}else{
				printf("Read %d byte from %s.\n", amount_read, req);
				sleep(1); //give the client process a chance to close the FIFO.
			}
		}
		
		
		// Close name.request
		if (r_close(fd) == -1){
			fprintf(stderr, "Cannot close %s!\n", req);
			exit(0);
		}else{
			printf("Closed %s.\n", req);
		}
		
		
		printf("Attempting to open %s.\n",rel);
		if ((fd = r_open2(rel, O_WRONLY)) == -1){
			fprintf(stderr, "Cannot open %s for writing\n", rel);
			return 1;
		}else{
			printf("Opened %s for writing.\n", rel);
		}
		
		printf("Attempting to write to %s.\n",rel);
		// Write to name.release from buf, write n chars/bytes
		for (i = 0; i < n; i++){
			if (r_write(fd, rel, 1) != 1){
				fprintf(stderr, "Cannot write to %s\n", rel);
				exit(0);
			}else{
				printf("Writing to %s...\n", rel);
				sleep(1); //give another client proces a chance to open the FIFO
			}
		}
		// Close name.release
		if (r_close(fd) == -1){
			fprintf(stderr, "Cannot close %s!\n", rel);
			exit(0);
		}else{
			printf("Closed %s.\n", rel);
		}
		
	}
	
	sleep(2); // Allow clients a chance to finish with FIFOs.
	if (unlink(req) == -1)
		perror("Server failed to unlink request FIFO");
	if (unlink(rel) == -1)
		perror("Server failed to unlink release FIFO");	
	return 1;
}
Example #4
0
unsigned long send_mailfile(char * mailfile,  ProxyContext *p)
{

    struct LineBuffer *filebuf, *footbuf;
    int mailfd, footfd;
    int res = 0, sendret = 0, gotprd = 0, gottxt = 0, nogo = 0;
    unsigned long len = 0;
    char svrout[1];

    if ((mailfd = r_open2(mailfile, O_RDONLY )) < 0)
    {
        proxy_context_uninit(p);
        // Emergency: send_mailfile: Can't open client mail file.
        g_gateway_config->emergency = make_message("Can't open mailfile (%s)!", mailfile);
        do_log(LOG_EMERG, "ERR: Can't open mailfile (%s)!", mailfile);
        return 0;
    }
    filebuf = linebuf_init(16384);
    footbuf = linebuf_init(512);
    if (!filebuf)
    {
        r_close(mailfd);

        if (p->ssl_enable)
        {
            SSL_shutdown(p->ssl);
            /* 释放SSL */
            SSL_free(p->ssl);
        }

        r_close(p->server_fd);
        proxy_context_uninit(p);
        // Emergency: send_mailfile: Unable to get memory.
        g_gateway_config->emergency = "Could not allocate memory for sending mail!";
        do_log(LOG_EMERG, "ERR: Could not allocate memory for sending mail!");
    }

    gotprd = 0;
    /*  // advance to mailfd pointer to past data already sent:
        if (config->broken)
        {
            if (p->hdroffset && !p->gobogus)
            {
                while (p->hdroffset)
                {
                    res = getlinep3(mailfd, filebuf, NULL);
                    p->hdroffset--;
                }
            }
        }
        else
        {
            if (p->hdroffset)
            {
                lseek(mailfd, p->hdroffset, SEEK_SET);
            }
            // See if bogus headerline sent
            if (p->gobogus)
            {
                if (p->boguspos < 91)
                {
                    svrout[0] = BOGUSX[p->boguspos];
                    secure_write(p->client_fd, svrout, 1, p->ssl_server);
                    p->boguspos++;
                }
                //now close it
                writeline(p->client_fd, WRITELINE_LEADING_RN, PERIOD, p->ssl_server);
                p->gobogus = 0;
            }
        }*/
    while (1)
    {
        /*
        sendret = checktimeout(p);
        if (sendret == GETLINE_PIPE)
        {
            do_log(LOG_CRIT, "ERR: Client disappeared during mail send!");
            linebuf_uninit(filebuf);
            return EPIPE;
        }
        else if (sendret)
        {
            context_uninit(p);
            linebuf_uninit(filebuf);
        // Emergency: send_mailfile: Sending mail to client
            g_gateway_config->emergency = "Sending mail to client";
            do_log(LOG_EMERG, "ERR: Sending mail to client");
            // we are dead now. Should not reach here. But allow it to fall through in case LOG_EMERG is changed in the future.
            return 1;
        }*/


        //to do check timeout

        if ((res = get_line(mailfd, filebuf)) < 0)
        {
            if (res == GETLINE_TOO_LONG)
            {
                // Buffer contains part of line,
                //take care of later
            }
            else
            {
                // Other error, take care of later
                break;
            }
        }
        if (filebuf->linelen >= 0 )
        {
            len += filebuf->linelen;
            //if (config->debug_message)
            do_log(LOG_DEBUG, ">%s", filebuf->line);

            if ((strncmp(filebuf->line, ".", 1 ) == 0 && strlen(filebuf->line) == 1))
                gotprd = 1;
            if ((strncmp(filebuf->line, ".\r", 2) == 0 && strlen(filebuf->line) == 2))
                gotprd = 1;

            /*
            //if ((strncmp(filebuf->line,"Content-Type: application/pgp-signature",39)==0 && strlen(filebuf->line)==39)) gotpgp=1;
            //if (gotpgp) nogo=1;
            if (strncmp(filebuf->line, "Content-Type: ", 14) == 0)
            {
                if ((strncmp(filebuf->line, "Content-Type: text/plain;", 25) == 0 && strlen(filebuf->line) == 25)) gottxt = 1;
                else nogo = 1;
            }
            */

            /*
            if ( gotprd && !nogo)
            {
                if ((footfd = r_open2(FOOTER, O_RDONLY)) >= 0)
                {
                    sendret = writeline(fd, WRITELINE_LEADING_RN, "**********", p->ssl_server);
                    while (1)
                    {
                        if ((sendret = getlinep3(footfd, footbuf, NULL)) < 0) break;
                        if (footbuf->linelen >= 0 ) sendret = writeline(fd, WRITELINE_LEADING_RN, footbuf->line, p->ssl_server);
                    }
                    r_close(footfd);
                    writeline_format(fd, NULL, WRITELINE_LEADING_RN, PROGRAM" "VERSION" running on %s.%s", paramlist_get(p->params, "%HOSTNAME%"), paramlist_get(p->params, "%DOMAINNAME%"));
                    sendret = writeline_format(fd, NULL, WRITELINE_LEADING_RN, "%s", paramlist_get(p->params, "%VDINFO%"));
                    sendret = writeline(fd, WRITELINE_LEADING_RN, "**********", p->ssl_server);
                }
            }*/

            // Take care of buffer here
            if (res == GETLINE_TOO_LONG)
            {

                if (p->ssl_enable)
                    sendret = write_line_ssl(p->ssl, WRITELINE_LEADING_NONE, filebuf->line);
                else
                    sendret = write_line(p->server_fd, WRITELINE_LEADING_NONE, filebuf->line);
            }
            else
            {
                if (!gotprd)
                {

                    if (p->ssl_enable)
                        sendret = write_line_ssl(p->ssl, WRITELINE_LEADING_RN, filebuf->line);
                    else
                        sendret = write_line(p->server_fd, WRITELINE_LEADING_RN, filebuf->line);
                }

            }


            if (sendret == GETLINE_PIPE)
            {
                do_log(LOG_CRIT, "ERR: Client disappeared during mail send!");
                linebuf_uninit(filebuf);
                return EPIPE;
            }
            else if (sendret)
            {
                proxy_context_uninit(p);
                linebuf_uninit(filebuf);
                // Emergency: send_mailfile: Sending mail to client
                g_gateway_config->emergency = "Sending mail to client";
                do_log(LOG_EMERG, "ERR: Sending mail to client");
                // we are dead now. Should not reach here. But allow it
                //to fall through in case LOG_EMERG is changed in the future.
                return 1;
            }
        }


    }

    if (res != GETLINE_EOF)
    {
        do_log(LOG_CRIT, "ERR: reading from mailfile %s, error code: %d", mailfile, res);
        linebuf_uninit(filebuf);
        return 1;
    }

    if (!gotprd)
    {
        do_log(LOG_DEBUG, "Wrote new EOM.");

        if (p->ssl_enable)
            write_line_ssl(p->ssl, WRITELINE_LEADING_RN, ".");
        else
            write_line(p->server_fd, WRITELINE_LEADING_RN, ".");
    }
    linebuf_uninit(filebuf);
    r_close(mailfd);
    return len;

}