Пример #1
0
void check_and_forward(FILE *fp,  FILE *sfp, int s)
{
  	char buf[MAX_LINE];
	char esc_char= ESC_CHAR;
	int len=0, total_len=0, ack_len=0;

	total_len=0;

	/* reset the EOF */
	clearerr(fp);

	while(fgets(buf, sizeof(buf), fp)){
		len = strlen(buf);
		total_len += len;

		if(buf[0]==ESC_CHAR || buf[0]==CMD_CHAR || buf[0]== '\n'){
			total_len ++;
			if(writen(s, &esc_char, 1)<1){
				syslog(LOG_CRIT|FACILITY,
				       "sending messages failed: %s",strerror(errno));
				sig_term(1);
			}
		}
		if(writen(s, buf, len)<len){
			syslog(LOG_CRIT|FACILITY,
			       "sending messages failed: %s",strerror(errno));
			sig_term(1);
		}
	}
	if(total_len>0){
		/********************* IMPORTANT ************************/
		/*** Here send one additional '\n' to ensure boundary ***/
		/********************************************************/
		/* send "segment boundary information"
		   solicit acknowledgement */
		sprintf(buf, "\n%c\n", CMD_CHAR);
		len= 3;		
		if(writen(s, buf, len)<len){
			syslog(LOG_CRIT|FACILITY,
			       "sending messages failed: %s",strerror(errno));
			sig_term(1);
		}
		if((ack_len= readline(s, buf, sizeof(buf)))<0){
			syslog(LOG_CRIT|FACILITY,
			       "cannot get acks, %s",strerror(errno));
			sig_term(1);
		}
		if(buf[0]==CMD_CHAR && atoi(buf+1)== total_len+3)
			save_offset(sfp, ftell(fp));
		else{ /* this should not happen */
			syslog(LOG_CRIT|FACILITY,
			       "sending messages failed unexpectedly");
			sig_term(1);
		}
		
	}		
	
}
Пример #2
0
/* to stop VPCS from another process on Windows
 */
BOOL WINAPI
handler_routine(DWORD e)
{
	if (e == CTRL_BREAK_EVENT) {
		sig_term(21);
		return TRUE;
	}
	return FALSE;
}
Пример #3
0
static void sig_leave(int code) {
	if (opt != NULL && opt->state._hts_in_mirror) {
		signal(code, sig_term); // quitter si encore
		printf("\n** Finishing pending transfers.. press again ^C to quit.\n");
		if (opt != NULL) {
			// ask for stop
			opt->state.stop = 1;
		}
	} else {
		sig_term(code);
	}
}
Пример #4
0
static void sig_leave(int code) {
  if (global_opt != NULL && global_opt->state._hts_in_mirror) {
    signal(code, sig_term);     // quitter si encore
    printf("\n** Finishing pending transfers.. press again ^C to quit.\n");
    if (global_opt != NULL) {
      // ask for stop
      hts_log_print(global_opt, LOG_ERROR, "Exit requested by shell or user");
      global_opt->state.stop = 1;
    }
  } else {
    sig_term(code);
  }
}
Пример #5
0
/* reset offset to 0, reopen the file that needs to be forwarded */
static void
sig_hup(int signo)
{
	/* make sure, drain up the messages in old file */
	check_and_forward(pFile, pStatusFile, s);
	syslog(LOG_INFO|FACILITY,
	       "restart tcppipe and set reading position to 0");
	fclose(pFile);
	fclose(pStatusFile);
	
	if((pFile= fopen(full_filename,"r"))==NULL){
		syslog(LOG_CRIT|FACILITY,
		       "can't open file %s", full_filename);
		sig_term(1);
	}
	if((pStatusFile= fopen(state_filename, "w+"))==NULL){
		syslog(LOG_CRIT|FACILITY,
		       "error in opening state file %s, cannot continue",
		       state_filename);
		sig_term(1);
	}
}
Пример #6
0
void krb5_finalize(struct tevent_context *ev,
                   struct tevent_signal *se,
                   int signum,
                   int count,
                   void *siginfo,
                   void *private_data)
{
    char *realm = (char *)private_data;
    int ret;

    ret = remove_krb5_info_files(se, realm);
    if (ret != EOK) {
        DEBUG(1, ("remove_krb5_info_files failed.\n"));
    }

    sig_term(signum);
}
Пример #7
0
static void process_remote(struct device_settings settings, const char *mac, int modes)
{
    int br;
    bool msg = true;
    unsigned char buf[128];

    int last_time_action = get_time();

    while (!io_canceled()) {
        br = read(isk, buf, sizeof(buf));
        if (msg) {
            syslog(LOG_INFO, "Connected 'PLAYSTATION(R)3 Remote (%s)'", mac);
            msg = false;
        }

        if (settings.timeout.enabled) {
            int current_time = get_time();
            if (was_active()) {
                last_time_action = current_time;
                set_active(false);
            } else if (current_time-last_time_action >= settings.timeout.timeout) {
                syslog(LOG_INFO, "Remote was not in use, and timeout reached, disconneting...");
                sig_term(0);
                break;
            }
        }

        if (br < 0) {
            break;
        } else if (br==13 && buf[0]==0xa1 && buf[1]==0x01) { //only continue if we've got a Remote
            if (settings.joystick.enabled) do_joystick(ufd->js, buf, settings.joystick);
            if (settings.remote.enabled) do_remote(ufd->mk, buf, modes);
            if (settings.input.enabled) do_input(ufd->mk, buf, settings.input);
        } else {
            if (debug) syslog(LOG_ERR, "Non-Remote packet received and ignored (0x%02x|0x%02x|0x%02x)", buf[0], buf[1], buf[2]);
        }
    }

    if (debug) syslog(LOG_ERR, "Read loop was broken on the Remote process");
}
Пример #8
0
bool qapp::x11EventFilter(XEvent *event)  
{
	xwindow *client;
	Window w;
	XEvent ev;
	XConfigureRequestEvent *cev;
	XClientMessageEvent *mev;
	XCrossingEvent *xev;
	XCirculateRequestEvent *rev;
	XPropertyEvent *pev;
			
#ifdef DEBUGMSG		
#include "eventnames.h"
	if(event->type < 36)
		logmsg << "Received: " << event_names[event->type] << " (WId:" << event->xany.window << ")\n";
#endif
	while(waitpid(-1, NULL, WNOHANG) > 0);

	if(sighup)
	{
		wm_restart();
		tb_mn->readmenu();
		read_cprops();
		sighup = FALSE;
	}

	switch(event->type)
	{
		case DestroyNotify:
			w = event->xdestroywindow.window;

			if((client = cwindows.find(w)) != NULL)
			{
				clients.remove(client);
				
				if(smode && client->isstate())
					keyboard::tscreen();  // turn off screen mode

				tb_pg->draw_pager();
				
				return TRUE;
			}	
			if(tb_ap->remove(w))  // client on toolbar
				return TRUE;
				
			if(event->xdestroywindow.event != w)
				return TRUE;

			if(w == tb->winId() || w == tb_pg->winId() || w == tb_wl->winId() || w == tb_mn->winId() || w == tb_pb->winId())
				sig_term(SIGTERM);

			return FALSE;

		case MapNotify:
			if(event->xmap.event != event->xmap.window)
				return TRUE;
		
			if((client = pwindows.find(event->xmap.window)) != NULL)
				tb_pg->add(client);  // add to pager

			return FALSE;

		case UnmapNotify:
			if((client = cwindows.find(event->xunmap.window)) != NULL)
			{
				if(event->xunmap.send_event)
				{
					// client requested transitions 
					// normal -> withdrawn
					// iconic -> withdrawn
					
					client->withdraw();
				}
				else
					client->unmap();
				
				return TRUE;	
			}
			if(event->xunmap.event != event->xunmap.window)
				return TRUE;

			if(pwindows.find(event->xunmap.window) != NULL)
				tb_pg->draw_pager();
		
			return FALSE;

		case EnterNotify:
			xev = &event->xcrossing;
			
			if(event->xcrossing.window == qt_xrootwin())
			{
				stopautofocus();
				rootptr = TRUE;
			}
			else if(mrb == FALSE && menu_open == FALSE && (client = (xwindow *)widgetAt(xev->x_root, xev->y_root)) != NULL && 
			clients.find(client) != -1 && ((client = clients.current()) != focusclient || rootptr))
			{
				rootptr = FALSE;
				setinactive(client);  // old client to inactive, save new client
				
				if(xev->detail != NotifyInferior)
					client->startautofocus();

				client->setchildfocus(xev->time);
				client->setcmapfocus();
			}
			return FALSE;

		case ColormapNotify:
			if((client = cwindows.find(event->xcolormap.window)) != NULL)
			{
				client->setcmap(event->xcolormap.colormap);
				return TRUE;
			}	
			return FALSE;

		case PropertyNotify:
			pev = &event->xproperty;
			
			if((client = cwindows.find(pev->window)) != NULL)
			{
				if(pev->atom == XA_WM_NORMAL_HINTS)
				{
					client->get_wmnormalhints();
				}	
				else if(pev->atom == XA_WM_HINTS)
				{
					client->get_wmhints();
				}
				else if(pev->atom == XA_WM_NAME || pev->atom == XA_WM_ICON_NAME)
				{
					client->get_wmname();
				}
				else if(pev->atom == wm_colormaps)
				{
					client->get_colormaps();
					
					if(client == focusclient)
						client->setcmapfocus();
				}
				return TRUE;
			}
			return FALSE;

		case ConfigureNotify:
			if(event->xconfigure.event != event->xconfigure.window)
				return TRUE;
				
			if((client = pwindows.find(event->xconfigure.window)) != NULL)
			{
				tb_pg->draw_pager();
				while(XCheckTypedEvent(qt_xdisplay(), ConfigureNotify, &ev));
			}
			return TRUE;

		case ReparentNotify:
			if((client = cwindows.find(event->xreparent.window)) != NULL &&
			event->xreparent.parent != client->winId())
			{
				clients.remove(client);
				tb_pg->draw_pager();
			}	
			return TRUE;
		
		case ButtonPress:
			w = event->xbutton.window;
			
			if(w == qt_xrootwin())  // set focus to root window
				XSetInputFocus(qt_xdisplay(), w, RevertToPointerRoot, CurrentTime);

			if(w == tb->winId() || w == tb_pb->winId() || w == tb_ap->winId())
				XRaiseWindow(qt_xdisplay(), tb->winId());

			if(w == qt_xrootwin() || w == tb_pg->winId())
				install_colormap(None);
				
			return FALSE;

		case FocusOut:
			if(menu_open) // Qt 2.2.4 does not seem to like this if a menu is open
				return TRUE;

			return FALSE;

		case ClientMessage:
			mev = &event->xclient;
			
			if(mev->message_type == wm_change_state && mev->format == 32 && 
			mev->data.l[0] == IconicState && (client = cwindows.find(mev->window)) != NULL)
				client->iconify();

			return TRUE;	

		case CirculateRequest:
			rev = &event->xcirculaterequest;
			
			if(rev->place == PlaceOnTop)
				XRaiseWindow(qt_xdisplay(), rev->window);
			else
				XLowerWindow(qt_xdisplay(), rev->window);
				
			return TRUE;

		case ConfigureRequest:
			cev = &event->xconfigurerequest;
			XWindowChanges wc;
			
			if((client = cwindows.find(cev->window)) != NULL)
			{
#ifdef DEBUGMSG
	logmsg << "configure request to client (WId:" << client->winId() << ")\n";
#endif	
				if(cev->value_mask & (CWWidth|CWHeight|CWX|CWY))
				{
					if(smode && client->isstate())
						keyboard::tscreen(); 
					
					if(! client->is_tiled() || client == tmaxclient)
					{	
						int cx,cy,cw,ch;

						if(cev->value_mask & CWWidth)
						cw = cev->width;
						else	
							cw = client->width();

						if(cev->value_mask & CWHeight)
							ch = cev->height;
						else
							ch = client->getcheight();

						if((cev->value_mask & CWX) && ! client->is_tiled())
							cx = cev->x;
						else
							cx = client->x();

						if((cev->value_mask & CWY) && ! client->is_tiled())
					    		cy = cev->y;
						else
							cy = client->y();
						
						client->resize_request(cx, cy, cw, ch);
					}
					cev->value_mask &= ~(CWWidth|CWHeight|CWX|CWY);
				}
				
				if(! cev->value_mask)
					return TRUE;

				wc.width = client->width();
				wc.height = client->height();
				wc.x = client->x();
				wc.y = client->y();
				wc.border_width = 0;
				wc.sibling = cev->above;
				wc.stack_mode = cev->detail;

				XConfigureWindow(qt_xdisplay(), client->winId(), cev->value_mask, &wc);
				send_configurenotify(client);
			}
			else  // never mapped window
			{
				if(cev->window == tb->winId() || tb_ap->client_exists(cev->window))  // deny requests on toolbar
					return TRUE;

#ifdef DEBUGMSG
	logmsg << "configure request to unreparented window (WId:" << cev->window << ")\n";

#endif	

				wc.x = cev->x;
				wc.y = cev->y;
				wc.width = cev->width;
				wc.height = cev->height;
				cev->value_mask &= (CWX|CWY|CWWidth|CWHeight);
				
				XConfigureWindow(qt_xdisplay(), cev->window, cev->value_mask, &wc);	
			}
			return TRUE;
			
		case MapRequest:
			run_client(event->xmaprequest.window);
			return TRUE;
			
		case KeyPress:
			return(keyboard::keypress(&event->xkey));

		default:
			if(servershapes && event->type == (ShapeEventBase + ShapeNotify))
			{
				XShapeEvent *sev = (XShapeEvent *)event;
		
				if((client = cwindows.find(sev->window)) != NULL)
				{
					client->reshape();
					return TRUE;
				}	
			}
	}
	return FALSE;
}
Пример #9
0
int main(int argc, char *argv[])
{
  	extern char *optarg;
  	int opt; /* command line option */
  	int DEBUG=0, new_status_file=0, show_pid=0, errflag=0, ret=0;
	
  	int server_port= DEFAULT_SERVER_PORT;
	struct sockaddr_in sin;
	struct hostent *hp;

	char *filename= NULL;
	char *hostname= NULL; /* host running tcppiped*/
	long offset=0;

	/* time interval for checking file status */
	int interval= DEFAULT_TIME_INTERVAL;
 
	int i;

	/* get the command options */
  	/* tcppipe -f <filename> -h <hostname> [-p <port number>] 
	   [-t <time interval(seconds)>] -n -d */
  	while((opt = getopt(argc, argv, "f:h:p:t:ndI")) != EOF)
    		switch(opt){
    		case 'd':
      			DEBUG=1;
      			break;
    		case 'f':
      			filename= optarg;
			break;
    		case 'h':
			hostname= optarg;
      			break;
		case 'p':
			server_port=atoi(optarg);
			break;
		case 't':
			interval= atoi(optarg);
			if( interval <1){
				fprintf(stderr,
				       "Invalid time interval, at least one second\n");
				exit(1);
			}
			break;
		case 'n':
			new_status_file++;
			break;

		case 'I':
			show_pid++;
			break;
		default:
      			errflag=1;
      			break;
    		}

  
	if(errflag|| filename==NULL || hostname==NULL|| argc> optind){
		fprintf(stderr, "Bad arguments, Usage:\ntcppipe -f <filename> -h <hostname> [-p port#] [-t] [-n] [-d] [-I]\n");
		exit(1);
	}

	/* get the full pathname of the file that needs forwarding */
	realpath(filename, full_filename);

	/* make up the names of status file and pid file */
	base_state_filename(state_filename, RUNTIME_FILE_DIR,
			    hostname, server_port, full_filename);
	
	strcpy(pid_filename, state_filename);

	/* make up state_filename, /tmp/"hostname-#.filename.hashvalue.pipe"*/
	strcat(state_filename, STATE_FILE_SUFFIX);

	
	/* make up state_filename, /tmp/"hostname-#.filename.hashvalue.pid"*/
	strcat(pid_filename, PID_FILE_SUFFIX);

	/* check if there is already a process running with the same parameters */
	if(test_file(pid_filename)){
		if(show_pid){
			char buf[MAX_LINE];
			pPIDFile= fopen(pid_filename, "r");
			fgets(buf, sizeof(buf),  pPIDFile);
			printf("%s", buf);
			fclose(pPIDFile);
			exit(0);
		}
		fprintf(stderr,
			"WARNING: Another copy of tcppipe is running with the same parameters\n");
		
	}else{
		/* there is no process running with the same parameters */
		if(show_pid)
			exit(1);
	}
	if((pFile= fopen(full_filename,"r"))==NULL){
                syslog(LOG_CRIT|FACILITY,
                       "can't open file %s", filename);
		fprintf(stderr,
		       "can't open file %s\n", filename);
		exit(1);
	}

	if( (hp = gethostbyname(hostname)) ==0){
                syslog(LOG_CRIT|FACILITY,
                       "unknown host %s", hostname);
		fprintf(stderr, 
		       "unknown host %s\n", hostname);
		exit(1);
	}

	/* initialize host address */	
	memset(&sin, 0, sizeof(struct sockaddr_in));
	memcpy((char*)&sin.sin_addr, hp->h_addr,hp->h_length);
	sin.sin_family = AF_INET;
	sin.sin_port = htons(server_port);
	
	/* initialize socket */
	if((s=socket(AF_INET, SOCK_STREAM, 0)) < 0 ){
                syslog(LOG_CRIT|FACILITY,
                       "can not open a socket");
		fprintf(stderr,
		       "can not open a socket\n");
		exit(1);
	}
	
	
	/* connect to host */
	if( connect( s, (struct sockaddr *)&sin, sizeof(sin))<0){
                syslog(LOG_CRIT|FACILITY,
                       "cannot connect to host %s: %s",
                       hostname, strerror(errno));
		fprintf(stderr,
		       "cannot connect to host %s: %s\n", 
		       hostname, strerror(errno));
		exit(1);
	}

	
	/* do handshake with host */
	if((ret=do_handshake(s, full_filename))<0){
                syslog(LOG_CRIT|FACILITY,
                       "handshake with host %s failed", hostname);
		fprintf(stderr,
		       "handshake with host %s failed\n", hostname);
		exit(1);
	}
	else if(ret==0){
                syslog(LOG_CRIT|FACILITY,
                       "the pipe connection is refused by host %s", hostname);
		fprintf(stderr,
		       "the pipe connection is refused by host %s\n", hostname);
		exit(1);
	}
        syslog(LOG_INFO|FACILITY,
        	"Established pipe (%s) to host %s", filename, hostname);
	fprintf(stderr,
                "Established pipe (%s) to host %s\n", filename, hostname);
		
	if( new_status_file || (pStatusFile= fopen(state_filename, "r+")) == NULL){
		if((pStatusFile=fopen(state_filename, "w+"))==NULL){
                        syslog(LOG_CRIT|FACILITY,
                       "error in opening state file %s, cannot continue",
                               state_filename);
 
			fprintf(stderr,
		       "error in opening state file %s, cannot continue\n",
			       state_filename);
			exit(1);
		}
	}

	/* load file, and seek to previous point */
	offset = get_offset(pStatusFile);

	if( offset<0){
                syslog(LOG_CRIT|FACILITY,
                       "corrupted status file %s, exit...",
                       state_filename);
		fprintf(stderr,
		       "corrupted status file %s, exit...\n",
		       state_filename);
		exit(1);
	}

	fseek(pFile, 0, SEEK_END);
	if( offset> ftell(pFile)){
		if(DEBUG){
			syslog(LOG_DEBUG|FACILITY,	
		       "file %s was truncated, reset reading position to 0",
		       full_filename);
			fprintf(stderr,
                       "file %s was truncated, reset reading position to 0\n",
                       full_filename);
		}
		offset=0;
		fseek(pFile, 0, SEEK_SET);
	}
	else
		fseek(pFile, offset, SEEK_SET);

	/* make it a daemon process */
	if(!DEBUG && fork()){
		    exit(0);
	}

	if(!DEBUG)
		setsid();

	if((pPIDFile= fopen(pid_filename, "w")) != NULL) {
		fprintf(pPIDFile, "%d\n", getpid());
		fclose(pPIDFile);
	}else{
		syslog(LOG_CRIT|FACILITY,
		       "failed to write pid file %s",
		       pid_filename);
		sig_term(1);
	}
	
	/* These  variables are only used by sig_pipe() */
	server_name = hostname;

	signal(SIGPIPE, sig_pipe);

	signal(SIGTERM, sig_term);

#ifdef __sun
	sigset(SIGHUP, sig_hup);
#else
	signal(SIGHUP, sig_hup);
#endif

	while(1){
		check_and_forward(pFile, pStatusFile, s);
		sleep(interval);
	}
}
Пример #10
0
/* catch signal SIGPIPE, when the server process closed the socket,
   and tcppipe attempted to write to the socket
*/
static void
sig_pipe(int signo)
{
	syslog(LOG_CRIT|FACILITY,"tcppiped process on %s terminated prematurely", server_name);
	sig_term(1);
}