Beispiel #1
0
static int prepare_buffer(int argc, char * argv[]) 
{

	if (argc<2) {
		usage();
		return -1;
	}
	if (strncmp(argv[1],"mount",5)==0) {
		return do_mount(argc,argv);
	} else if (strncmp(argv[1],"resume",6)==0) {
		return do_resume(argc,argv);
	} else if (strncmp(argv[1],"suspend",7)==0) {
		return do_suspend(argc,argv);

	} else if (strncmp(argv[1],"status",6)==0) {
		return do_status(argc,argv);

	} else if (strncmp(argv[1],"unmount",7)==0) {
		return do_unmount(argc,argv);
	} else if (strncmp(argv[1],"exit",4)==0) {
		return do_exit(argc,argv);

	} else {
		usage();
		return -1;
	}

	return 0;
}
Beispiel #2
0
libusb_device_handle * open_vid_pid(struct mach_id *mach, struct sdp_dev *p_id)
{
	int r = libusb_init(NULL);
	int err;
	libusb_device_handle *h;
	h = libusb_open_device_with_vid_pid(NULL, mach->vid, mach->pid);
	if (!h) {
		printf("%s:Could not open device vid=0x%x pid=0x%x\n", __func__,
				mach->vid, mach->pid);
		goto err1;
	}
	if (libusb_kernel_driver_active(h, 0))
		libusb_detach_kernel_driver(h, 0);
	err = libusb_claim_interface(h, 0);
	if (err) {
		printf("claim failed, err=%i\n", err);
		goto err2;
	}
	err = do_status(p_id);
	if (!err)
		return h;
	printf("status failed, err=%i\n", err);
err2:
	libusb_release_interface(h, 0);
	libusb_close(h);
err1:
	libusb_exit(NULL);
	return NULL;
}
Beispiel #3
0
int main(int argc, char *argv[], char *envp[])
{
	int rv;

	init_set_proc_title(argc, argv, envp);
	get_secs(&start_time);

	memset(&cl, 0, sizeof(cl));
	strncpy(cl.configfile,
			BOOTH_DEFAULT_CONF, BOOTH_PATH_LEN - 1);
	cl.lockfile[0] = 0;
	debug_level = 0;
	cl_log_set_entity("booth");
	cl_log_enable_stderr(TRUE);
	cl_log_set_facility(0);


	rv = read_arguments(argc, argv);
	if (rv < 0)
		goto out;


	switch (cl.type) {
	case STATUS:
		rv = do_status(cl.type);
		break;

	case ARBITRATOR:
	case DAEMON:
	case SITE:
		rv = do_server(cl.type);
		break;

	case CLIENT:
		rv = do_client();
		break;
	}

out:
	/* Normalize values. 0x100 would be seen as "OK" by waitpid(). */
	return (rv >= 0 && rv < 0x70) ? rv : 1;
}
Beispiel #4
0
int main(int argc, char * const argv[])
{
	struct sdp_dev *p_id;
	struct mach_id *mach;
	libusb_device **devs;
	libusb_device *dev;
	int r;
	int err;
	ssize_t cnt;
	libusb_device_handle *h = NULL;
	int config = 0;
	int verify = 0;
	struct sdp_work *curr;
	struct sdp_work *cmd_head = NULL;
	char const *conf;
	char const *base_path = get_base_path(argv[0]);
	char const *conf_path = "/etc/imx-loader.d/";

	err = parse_opts(argc, argv, &conf_path, &verify, &cmd_head);
	if (err < 0)
		return -1;

	// Get list of machines...
	conf = conf_file_name("imx_usb.conf", base_path, conf_path);
	if (conf == NULL)
		return -1;

	struct mach_id *list = parse_imx_conf(conf);
	if (!list)
		return -1;

	r = libusb_init(NULL);
	if (r < 0)
		goto out;

	cnt = libusb_get_device_list(NULL, &devs);
	if (cnt < 0)
		goto out;

//	print_devs(devs);
	dev = find_imx_dev(devs, &mach, list);
	if (dev) {
		err = libusb_open(dev, &h);
		if (err)
			printf("%s:Could not open device vid=0x%x pid=0x%x err=%d\n", __func__, mach->vid, mach->pid, err);
	}
	libusb_free_device_list(devs, 1);

	if (!h)
		goto out;

	// Get machine specific configuration file..
	conf = conf_file_name(mach->file_name, base_path, conf_path);
	if (conf == NULL)
		goto out;

	p_id = parse_conf(conf);
	if (!p_id)
		goto out;

	if (p_id->mode == MODE_HID)
		p_id->transfer = &transfer_hid;
	if (p_id->mode == MODE_BULK)
		p_id->transfer = &transfer_bulk;

	// USB private pointer is libusb device handle...
	p_id->priv = h;

	libusb_get_configuration(h, &config);
	printf("%04x:%04x(%s) bConfigurationValue =%x\n",
			mach->vid, mach->pid, p_id->name, config);

	if (libusb_kernel_driver_active(h, 0))
		 libusb_detach_kernel_driver(h, 0);

	err = libusb_claim_interface(h, 0);
	if (err) {
		printf("Claim failed\n");
		goto out;
	}
	printf("Interface 0 claimed\n");
	err = do_status(p_id);
	if (err) {
		printf("status failed\n");
		goto out;
	}

	// By default, use work from config file...
	curr = p_id->work;

	if (cmd_head != NULL)
		curr = cmd_head;

	if (curr == NULL) {
		printf("no job found\n"); 
		goto out;
	}

	while (curr) {
		if (curr->mem)
			perform_mem_work(p_id, curr->mem);
//		printf("jump_mode %x\n", curr->jump_mode);
		if (curr->filename[0]) {
			err = DoIRomDownload(p_id, curr, verify);
		}
		if (err) {
			err = do_status(p_id);
			break;
		}
		if (!curr->next && (!curr->plug || curr != cmd_head))
			break;
		err = do_status(p_id);
		printf("jump_mode %x plug=%i err=%i\n", curr->jump_mode, curr->plug, err);
		if (err) {
			int retry;
			/* Rediscovers device */
			libusb_release_interface(h, 0);
			libusb_close(h);
			libusb_exit(NULL);
			for (retry = 0; retry < 10; retry++) {
				printf("sleeping\n");
				sleep(3);
				printf("done sleeping\n");
				h = open_vid_pid(mach, p_id);
				if (h)
					break;
			}
			if (!h)
				goto out;
		}
		if (curr == cmd_head && curr->plug) {
			curr->plug = 0;
			continue;
		}
		curr = curr->next;
	}

exit:
	libusb_release_interface(h, 0);
out:
	if (h)
		libusb_close(h);
	libusb_exit(NULL);
	return 0;
}
int main(int argc, char * const argv[])
{
    int option_index;
    char command[1024];

    /* Initialize default strings */
    strcpy(vmlinux, "--no-vmlinux");
    strcpy(kernel_range, "");

    while (1) {
        int c = getopt_long(argc, argv, "", long_options, &option_index);
        if (c == -1) {
            break;
        }
        switch (c) {
            case 0:
                break;
            /* --event */
            case 'e':   
                if (num_events == MAX_EVENTS) {
                    fprintf(stderr, "More than %d events specified\n",
                            MAX_EVENTS);
                    exit(1);
                }
                if (process_event(optarg)) {
                    exit(1);
                }
                break;
            /* --vmlinux */
            case 'v':
                sprintf(vmlinux, "-k %s", optarg);
                break;
            /* --kernel-range */
            case 'r':
                sprintf(kernel_range, "-r %s", optarg);
                break;
            /* --shutdown */
            case 'h': {
                int pid = read_num(OP_DATA_DIR"/lock");
                if (pid >= 0) {
                    kill(pid, SIGKILL);
                }   
                setup_session_dir();
                break;
            }
            /* --status */
            case 't':
                do_status();
                break;
            default:
                usage();
                exit(1);
        }
    }
    verbose("list_events = %d\n", list_events);
    verbose("setup = %d\n", setup);

    if (list_events) {
        do_list_events();
    }

    if (quick) {
        process_event("CPU_CYCLES");
        setup = 1;
    }

    if (reset) {
        do_reset();
    }

    if (show_usage) {
        usage();
    }

    if (setup) {
        if (do_setup()) {
            fprintf(stderr, "do_setup failed");
            exit(1);
        }
    }

    if (num_events != 0) {
        int i;

        strcpy(command, "oprofiled --session-dir="OP_DATA_DIR);

#if !defined(WITH_ARM_V7_A)
        /* Since counter #3 can only handle CPU_CYCLES, check and shuffle the 
         * order a bit so that the maximal number of events can be profiled
         * simultaneously
         */
        if (num_events == 3) {
            for (i = 0; i < num_events; i++) {
                int event_idx = selected_events[i];

                if (event_info[event_idx].id == 0xff) {
                    break;
                }
            }

            /* No CPU_CYCLES is found */
            if (i == 3) {
                fprintf(stderr, "You can only specify three events if one of "
                                "them is CPU_CYCLES\n");
                exit(1);
            }
            /* Swap CPU_CYCLES to counter #2 (starting from #0)*/
            else if (i != 2) {
                int temp;

                temp = selected_events[2];
                selected_events[2] = selected_events[i];
                selected_events[i] = temp;

                temp = selected_counts[2];
                selected_counts[2] = selected_counts[i];
                selected_counts[i] = temp;
            }
        }
#endif


        /* Configure the counters and enable them */
        for (i = 0; i < num_events; i++) {
            int event_idx = selected_events[i];
            int setup_result = 0;

            if (i == 0) {
                snprintf(command+strlen(command), 1024 - strlen(command), 
                         " --events=");
            }
            else {
                snprintf(command+strlen(command), 1024 - strlen(command), 
                         ",");
            }
            /* Compose name:id:count:unit_mask:kernel:user, something like
             * --events=CYCLES_DATA_STALL:2:0:200000:0:1:1,....
             */
            snprintf(command+strlen(command), 1024 - strlen(command), 
                     "%s:%d:%d:%d:0:1:1",
                     event_info[event_idx].name,
                     event_info[event_idx].id,
                     i,
                     selected_counts[i]);

            setup_result |= echo_dev("1", 0, "user", i);
            setup_result |= echo_dev("1", 0, "kernel", i);
            setup_result |= echo_dev("0", 0, "unit_mask", i);
            setup_result |= echo_dev("1", 0, "enabled", i);
            setup_result |= echo_dev(NULL, selected_counts[i], "count", i);
            setup_result |= echo_dev(NULL, event_info[event_idx].id, 
                                     "event", i);
            if (setup_result) {
                fprintf(stderr, "Counter configuration failed for %s\n",
                        event_info[event_idx].name);
                fprintf(stderr, "Did you do \"opcontrol --setup\" first?\n");
                exit(1);
            }
        }

        /* Disable the unused counters */
        for (i = num_events; i < MAX_EVENTS; i++) {
            echo_dev("0", 0, "enabled", i);
        }

        snprintf(command+strlen(command), 1024 - strlen(command), " %s",
                 vmlinux);
        if (kernel_range[0]) {
            snprintf(command+strlen(command), 1024 - strlen(command), " %s",
                     kernel_range);
        }
        verbose("command: %s\n", command);
        system(command);
    }

    if (start) {
        echo_dev("1", 0, "enable", -1);
    }

    if (stop) {
        echo_dev("1", 0, "dump", -1);
        echo_dev("0", 0, "enable", -1);
    }
}
Beispiel #6
0
 /* button handling using XFORM_DO
 */
int
handle_button( int button, WORD *msg )
{
    OBJECT *tree  = (OBJECT *)rs_trindex[GENERAL];
    int    done = FALSE;
    BOOLEAN  click_flag = FALSE;
    MRETS  mk;
    int    ox, oy;
       
    if( ( button != -1 ) && ( button & 0x8000 ) )
    {
      click_flag = TRUE;
      button &= 0x7FFF;
    }

    switch( button )
    {
       case GSAVE:   if( (*xcpb->XGen_Alert)( SAVE_DEFAULTS ) )
       		     {
       			 (*xcpb->CPX_Save)( &cur_value, sizeof( GEN ) );
       			 push_data();
       			 Set_Data();
       		     }
       		     XDeselect( tree, GSAVE );
       		     break;
       		     
       case GOK:     done = TRUE;
       		     Set_Data();
       		     Deselect( GOK );
       		     break;
       			
       case GCANCEL: done = TRUE;
       		     pop_data();
       		     Deselect( GCANCEL );
       		     break;
       
       case STATUS:  graf_mkstate( &mk.x, &mk.y, &mk.buttons, &mk.kstate );
       		     if( objc_find( tree, ROOT, MAX_DEPTH, mk.x, mk.y ) == STATUS )
       		         do_status();
       		     break;
       		     
       case BELL:    keybell_handle( BELLICON, &cur_value.bell );
       		     break;
       		     
       case KEYCLICK:keybell_handle( KEYICON, &cur_value.keyclick );
       		     break;
       		     
       case M0:
       case M1:
       case M2:
       case M3:
       case M4:	     Dclick_Select( button );
       		     break;
       		     
       case DOUBLE:  Dclick_Handle( click_flag, button );
        	     break;

       	case KREPEAT:   (*xcpb->MFsave )( MFSAVE, &Other );
       			if( AES_Version >= 0x0320 )
       			    graf_mouse( FLAT_HAND, 0L );


		        if(( AES_Version >= 0x0330 )
	      		    && ( numcol > LWHITE ) )
	     		    XSelect( tree, KREPEAT );

		        graf_mkstate( &ox, &mk.y, &mk.buttons, &mk.kstate );
	  		do
	  		{
	     		   graf_mkstate( &mk.x, &mk.y, &mk.buttons, &mk.kstate );
	  		}while( mk.buttons && ( mk.x == ox ));

			if( mk.buttons && ( mk.x != ox ))
			{
       			   (*xcpb->Sl_dragx)( tree, REPTBASE, KREPEAT, KEYMIN,
       					      KEYMAX, &cur_value.repeat,
       	                                      KRepeat );
       	                   Set_Kbrate();
			}
			
          		if(( AES_Version >= 0x0330 )
	       		     && ( numcol > LWHITE ) )
	     		     XDeselect( tree, KREPEAT );
       	                
       	                (*xcpb->MFsave)( MFRESTORE, &Other );
       			break;
 	       					
       	case KRESPONS:  (*xcpb->MFsave)( MFSAVE, &Other );
       			if( AES_Version >= 0x0320 )
       			    graf_mouse( FLAT_HAND, 0L );
       			    
		        if(( AES_Version >= 0x0330 )
	      		    && ( numcol > LWHITE ) )
	     		    XSelect( tree, KRESPONS );

		        graf_mkstate( &ox, &mk.y, &mk.buttons, &mk.kstate );
	  		do
	  		{
	     		   graf_mkstate( &mk.x, &mk.y, &mk.buttons, &mk.kstate );
	  		}while( mk.buttons && ( mk.x == ox ));

			if( mk.buttons && ( mk.x != ox ))
			{
       			   (*xcpb->Sl_dragx)( tree, RESPBASE, KRESPONS, KEYMIN,
       					      KEYMAX, &cur_value.response,
       	                                      KResponse );
       	                   Set_Kbrate();                  
			}
			
          		if(( AES_Version >= 0x0330 )
	       		     && ( numcol > LWHITE ) )
	     		     XDeselect( tree, KRESPONS );

       	                (*xcpb->MFsave)( MFRESTORE, &Other );
       			break;
       			
       	case RRESP:     (*xcpb->Sl_arrow)( tree, RESPBASE, KRESPONS, RRESP,
       	                                  1, KEYMIN, KEYMAX,
       	                                  &cur_value.response, HORIZONTAL,
       	                                  KResponse );
       	                Set_Kbrate();
       	 		break;
 	       	
       	case LRESP:     (*xcpb->Sl_arrow)( tree, RESPBASE, KRESPONS, LRESP,
       	                                  -1, KEYMIN, KEYMAX,
       	                                  &cur_value.response, HORIZONTAL,
       	                                  KResponse );
       	                Set_Kbrate();
       			break;
 	       	
       	case RREPEAT:   (*xcpb->Sl_arrow)( tree, REPTBASE, KREPEAT, RREPEAT,
       	                                  1, KEYMIN, KEYMAX,
       	                                  &cur_value.repeat, HORIZONTAL,
       	                                  KRepeat );
       	                Set_Kbrate();
       			break;
 	       	
       	case LREPEAT:   (*xcpb->Sl_arrow)( tree, REPTBASE, KREPEAT, LREPEAT,
       	                                  -1, KEYMIN, KEYMAX,
       	                                  &cur_value.repeat, HORIZONTAL,
       	                                  KRepeat );
       	                Set_Kbrate();
       			break;

	case REPTBASE:  Graf_mkstate( &mk );
			objc_offset( tree, KREPEAT, &ox, &oy );
			oy = (( mk.x < ox ) ? ( -3 ) : ( 3 ) );
       			(*xcpb->Sl_arrow)( tree, REPTBASE, KREPEAT, -1,
       			                  oy, KEYMIN, KEYMAX,
       			                  &cur_value.repeat, HORIZONTAL,
       			                  KRepeat );
       			Set_Kbrate();
  			break;
			
	case RESPBASE:  Graf_mkstate( &mk );
			objc_offset( tree, KRESPONS, &ox, &oy );
			oy = (( mk.x < ox ) ? ( -3 ) : ( 3 ));
       			(*xcpb->Sl_arrow)( tree, RESPBASE, KRESPONS, -1,
       			                  oy, KEYMIN, KEYMAX,
       			                  &cur_value.response, HORIZONTAL,
       			                  KResponse );
       			Set_Kbrate();
			break;
			

	case BLK1BUTT:  Do_Speed();
			break;
			
	case BLK2BUTT:  Do_Block2();
        		
	default:	if( button == -1 )
  			{
     			   switch( msg[0] )
     			   {
     			     case WM_REDRAW: Redraw_Objects();
     			     		     break;
     			     		     
     			     case AC_CLOSE:  done = TRUE;
     			     		     pop_data();	
     			     		     break;
     			     		     
     			     case WM_CLOSED: done = TRUE;
     			     		     Set_Data();
					     break;
     			     default:
     			     		break;
     			   }
     			}
     			break;
    }
    return( done );
}
Beispiel #7
0
int ifdhandler_process(ct_socket_t * sock, ifd_reader_t * reader,
		       ct_buf_t * argbuf, ct_buf_t * resbuf)
{
	unsigned char cmd, unit;
	ct_tlv_parser_t args;
	ct_tlv_builder_t resp;
	int rc;

	/* Get command and target unit */
	if (ct_buf_get(argbuf, &cmd, 1) < 0 || ct_buf_get(argbuf, &unit, 1) < 0)
		return IFD_ERROR_INVALID_MSG;

	ifd_debug(1, "ifdhandler_process(cmd=%s, unit=%u)",
		  get_cmd_name(cmd), unit);

	/* First, handle commands that don't do TLV encoded
	 * arguments - currently this is only CT_CMD_TRANSACT. */
	if (cmd == CT_CMD_TRANSACT_OLD) {
		/* Security - deny any APDUs if there's an
		 * exclusive lock held by some other client. */
		if ((rc =
		     ifdhandler_check_lock(sock, unit, IFD_LOCK_EXCLUSIVE)) < 0)
			return rc;
		return do_transact_old(reader, unit, argbuf, resbuf);
	}

	if ((rc = do_before_command(reader)) < 0) {
		return rc;
	}

	memset(&args, 0, sizeof(args));
	if (ct_tlv_parse(&args, argbuf) < 0)
		return IFD_ERROR_INVALID_MSG;
	if (args.use_large_tags)
		sock->use_large_tags = 1;

	ct_tlv_builder_init(&resp, resbuf, sock->use_large_tags);

	switch (cmd) {
	case CT_CMD_STATUS:
		rc = do_status(reader, unit, &args, &resp);
		break;

	case CT_CMD_OUTPUT:
		rc = do_output(reader, unit, &args, &resp);
		break;

	case CT_CMD_RESET:
	case CT_CMD_REQUEST_ICC:
		rc = do_reset(reader, unit, &args, &resp);
		break;

	case CT_CMD_EJECT_ICC:
		rc = do_eject(reader, unit, &args, &resp);
		break;

	case CT_CMD_PERFORM_VERIFY:
		rc = do_verify(reader, unit, &args, &resp);
		break;

	case CT_CMD_LOCK:
		rc = do_lock(sock, reader, unit, &args, &resp);
		break;

	case CT_CMD_UNLOCK:
		rc = do_unlock(sock, reader, unit, &args, &resp);
		break;

	case CT_CMD_MEMORY_READ:
		rc = do_memory_read(reader, unit, &args, &resp);
		break;

	case CT_CMD_MEMORY_WRITE:
		rc = do_memory_write(reader, unit, &args, &resp);
		break;

	case CT_CMD_TRANSACT:
		rc = do_transact(reader, unit, &args, &resp);
		break;
	case CT_CMD_SET_PROTOCOL:
		rc = do_set_protocol(reader, unit, &args, &resp);
		break;
	default:
		return IFD_ERROR_INVALID_CMD;
	}

	if (rc >= 0)
		rc = resp.error;

	/*
	 * TODO consider checking error
	 */
	do_after_command(reader);

	return rc;
}
Beispiel #8
0
int main(int argc, char *argv[])
{
    struct option long_opts[] = {
        { "async",         no_argument,       NULL, 'a' },
        { "break",         no_argument,       NULL, 'b' },
        { "disk",          required_argument, NULL, 'd' },
        { "get-status",    no_argument,       NULL, 'G' },
        { "help",          no_argument,       NULL, 'h' },
        { "list-statuses", no_argument,       NULL, 'L' },
        { "offset",        required_argument, NULL, 'o' },
/*      { "random",        no_argument,       NULL, 'r' }, */
        { "size",          required_argument, NULL, 's' },
        { "set-status",    required_argument, NULL, 'S' },
        { "write",         no_argument,       NULL, 'w' },
        { NULL,            0,                 NULL, '\0' }
    };
    int long_idx;
    int opt;
    enum {
        OP_IO,
        OP_LIST_STATUSES,
        OP_GET_STATUS,
        OP_SET_STATUS,
        OP_DEACTIVATE_DEVICE
    } op = OP_IO;
    char *disk_path = NULL;
/*     bool random = false; */
    bool async = false;
    bool write = false;
    uint64_t size_in_bytes = 0;
    uint64_t offset = 0;
    char status_str[256] = "";
    int err;

#ifdef WIN32
    os_windows_disable_crash_popup();
#endif

    program = argv[0];

    if (argc == 1)
    {
	fprintf(stderr, "%s: no option given\nType %s --help for usage help\n",
                program, program);
	return -1;
    }

    while (true)
    {
	opt = os_getopt_long(argc, argv, "ad:bGhLo:r:s:wS:", long_opts, &long_idx);
        /* Missing argument, quit right away (os_getopt_long() takes care of
         * printing an error message) */
        if (opt == ':' || opt == '?')
            return 1;

        if (opt == -1)
            break;

	switch (opt)
	{
	case 'a':
	    async = true;
	    break;

	case 'b':
	    op = OP_DEACTIVATE_DEVICE;
	    break;

	case 'd':
	    disk_path = optarg;
	    break;

        case 'G':
            op = OP_GET_STATUS;
            break;

	case 'h':
	    usage();
	    break;

        case 'L':
            op = OP_LIST_STATUSES;
            break;

	case 'o':
            if (to_uint64(optarg, &offset) != EXA_SUCCESS)
            {
                fprintf(stderr, "invalid offset\n");
                exit(1);
            }
	    break;

/* 	case 'r': */
/* 	    random = true; */
/* 	    break; */

	case 's':
            if (to_uint64(optarg, &size_in_bytes) != EXA_SUCCESS)
            {
                fprintf(stderr, "invalid size\n");
                exit(1);
            }
	    break;

	case 'w':
	    write = true;
	    break;

        case 'S':
            op = OP_SET_STATUS;
            strlcpy(status_str, optarg, sizeof(status_str));
            break;
	}
    }

    if (optind < argc)
    {
        fprintf(stderr, "%s: too many arguments\nType %s --help for usage help\n",
                program, program);
        return 1;
    }

    err = exa_rdev_static_init(RDEV_STATIC_GET);
    if (err != 0)
    {
        fprintf(stderr, "failed initializating RDEV statics: error %d\n", err);
        fprintf(stderr, "\tExanodes is probably not running on this node.\n");
        return 1;
    }

    if (disk_path == NULL && op != OP_LIST_STATUSES)
    {
        fprintf(stderr, "no disk path specified\n");
        err = -1;
        goto done;
    }

    switch (op)
    {
    case OP_IO:
        err = do_io(disk_path, async, write, offset, size_in_bytes);
        break;

    case OP_LIST_STATUSES:
        err = list_statuses();
        break;

    case OP_GET_STATUS:
        err = do_status(disk_path, 'g', NULL);
        break;

    case OP_SET_STATUS:
        err = do_status(disk_path, 's', status_str);
        break;

    case OP_DEACTIVATE_DEVICE:
	err = deactivate_device(disk_path);
	break;
    }

done:
    exa_rdev_static_clean(RDEV_STATIC_RELEASE);
    return err ? 1 : 0;
}
int main(int argc, char * const argv[])
{
	struct sdp_dev *p_id;
	int err = 0;
	int config = 0;
	int verify = 0;
	int usertscts = 1;
	int uart_fd;
	struct sdp_work *curr;
	char const *conf;
	char const *ttyfile;
	char const *conffilepath;
	char const *conffile;
	char const *basepath;
#ifndef WIN32
	struct termios orig;
#else
	DCB orig;
#endif

	curr=NULL;

	err = parse_opts(argc, argv, &ttyfile, &conffilepath, &verify, &usertscts, &curr);

	if (err < 0)
		return err;

	// Get machine specific configuration file..
	if ((conffile = strrchr(conffilepath, PATH_SEPARATOR)) == NULL) {
		// Only a file was given as configuration
		basepath = get_base_path(argv[0]);
		conffile = conffilepath;
	} else {
		// A whole path is given as configuration
		basepath = get_base_path(conffilepath);
		conffile++; // Filename starts after slash
	}

	conf = conf_file_name(conffile, basepath, SYSCONFDIR "/imx-loader.d/");
	if (conf == NULL)
		return -1;

	p_id = parse_conf(conf);
	if (!p_id)
		return -1;

	// Open UART and start associating phase...
	err = uart_connect(&uart_fd, ttyfile, usertscts, &orig);

	if (err < 0)
		goto out;

	p_id->transfer = &transfer_uart;

	// UART private pointer is TTY file descriptor...
	p_id->priv = &uart_fd;

	err = do_status(p_id);
	if (err) {
		printf("status failed\n");
		goto out;
	}

	// By default, use work from config file...
	if (curr == NULL)
		curr = p_id->work;

	while (curr) {
		if (curr->mem)
			perform_mem_work(p_id, curr->mem);
//		printf("jump_mode %x\n", curr->jump_mode);
		if (curr->filename[0]) {
			err = DoIRomDownload(p_id, curr, verify);
		}
		if (err) {
			err = do_status(p_id);
			break;
		}
		if (!curr->next && !curr->plug)
			break;
		err = do_status(p_id);
		printf("jump_mode %x plug=%i err=%i\n", curr->jump_mode, curr->plug, err);

		if (err)
			goto out;

		if (curr->plug) {
			curr->plug = 0;
			continue;
		}
		curr = curr->next;
	}

out:
	uart_close(&uart_fd, &orig);
	return err;
}
Beispiel #10
0
int main(int argc, char *argv[], char *envp[])
{
	int rv;
	const char *cp;
#ifdef LOGGING_LIBQB
	enum qb_log_target_slot i;
#endif

	init_set_proc_title(argc, argv, envp);
	get_time(&start_time);

	memset(&cl, 0, sizeof(cl));
	strncpy(cl.configfile,
			BOOTH_DEFAULT_CONF, BOOTH_PATH_LEN - 1);
	cl.lockfile[0] = 0;
	debug_level = 0;


	cp = ((cp = strstr(argv[0], ATTR_PROG)) && !strcmp(cp, ATTR_PROG)
		? ATTR_PROG
		: "booth");
#ifndef LOGGING_LIBQB
	cl_log_set_entity(cp);
#else
	qb_log_init(cp, LOG_USER, LOG_DEBUG);  /* prio driven by debug_level */
	for (i = QB_LOG_TARGET_START; i < QB_LOG_TARGET_MAX; i++) {
		if (i == QB_LOG_SYSLOG || i == QB_LOG_BLACKBOX)
			continue;
		qb_log_format_set(i, "%t %H %N: [%P]: %p: %b");
	}
	(void) qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
	                         QB_LOG_FILTER_FILE, "*", LOG_DEBUG);
#endif
	cl_log_enable_stderr(TRUE);
	cl_log_set_facility(0);

	rv = read_arguments(argc, argv);
	if (rv < 0)
		goto out;


	switch (cl.type) {
	case STATUS:
		rv = do_status(cl.type);
		break;

	case ARBITRATOR:
	case DAEMON:
	case SITE:
		rv = do_server(cl.type);
		break;

	case CLIENT:
		rv = do_client();
		break;

	case GEOSTORE:
		rv = do_attr();
		break;
	}

out:
#ifdef LOGGING_LIBQB
	qb_log_fini();
#endif
	/* Normalize values. 0x100 would be seen as "OK" by waitpid(). */
	return (rv >= 0 && rv < 0x70) ? rv : 1;
}
/**
 * Send mail messages via SMTP
 * @param mail A Mail object
 * @return FALSE if failed, TRUE if succeeded
 */
int sendmail(Mail_T mail) {

  int i;
  int rv;
  Mail_T m;
  SendMail_T S;
  char *b64 = NULL;
  char now[STRLEN];
  
  ASSERT(mail);
  
  S.socket = NULL;
  if(sigsetjmp(S.error, TRUE)) {
    rv = FALSE;
    goto exit;
  } else {
    rv = TRUE;
  }
  
  open_server(&S);
  
  Time_gmtstring(Time_now(), now);
  
  snprintf(S.localhost, sizeof(S.localhost), "%s", Run.mail_hostname ? Run.mail_hostname : Run.localhostname);
  
  do_status(&S);

  /* Use EHLO if TLS or Authentication is requested */
  if((S.ssl.use_ssl && S.ssl.version == SSL_VERSION_TLS) || S.username) {
    do_send(&S, "EHLO %s\r\n", S.localhost);
  } else {
    do_send(&S, "HELO %s\r\n", S.localhost);
  }
  do_status(&S);

  /* Switch to TLS now if configured */
  if(S.ssl.use_ssl && S.ssl.version == SSL_VERSION_TLS) {
    do_send(&S, "STARTTLS\r\n"); 
    do_status(&S);
    if(!socket_switch2ssl(S.socket, S.ssl)) {
      rv = FALSE;
      goto exit;
    }
    /* After starttls, send ehlo again: RFC 3207: 4.2 Result of the STARTTLS Command */
    do_send(&S, "EHLO %s\r\n", S.localhost);
    do_status(&S);
  }

  /* Authenticate if possible */
  if(S.username) {
    unsigned char buffer[STRLEN];
    int len;

    len = snprintf((char *)buffer, STRLEN, "%c%s%c%s", '\0', S.username, '\0', S.password?S.password:"");
    b64 = encode_base64(len, buffer);
    do_send(&S, "AUTH PLAIN %s\r\n", b64); 
    do_status(&S);
  }
  
  for(i = 0, m= mail; m; m= m->next, i++) { 
    do_send(&S, "MAIL FROM: <%s>\r\n", m->from);
    do_status(&S);
    do_send(&S, "RCPT TO: <%s>\r\n", m->to);
    do_status(&S);
    do_send(&S, "DATA\r\n");
    do_status(&S);
    do_send(&S, "From: %s\r\n", m->from);
    if (m->replyto)
      do_send(&S, "Reply-To: %s\r\n", m->replyto);
    do_send(&S, "To: %s\r\n", m->to);
    do_send(&S, "Subject: %s\r\n", m->subject);
    do_send(&S, "Date: %s\r\n", now);
    do_send(&S, "X-Mailer: %s %s\r\n", prog, VERSION);
    do_send(&S, "Mime-Version: 1.0\r\n");
    do_send(&S, "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n");
    do_send(&S, "Content-Transfer-Encoding: 8bit\r\n");
    do_send(&S, "Message-id: <%ld.%lu@%s>\r\n", time(NULL), random(), S.localhost);
    do_send(&S, "\r\n");
    do_send(&S, "%s\r\n", m->message);
    do_send(&S, ".\r\n");
    do_status(&S);
  }
  do_send(&S, "QUIT\r\n");
  do_status(&S);
  
exit:
  if(S.socket)
    socket_free(&S.socket);
  
  FREE(b64);

  return rv;
}
int
main(int argc, char **argv)
{
	static struct option long_options[] = {
		{"help", no_argument, NULL, '?'},
		{"version", no_argument, NULL, 'V'},
		{"log", required_argument, NULL, 'l'},
		{"mode", required_argument, NULL, 'm'},
		{"pgdata", required_argument, NULL, 'D'},
		{"silent", no_argument, NULL, 's'},
		{"timeout", required_argument, NULL, 't'},
		{"core-files", no_argument, NULL, 'c'},
		{NULL, 0, NULL, 0}
	};

	int			option_index;
	int			c;
	pgpid_t		killproc = 0;

#if defined(WIN32) || defined(__CYGWIN__)
	setvbuf(stderr, NULL, _IONBF, 0);
#endif

	progname = get_progname(argv[0]);
	set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_ctl"));

	/*
	 * save argv[0] so do_start() can look for the postmaster if necessary. we
	 * don't look for postmaster here because in many cases we won't need it.
	 */
	argv0 = argv[0];

	umask(077);

	/* support --help and --version even if invoked as root */
	if (argc > 1)
	{
		if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0 ||
			strcmp(argv[1], "-?") == 0)
		{
			do_help();
			exit(0);
		}
		else if (strcmp(argv[1], "-V") == 0 || strcmp(argv[1], "--version") == 0)
		{
			puts("pg_ctl (PostgreSQL) " PG_VERSION);
			exit(0);
		}
	}

	/*
	 * Disallow running as root, to forestall any possible security holes.
	 */
#ifndef WIN32
	if (geteuid() == 0)
	{
		write_stderr(_("%s: cannot be run as root\n"
					   "Please log in (using, e.g., \"su\") as the "
					   "(unprivileged) user that will\n"
					   "own the server process.\n"),
					 progname);
		exit(1);
	}
#endif

	/*
	 * 'Action' can be before or after args so loop over both. Some
	 * getopt_long() implementations will reorder argv[] to place all flags
	 * first (GNU?), but we don't rely on it. Our /port version doesn't do
	 * that.
	 */
	optind = 1;

	/* process command-line options */
	while (optind < argc)
	{
		while ((c = getopt_long(argc, argv, "cD:l:m:N:o:p:P:st:U:wW", long_options, &option_index)) != -1)
		{
			switch (c)
			{
				case 'D':
					{
						char	   *pgdata_D;
						char	   *env_var = pg_malloc(strlen(optarg) + 8);

						pgdata_D = xstrdup(optarg);
						canonicalize_path(pgdata_D);
						snprintf(env_var, strlen(optarg) + 8, "PGDATA=%s",
								 pgdata_D);
						putenv(env_var);

						/*
						 * We could pass PGDATA just in an environment
						 * variable but we do -D too for clearer postmaster
						 * 'ps' display
						 */
						pgdata_opt = pg_malloc(strlen(pgdata_D) + 7);
						snprintf(pgdata_opt, strlen(pgdata_D) + 7,
								 "-D \"%s\" ",
								 pgdata_D);
						break;
					}
				case 'l':
					log_file = xstrdup(optarg);
					break;
				case 'm':
					set_mode(optarg);
					break;
				case 'N':
					register_servicename = xstrdup(optarg);
					break;
				case 'o':
					post_opts = xstrdup(optarg);
					break;
				case 'p':
					exec_path = xstrdup(optarg);
					break;
				case 'P':
					register_password = xstrdup(optarg);
					break;
				case 's':
					silent_mode = true;
					break;
				case 't':
					wait_seconds = atoi(optarg);
					break;
				case 'U':
					if (strchr(optarg, '\\'))
						register_username = xstrdup(optarg);
					else
						/* Prepend .\ for local accounts */
					{
						register_username = malloc(strlen(optarg) + 3);
						if (!register_username)
						{
							write_stderr(_("%s: out of memory\n"), progname);
							exit(1);
						}
						strcpy(register_username, ".\\");
						strcat(register_username, optarg);
					}
					break;
				case 'w':
					do_wait = true;
					wait_set = true;
					break;
				case 'W':
					do_wait = false;
					wait_set = true;
					break;
				case 'c':
					allow_core_files = true;
					break;
				default:
					/* getopt_long already issued a suitable error message */
					do_advice();
					exit(1);
			}
		}

		/* Process an action */
		if (optind < argc)
		{
			if (ctl_command != NO_COMMAND)
			{
				write_stderr(_("%s: too many command-line arguments (first is \"%s\")\n"), progname, argv[optind]);
				do_advice();
				exit(1);
			}

			if (strcmp(argv[optind], "init") == 0
				|| strcmp(argv[optind], "initdb") == 0)
				ctl_command = INIT_COMMAND;
			else if (strcmp(argv[optind], "start") == 0)
				ctl_command = START_COMMAND;
			else if (strcmp(argv[optind], "stop") == 0)
				ctl_command = STOP_COMMAND;
			else if (strcmp(argv[optind], "restart") == 0)
				ctl_command = RESTART_COMMAND;
			else if (strcmp(argv[optind], "reload") == 0)
				ctl_command = RELOAD_COMMAND;
			else if (strcmp(argv[optind], "status") == 0)
				ctl_command = STATUS_COMMAND;
			else if (strcmp(argv[optind], "kill") == 0)
			{
				if (argc - optind < 3)
				{
					write_stderr(_("%s: missing arguments for kill mode\n"), progname);
					do_advice();
					exit(1);
				}
				ctl_command = KILL_COMMAND;
				set_sig(argv[++optind]);
				killproc = atol(argv[++optind]);
			}
#if defined(WIN32) || defined(__CYGWIN__)
			else if (strcmp(argv[optind], "register") == 0)
				ctl_command = REGISTER_COMMAND;
			else if (strcmp(argv[optind], "unregister") == 0)
				ctl_command = UNREGISTER_COMMAND;
			else if (strcmp(argv[optind], "runservice") == 0)
				ctl_command = RUN_AS_SERVICE_COMMAND;
#endif
			else
			{
				write_stderr(_("%s: unrecognized operation mode \"%s\"\n"), progname, argv[optind]);
				do_advice();
				exit(1);
			}
			optind++;
		}
	}

	if (ctl_command == NO_COMMAND)
	{
		write_stderr(_("%s: no operation specified\n"), progname);
		do_advice();
		exit(1);
	}

	/* Note we put any -D switch into the env var above */
	pg_data = getenv("PGDATA");
	if (pg_data)
	{
		pg_data = xstrdup(pg_data);
		canonicalize_path(pg_data);
	}

	if (pg_data == NULL &&
		ctl_command != KILL_COMMAND && ctl_command != UNREGISTER_COMMAND)
	{
		write_stderr(_("%s: no database directory specified "
					   "and environment variable PGDATA unset\n"),
					 progname);
		do_advice();
		exit(1);
	}

	if (!wait_set)
	{
		switch (ctl_command)
		{
			case RESTART_COMMAND:
			case START_COMMAND:
				do_wait = false;
				break;
			case STOP_COMMAND:
				do_wait = true;
				break;
			default:
				break;
		}
	}

	if (ctl_command == RELOAD_COMMAND)
	{
		sig = SIGHUP;
		do_wait = false;
	}

	if (pg_data)
	{
		snprintf(postopts_file, MAXPGPATH, "%s/postmaster.opts", pg_data);
		snprintf(pid_file, MAXPGPATH, "%s/postmaster.pid", pg_data);
		snprintf(conf_file, MAXPGPATH, "%s/postgresql.conf", pg_data);
		snprintf(backup_file, MAXPGPATH, "%s/backup_label", pg_data);
		snprintf(recovery_file, MAXPGPATH, "%s/recovery.conf", pg_data);
	}

	switch (ctl_command)
	{
		case INIT_COMMAND:
			do_init();
			break;
		case STATUS_COMMAND:
			do_status();
			break;
		case START_COMMAND:
			do_start();
			break;
		case STOP_COMMAND:
			do_stop();
			break;
		case RESTART_COMMAND:
			do_restart();
			break;
		case RELOAD_COMMAND:
			do_reload();
			break;
		case KILL_COMMAND:
			do_kill(killproc);
			break;
#if defined(WIN32) || defined(__CYGWIN__)
		case REGISTER_COMMAND:
			pgwin32_doRegister();
			break;
		case UNREGISTER_COMMAND:
			pgwin32_doUnregister();
			break;
		case RUN_AS_SERVICE_COMMAND:
			pgwin32_doRunAsService();
			break;
#endif
		default:
			break;
	}

	exit(0);
}
Beispiel #13
0
int do_work(struct sdp_dev *p_id, struct sdp_work **work, int verify)
{
	struct sdp_work *curr = *work;
	int config = 0;
	int err = 0;
	libusb_device_handle *h = p_id->priv;

	libusb_get_configuration(h, &config);
	dbg_printf("bConfigurationValue = 0x%x\n", config);

	if (libusb_kernel_driver_active(h, 0))
		 libusb_detach_kernel_driver(h, 0);

	err = libusb_claim_interface(h, 0);
	if (err) {
		fprintf(stderr, "claim interface failed\n");
		return err;
	}
	printf("Interface 0 claimed\n");
	err = do_status(p_id);
	if (err) {
		fprintf(stderr, "status failed\n");
		goto err_release_interface;
	}

	while (curr) {
		/* Do current job */
		if (curr->mem)
			perform_mem_work(p_id, curr->mem);
		if (curr->filename[0])
			err = DoIRomDownload(p_id, curr, verify);
		if (err) {
			fprintf(stderr, "DoIRomDownload failed, err=%d\n", err);
			do_status(p_id);
			break;
		}

		/* Check if more work is to do... */
		if (!curr->next) {
			/*
			 * If only one job, but with a plug-in is specified
			 * reexecute the same job, but this time download the
			 * image. This allows to specify a single file with
			 * plugin and image, and imx_usb will download & run
			 * the plugin first and then the image.
			 * NOTE: If the file does not contain a plugin,
			 * DoIRomDownload->process_header will set curr->plug
			 * to 0, so we won't download the same image twice...
			 */
			if (curr->plug) {
				curr->plug = 0;
			} else {
				curr = NULL;
				break;
			}
		} else {
			curr = curr->next;
		}

		/*
		 * Check if device is still here, otherwise return
		 * with work (retry)
		 */
		err = do_status(p_id);
		if (err < 0) {
			err = 0;
			break;
		}
	}

	*work = curr;

err_release_interface:
	libusb_release_interface(h, 0);
	return err;
}
Beispiel #14
0
int
main(int argc, char **argv)
{
	int			c;
	char		*nodename = NULL; /* GTM Proxy nodename */

	progname = "gtm_ctl";

	/*
	 * save argv[0] so do_start() can look for the gtm if necessary. we
	 * don't look for gtm here because in many cases we won't need it.
	 */
	argv0 = argv[0];

	umask(077);

	/* support --help and --version even if invoked as root */
	if (argc > 1)
	{
		if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0 ||
			strcmp(argv[1], "-?") == 0)
		{
			do_help();
			exit(0);
		}
		if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
		{
			puts("gtm_ctl (Postgres-XC) " PGXC_VERSION);
			exit(0);
		}
	}

	/*
	 * Disallow running as root, to forestall any possible security holes.
	 */
	if (geteuid() == 0)
	{
		write_stderr(_("%s: cannot be run as root\n"
					   "Please log in (using, e.g., \"su\") as the "
					   "(unprivileged) user that will\n"
					   "own the server process.\n"),
					 progname);
		exit(1);
	}

	/*
	 * 'Action' can be before or after args so loop over both. Some
	 * getopt_long() implementations will reorder argv[] to place all flags
	 * first (GNU?), but we don't rely on it. Our /port version doesn't do
	 * that.
	 */
	optind = 1;

	/* process command-line options */
	while (optind < argc)
	{
		while ((c = getopt(argc, argv, "D:i:l:m:o:p:t:wWZ:C:")) != -1)
		{
			switch (c)
			{
				case 'D':
					{
						char	   *env_var = pg_malloc(strlen(optarg) + 9);

						gtmdata_D = xstrdup(optarg);
						canonicalize_path(gtmdata_D);
						snprintf(env_var, strlen(optarg) + 9, "GTMDATA=%s",
								 gtmdata_D);
						putenv(env_var);

						/*
						 * We could pass GTMDATA just in an environment
						 * variable but we do -D too for clearer gtm
						 * 'ps' display
						 */
						gtmdata_opt = (char *) pg_malloc(strlen(gtmdata_D) + 8);
						snprintf(gtmdata_opt, strlen(gtmdata_D) + 8,
								 "-D \"%s\" ",
								 gtmdata_D);
						break;
					}
				case 'i':
					nodename = strdup(optarg);
					break;
				case 'l':
					log_file = xstrdup(optarg);
					break;
				case 'm':
					set_mode(optarg);
					break;
				case 'o':
					gtm_opts = xstrdup(optarg);
					break;
				case 'p':
					gtm_path = xstrdup(optarg);
					canonicalize_path(gtm_path);
					break;
				case 't':
					wait_seconds = atoi(optarg);
					break;
				case 'C':
					control_file = xstrdup(optarg);
					break;
				case 'w':
					do_wait = true;
					wait_set = true;
					break;
				case 'W':
					do_wait = false;
					wait_set = true;
					break;
				case 'Z':
					gtm_app = xstrdup(optarg);
					if (strcmp(gtm_app,"gtm_proxy") != 0
						&& strcmp(gtm_app,"gtm_standby") != 0
						&& strcmp(gtm_app,"gtm") != 0)
					{
						write_stderr(_("%s: %s launch name set not correct\n"), progname, gtm_app);
						do_advice();
						exit(1);
					}
					break;
				default:
					/* getopt_long already issued a suitable error message */
					do_advice();
					exit(1);
			}
		}

		/* Process an action */
		if (optind < argc)
		{
			if (ctl_command != NO_COMMAND)
			{
				write_stderr(_("%s: too many command-line arguments (first is \"%s\")\n"), progname, argv[optind]);
				do_advice();
				exit(1);
			}

			if (strcmp(argv[optind], "start") == 0)
				ctl_command = START_COMMAND;
			else if (strcmp(argv[optind], "stop") == 0)
				ctl_command = STOP_COMMAND;
			else if (strcmp(argv[optind], "promote") == 0)
				ctl_command = PROMOTE_COMMAND;
			else if (strcmp(argv[optind], "restart") == 0)
				ctl_command = RESTART_COMMAND;
			else if (strcmp(argv[optind], "status") == 0)
				ctl_command = STATUS_COMMAND;
			else if (strcmp(argv[optind], "reconnect") == 0)
				ctl_command = RECONNECT_COMMAND;
			else
			{
				write_stderr(_("%s: unrecognized operation mode \"%s\"\n"),
							 progname, argv[optind]);
				do_advice();
				exit(1);
			}
			optind++;
		}
	}

	/*
	 * Take care of the control file (-C Option)
	 */
	if (control_file)
	{
		char ctrl_path[MAXPGPATH+1];
		char C_opt_path[MAXPGPATH+1];
		char bkup_path[MAXPGPATH+1];
		FILE *f1, *f2;
		int c;

		if (!gtmdata_D)
		{
			write_stderr(_("No -D option specified.\n"));
			exit(1);
		}
		if ((strcmp(gtm_app, "gtm") != 0) && (strcmp(gtm_app, "gtm_master") != 0))
		{
			write_stderr(_("-C option is valid only for gtm.\n"));
			exit(1);
		}
		/* If there's already a control file, backup it to *.bak */
		trim_last_slash(gtmdata_D);
		snprintf(ctrl_path, MAXPGPATH, "%s/%s", gtmdata_D, GTM_CONTROL_FILE);
		if ((f1 = fopen(ctrl_path, "r")))
		{

			snprintf(bkup_path, MAXPGPATH, "%s/%s.bak", gtmdata_D, GTM_CONTROL_FILE);
			if (!(f2 = fopen(bkup_path, "w")))
			{
				fclose(f1);
				write_stderr(_("Cannot open backup file, %s/%s.bak, %s\n"),
							 gtmdata_D, GTM_CONTROL_FILE, strerror(errno));
				exit(1);
			}
			while ((c = getc(f1)) != EOF)
				putc(c, f2);
			fclose(f1);
			fclose(f2);
		}
		/* Copy specified control file. */
		snprintf(C_opt_path, MAXPGPATH, "%s/%s", gtmdata_D, control_file);
		if (!(f1 = fopen(ctrl_path, "w")))
		{
			write_stderr(_("Cannot oopen control file, %s, %s\n"), ctrl_path, strerror(errno));
			exit(1);
		}
		if (!(f2 = fopen(C_opt_path, "r")))
		{
			fclose(f1);
			write_stderr(_("Cannot open -C option file, %s, %s\n"), C_opt_path, strerror(errno));
			exit(1);
		}
		while ((c = getc(f2)) != EOF)
			putc(c, f1);
		fclose(f1);
		fclose(f2);
	}

	if (ctl_command == NO_COMMAND)
	{
		write_stderr(_("%s: no operation specified\n"), progname);
		do_advice();
		exit(1);
	}

	gtm_data = getenv("GTMDATA");

	if (gtm_data)
	{
		gtm_data = xstrdup(gtm_data);
		canonicalize_path(gtm_data);
	}

	if (!gtm_data)
	{
		write_stderr("%s: no GTM/GTM Proxy directory specified \n",
					 progname);
		do_advice();
		exit(1);
	}

	/*
	 * pid files of gtm and gtm proxy are named differently
	 * -Z option has also to be set for STOP_COMMAND
	 * or gtm_ctl will not be able to find the correct pid_file
	 */
	if (!gtm_app)
	{
		write_stderr("%s: no launch option not specified\n",
					 progname);
		do_advice();
		exit(1);
	}

	if (strcmp(gtm_app,"gtm_proxy") != 0 &&
		strcmp(gtm_app, "gtm_standby") != 0 &&
		strcmp(gtm_app,"gtm") != 0)
	{
		write_stderr(_("%s: launch option incorrect\n"),
						progname);
		do_advice();
		exit(1);
	}

	/* Check if GTM Proxy ID is set, this is not necessary when stopping */
	if (ctl_command == START_COMMAND ||
		ctl_command == RESTART_COMMAND)
	{
		/* Rebuild option string to include Proxy ID */
		if (strcmp(gtm_app, "gtm_proxy") == 0)
		{
			gtmdata_opt = (char *) pg_realloc(gtmdata_opt, strlen(gtmdata_opt) + 9);
			if (nodename)
				sprintf(gtmdata_opt, "%s -i %s ", gtmdata_opt, nodename);
			else
				sprintf(gtmdata_opt, "%s ", gtmdata_opt);
		}
	}

	if (!wait_set)
	{
		switch (ctl_command)
		{
			case RESTART_COMMAND:
			case START_COMMAND:
			case PROMOTE_COMMAND:
			case STATUS_COMMAND:
				do_wait = false;
				break;
			case STOP_COMMAND:
				do_wait = true;
				break;
			default:
				break;
		}
	}

	/* Build strings for pid file and option file */
	if (strcmp(gtm_app,"gtm_proxy") == 0)
	{
		snprintf(pid_file, MAXPGPATH, "%s/gtm_proxy.pid", gtm_data);
		snprintf(gtmopts_file, MAXPGPATH, "%s/gtm_proxy.opts", gtm_data);
		snprintf(conf_file, MAXPGPATH, "%s/gtm_proxy.conf", gtm_data);
	}
	else if (strcmp(gtm_app,"gtm") == 0)
	{
		snprintf(pid_file, MAXPGPATH, "%s/gtm.pid", gtm_data);
		snprintf(gtmopts_file, MAXPGPATH, "%s/gtm.opts", gtm_data);
		snprintf(conf_file, MAXPGPATH, "%s/gtm.conf", gtm_data);
	}
	else if (strcmp(gtm_app,"gtm_standby") == 0)
	{
		snprintf(pid_file, MAXPGPATH, "%s/gtm.pid", gtm_data);
		snprintf(gtmopts_file, MAXPGPATH, "%s/gtm.opts", gtm_data);
		snprintf(conf_file, MAXPGPATH, "%s/gtm.conf", gtm_data);
	}

	if (ctl_command==STATUS_COMMAND)
		gtm_opts = xstrdup("-c");

	switch (ctl_command)
	{
		case START_COMMAND:
			do_start();
			break;
		case STOP_COMMAND:
			do_stop();
			break;
		case PROMOTE_COMMAND:
			do_promote();
			break;
		case RESTART_COMMAND:
			do_restart();
			break;
		case STATUS_COMMAND:
			do_status();
			break;
		case RECONNECT_COMMAND:
			do_reconnect();
			break;
		default:
			break;
	}

	exit(0);
}