int tcp_open( int port ) { int i, fd, err; struct sockaddr_in sinaddr; sinaddr.sin_family = AF_INET; sinaddr.sin_addr.s_addr = INADDR_ANY; sinaddr.sin_port = htons( port ); fd = socket( AF_INET, SOCK_STREAM, 0 ); #ifdef WINDOW_1 int windowsize=1024; setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *)&windowsize, sizeof(windowsize)); //aaaaaa=fopen("/tmp/qqqqq", "a"); //fprintf(aaaaaa, " monitor: tcp_send\n"); //fclose(aaaaaa); #endif Max_open(fd); err = errno; if( fd < 0 ){ FPRINTF(STDERR,"tcp_open: socket call failed - %s\n", Errormsg(err) ); return( -1 ); } i = Link_setreuse( fd ); if( i >= 0 ) i = bind( fd, (struct sockaddr *) & sinaddr, sizeof (sinaddr) ); if( i >= 0 ) i = listen( fd, 10 ); err = errno; if( i < 0 ){ FPRINTF(STDERR,"tcp_open: connect to '%s port %d' failed - %s\n", inet_ntoa( sinaddr.sin_addr ), ntohs( sinaddr.sin_port ), Errormsg(errno) ); close(fd); fd = -1; } if( fd == 0 ){ fd = dup(fd); if( fd < 0 ){ FPRINTF(STDERR,"tcp_open: dup failed - %s\n", Errormsg(errno) ); close(fd); fd = -1; } } return( fd ); }
static void use_syslog(int kind, char *msg) { /* testing mode indicates that this is not being used * in the "real world", so don't get noisy. */ /* we get rid of first set of problems - stupid characters */ char buffer[SMALLBUFFER]; plp_snprintf(buffer,sizeof(buffer)-1, "%s",msg); msg = buffer; #ifndef HAVE_SYSLOG_H /* Note: some people would open "/dev/console", as default Bad programmer, BAD! You should parameterize this and set it up as a default value. This greatly aids in testing for portability. Patrick Powell Tue Apr 11 08:07:47 PDT 1995 */ int Syslog_fd; if (Syslog_fd = open( Syslog_device_DYN, O_WRONLY|O_APPEND|O_NOCTTY, Spool_file_perms_DYN )) > 0 ) ){ int len; Max_open( Syslog_fd); len = safestrlen(msg); msg[len] = '\n'; msg[len+1] = 0; Write_fd_len( Syslog_fd, msg, len+1 ); close( Syslog_fd ); msg[len] = 0; } #else /* HAVE_SYSLOG_H */ # ifdef HAVE_OPENLOG /* use the openlog facility */ openlog(Name, LOG_PID | LOG_NOWAIT, SYSLOG_FACILITY ); syslog(kind, "%s", msg); closelog(); # else (void) syslog(SYSLOG_FACILITY | kind, "%s", msg); # endif /* HAVE_OPENLOG */ #endif /* HAVE_SYSLOG_H */ }
int udp_open( int port ) { int i, fd, err; struct sockaddr_in sinaddr; sinaddr.sin_family = AF_INET; sinaddr.sin_addr.s_addr = INADDR_ANY; sinaddr.sin_port = htons( port ); fd = socket( AF_INET, SOCK_DGRAM, 0 ); Max_open(fd); err = errno; if( fd < 0 ){ FPRINTF(STDERR,"udp_open: socket call failed - %s\n", Errormsg(err) ); return( -1 ); } i = -1; i = bind( fd, (struct sockaddr *) & sinaddr, sizeof (sinaddr) ); err = errno; if( i < 0 ){ FPRINTF(STDERR,"udp_open: bind to '%s port %d' failed - %s\n", inet_ntoa( sinaddr.sin_addr ), ntohs( sinaddr.sin_port ), Errormsg(errno) ); close(fd); fd = -1; } if( fd == 0 ){ fd = dup(fd); if( fd < 0 ){ FPRINTF(STDERR,"udp_open: dup failed - %s\n", Errormsg(errno) ); close(fd); fd = -1; } } return( fd ); }
static pid_t Make_lpd_call( const char *name, WorkerProc *proc, int passfd_count, int *passfd, struct line_list *args, int intern_logger, int intern_status, int intern_mail, int intern_lpd_request, int param_fd ) { int pid, fd, i, n, newfd; struct line_list env; Init_line_list(&env); pid = dofork(1); if( pid ){ return(pid); } Name = "LPD_CALL"; if(DEBUGL2){ LOGDEBUG("Make_lpd_call: name '%s', lpd path '%s'", name, Lpd_path_DYN ); LOGDEBUG("Make_lpd_call: passfd count %d", passfd_count ); for( i = 0; i < passfd_count; ++i ){ LOGDEBUG(" [%d] %d", i, passfd[i]); } Dump_line_list("Make_lpd_call - args", args ); } for( i = 0; i < passfd_count; ++i ){ fd = passfd[i]; if( fd < i ){ /* we have fd 3 -> 4, but 3 gets wiped out */ do{ newfd = dup(fd); Max_open(newfd); if( newfd < 0 ){ Errorcode = JABORT; logerr_die(LOG_INFO, "Make_lpd_call: dup failed"); } DEBUG4("Make_lpd_call: fd [%d] = %d, dup2 -> %d", i, fd, newfd ); passfd[i] = newfd; } while( newfd < i ); } } if(DEBUGL2){ LOGDEBUG("Make_lpd_call: after fixing fd count %d", passfd_count); for( i = 0 ; i < passfd_count; ++i ){ fd = passfd[i]; LOGDEBUG(" [%d]=%d",i,fd); } } for( i = 0; i < passfd_count; ++i ){ fd = passfd[i]; DEBUG2("Make_lpd_call: fd %d -> %d",fd, i ); if( dup2( fd, i ) == -1 ){ Errorcode = JABORT; logerr_die(LOG_INFO, "Make_lpd_call: dup2(%d,%d) failed", fd, i ); } } /* close other ones to simulate close_on_exec() */ n = Max_fd+10; for( i = passfd_count ; i < n; ++i ){ close(i); } Do_work( name, args, proc, intern_logger, intern_status, intern_mail, intern_lpd_request, param_fd ); /* not reached: */ return(0); }