예제 #1
0
파일: control.c 프로젝트: scoil44/LagerPi
int main (int argc, char *argv[]) {
	// Bind the clean-up function to program exit
	atexit(dbmsg_disconnect);


///////////////////////////////////////////////////////////////////////////////
	if (argc < 2) {
		// Initialize the controller
		init_local();
		// Determine heater posture based on current readings
		comp_temps();
		
///////////////////////////////////////////////////////////////////////////////
	} else {
		// Initialize the web controller
		init_web();
		// One time variables
		devStat state;
		int pin = 5;
		// Parse the input parameters
		while ((argc > 1) && (argv[1][0] == '-')) {
			switch (argv[1][1]) {
			case 'h':		// Heater
				pin = (int)argv[1][2] - '1';
				break;
			case 'f':		// Fan
				pin = 4;
				break;
			case 'r':		// Run
				state = ON;
				break;
			case 's':		// Stop
				state = OFF;
				break;
			case 'a':		// All on
				allON();
				return 0;
			case 'x':		// All stop
				allOFF();
				return 0;
			case 't':
				desired_product_temp = get_desired_temp();
				return 0;
			default:
				printf("Invalid parameters\n");
				display_help();
				return 0;
			}
			++argv;
			--argc;
		}
		if (state == OFF) {
			turnOFF(heats[pin]);
		} else {
			turnON(heats[pin]);
		}
	}

	return 0;
}
예제 #2
0
static int do_client(CLI *c) {
    int result;

    if(init_local(c))
        return -1;
    if(!options.option.client && !c->opt->protocol) {
        /* Server mode and no protocol negotiation needed */
        if(init_ssl(c))
            return -1;
        if(init_remote(c))
            return -1;
    } else {
        if(init_remote(c))
            return -1;
        if(negotiate(c)<0) {
            s_log(LOG_ERR, "Protocol negotiations failed");
            return -1;
        }
        if(init_ssl(c))
            return -1;
    }
    result=transfer(c);
    s_log(LOG_NOTICE,
        "Connection %s: %d bytes sent to SSL, %d bytes sent to socket",
         result ? "reset" : "closed", c->ssl_bytes, c->sock_bytes);
    return result;
}
예제 #3
0
/*
 * init_all
 *
 * Initialize modules.  Exit on failure.
 */
static void init_all(void)
{
	int r;

	if ((r = init_local()) ||
	    (r = init_cluster())) {
		exit(r);
	}
}
예제 #4
0
파일: client.c 프로젝트: tricky1997/wifisec
static void client_try(CLI * c)
{
	init_local(c);
	if (!c->opt->option.client) {
		init_ssl(c);
		init_remote(c);
	} else {
		init_remote(c);
		init_ssl(c);
	}
	transfer(c);
}
예제 #5
0
파일: client.c 프로젝트: l7s/stunnel
static void do_client(CLI *c) {
    init_local(c);
    if(!c->opt->option.client && !c->opt->protocol) {
        /* server mode and no protocol negotiation needed */
        init_ssl(c);
        init_remote(c);
    } else {
        init_remote(c);
        negotiate(c);
        init_ssl(c);
    }
    transfer(c);
}
예제 #6
0
파일: client.c 프로젝트: nmacs/lm3s-uclinux
static void client_try(CLI *c) {
    init_local(c);
    if(!c->opt->option.client && c->opt->protocol<0) {
        /* server mode and no protocol negotiation needed */
        init_ssl(c);
        init_remote(c);
    } else {
        protocol(c, PROTOCOL_PRE_CONNECT);
        init_remote(c);
        protocol(c, PROTOCOL_PRE_SSL);
        init_ssl(c);
        protocol(c, PROTOCOL_POST_SSL);
    }
    transfer(c);
}
예제 #7
0
NOEXPORT void client_try(CLI *c) {
    init_local(c);
    if(!c->opt->option.client && c->opt->protocol<0
#ifndef OPENSSL_NO_TLSEXT
            && !c->opt->servername_list_head
#endif
            ) {
        /* server mode and no protocol negotiation needed */
        init_ssl(c);
        init_remote(c);
    } else { /* client mode or protocol negotiation enabled */
        protocol(c, PROTOCOL_PRE_CONNECT);
        init_remote(c);
        protocol(c, PROTOCOL_PRE_SSL);
        init_ssl(c);
        protocol(c, PROTOCOL_POST_SSL);
    }
    transfer(c);
}
bool
SCI_Transporter::connect_server_impl(NDB_SOCKET_TYPE sockfd)
{
  SocketOutputStream s_output(sockfd);
  SocketInputStream s_input(sockfd);
  char buf[256];
  DBUG_ENTER("SCI_Transporter::connect_server_impl");

  if (!init_local()) {
    NDB_CLOSE_SOCKET(sockfd);
    DBUG_RETURN(false);
  }
  // Send ok to client
  s_output.println("sci server 1 ok");

  // Wait for ok from client
  if (s_input.gets(buf, 256) == 0) {
    DBUG_PRINT("error", ("No response from client in SCI"));
    NDB_CLOSE_SOCKET(sockfd);
    DBUG_RETURN(false);
  }

  if (!init_remote()) {
    NDB_CLOSE_SOCKET(sockfd);
    DBUG_RETURN(false);
  }
  // Send ok to client
  s_output.println("sci server 2 ok");
  // Wait for ok from client
  if (s_input.gets(buf, 256) == 0) {
    DBUG_PRINT("error", ("No second response from client in SCI"));
    NDB_CLOSE_SOCKET(sockfd);
    DBUG_RETURN(false);
  }

  NDB_CLOSE_SOCKET(sockfd);
  DBUG_PRINT("info", ("Successfully connected server to node %d",
              remoteNodeId));
  DBUG_RETURN(true);
}
예제 #9
0
파일: eh_server.c 프로젝트: morfoh/libeh
int eh_server_local(struct eh_server *self, const char *path, bool cloexec)
{
	struct sockaddr_un sun;
	int fd, e;

	assert(self);
	assert(path);

	e = init_local(&sun, path);
	if (e != 1)
		return e; /* 0 or -1 */
	unlink(path);

	if ((fd = eh_socket(sun.sun_family, SOCK_STREAM, cloexec, true)) < 0)
		return -1; /* socket() call failed */
	else if (bind(fd, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
		close(fd);
		return -1;
	}

	eh_io_init(&self->connection_watcher, connect_callback, self, fd, EH_READ);
	return 1;
}
예제 #10
0
void
hastd_secondary(struct hast_resource *res, struct nv *nvin)
{
	sigset_t mask;
	pthread_t td;
	pid_t pid;
	int error, mode, debuglevel;

	/*
	 * Create communication channel between parent and child.
	 */
	if (proto_client(NULL, "socketpair://", &res->hr_ctrl) < 0) {
		KEEP_ERRNO((void)pidfile_remove(pfh));
		pjdlog_exit(EX_OSERR,
		    "Unable to create control sockets between parent and child");
	}
	/*
	 * Create communication channel between child and parent.
	 */
	if (proto_client(NULL, "socketpair://", &res->hr_event) < 0) {
		KEEP_ERRNO((void)pidfile_remove(pfh));
		pjdlog_exit(EX_OSERR,
		    "Unable to create event sockets between child and parent");
	}

	pid = fork();
	if (pid < 0) {
		KEEP_ERRNO((void)pidfile_remove(pfh));
		pjdlog_exit(EX_OSERR, "Unable to fork");
	}

	if (pid > 0) {
		/* This is parent. */
		proto_close(res->hr_remotein);
		res->hr_remotein = NULL;
		proto_close(res->hr_remoteout);
		res->hr_remoteout = NULL;
		/* Declare that we are receiver. */
		proto_recv(res->hr_event, NULL, 0);
		/* Declare that we are sender. */
		proto_send(res->hr_ctrl, NULL, 0);
		res->hr_workerpid = pid;
		return;
	}

	gres = res;
	mode = pjdlog_mode_get();
	debuglevel = pjdlog_debug_get();

	/* Declare that we are sender. */
	proto_send(res->hr_event, NULL, 0);
	/* Declare that we are receiver. */
	proto_recv(res->hr_ctrl, NULL, 0);
	descriptors_cleanup(res);

	descriptors_assert(res, mode);

	pjdlog_init(mode);
	pjdlog_debug_set(debuglevel);
	pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
	setproctitle("%s (%s)", res->hr_name, role2str(res->hr_role));

	PJDLOG_VERIFY(sigemptyset(&mask) == 0);
	PJDLOG_VERIFY(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);

	/* Error in setting timeout is not critical, but why should it fail? */
	if (proto_timeout(res->hr_remotein, 2 * HAST_KEEPALIVE) < 0)
		pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
	if (proto_timeout(res->hr_remoteout, res->hr_timeout) < 0)
		pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");

	init_local(res);
	init_environment();

	if (drop_privs(res) != 0)
		exit(EX_CONFIG);
	pjdlog_info("Privileges successfully dropped.");

	/*
	 * Create the control thread before sending any event to the parent,
	 * as we can deadlock when parent sends control request to worker,
	 * but worker has no control thread started yet, so parent waits.
	 * In the meantime worker sends an event to the parent, but parent
	 * is unable to handle the event, because it waits for control
	 * request response.
	 */
	error = pthread_create(&td, NULL, ctrl_thread, res);
	PJDLOG_ASSERT(error == 0);

	init_remote(res, nvin);
	event_send(res, EVENT_CONNECT);

	error = pthread_create(&td, NULL, recv_thread, res);
	PJDLOG_ASSERT(error == 0);
	error = pthread_create(&td, NULL, disk_thread, res);
	PJDLOG_ASSERT(error == 0);
	(void)send_thread(res);
}
예제 #11
0
int main(int argc, char **argv)
{
    std::string hosts_file, fe_host, be_hosts_file, cp_hosts_file,
                output_file, topology, topology_type;
    bool have_hosts = false, have_fe_host = false;
    bool have_be_hosts = false, have_cp_hosts = false;
    bool have_max = false, have_be_max = false, have_cp_max = false;
    bool have_topology = false;

    int c;
    int max_procs=1024; // bigger than any reasonable use
    int be_max_procs=1024; // bigger than any reasonable use
    int cp_max_procs=1024; // bigger than any reasonable use

    FILE *infile, *outfile;

    if( argc == 1 ) usage_exit(argv[0]);

    extern char * optarg;
    const char optstring[] = "b:c:f:h:o:p:q:r:t:";
    while (true) {

#if defined(os_linux)
        int option_index = 0;
        static struct option long_options[] = {
            {"behosts", 1, 0, 'b'},
            {"cphosts", 1, 0, 'c'},
            {"fehost", 1, 0, 'f'},
            {"hosts", 1, 0, 'h'},
            {"outfile", 1, 0, 'o'},
            {"maxprocs", 1, 0, 'p'},
            {"beprocs", 1, 0, 'q'},
            {"cpprocs", 1, 0, 'r'},
            {"topology", 1, 0, 't'},
            {0, 0, 0, 0}
        };
        c = getopt_long( argc, argv, optstring, long_options, &option_index );
#else
        c = getopt( argc, argv, optstring );
#endif
        if (c == -1)
            break;

        switch (c) {
        case 'b':
            have_be_hosts = true;
            be_hosts_file = optarg;
            break;
        case 'c':
            have_cp_hosts = true;
            cp_hosts_file = optarg;
            break;
        case 'f':
            have_fe_host = true;
            fe_host = optarg;
            break;
        case 'h':
            have_hosts = true;
            hosts_file = optarg;
            break;
        case 'o':
            output_file = optarg;
            break;
        case 'p':
            have_max = true;
            max_procs = atoi(optarg);
            if( max_procs <= 0 )
                max_procs = 1;
            break;
        case 'q':
            have_be_max = true;
            be_max_procs = atoi(optarg);
            if( be_max_procs <= 0 )
                be_max_procs = 1;
            break;
        case 'r':
            have_cp_max = true;
            cp_max_procs = atoi(optarg);
            if( cp_max_procs <= 0 )
                cp_max_procs = 1;
            break;
        case 't':
            have_topology = true;
            if( 0 == strncmp(optarg,"b:",2) ) {
                topology_type = "balanced";
                topology = optarg+2;
            }
            else if( 0 == strncmp(optarg,"g:",2) ) {
                topology_type = "generic";
                topology = optarg+2;
            }
            else if( 0 == strncmp(optarg,"k:",2) ) {
                topology_type = "knomial";
                topology = optarg+2;
            }
            else {
                fprintf(stderr, "Error: invalid topology specification '%s'\n", optarg);
                usage_exit(argv[0]);
            }
            break;
        default:
            usage_exit(argv[0]);
        }
    }

    // sanity checks
    if( have_hosts ) {
        if( have_be_hosts || have_cp_hosts ) {
            fprintf(stderr, "Error: cannot specify --hosts and --behosts,--cphosts\n");
            usage_exit(argv[0]);
        }
        if( have_be_max || have_cp_max ) {
            fprintf(stderr, "Error: cannot specify --hosts and --beprocs,--cpprocs, perhaps you want --maxprocs??\n");
            usage_exit(argv[0]);
        }
    }
    if( have_max && (have_be_hosts || have_cp_hosts) ) {
        fprintf(stderr, "Error: cannot specify --maxprocs and --behosts,--cphosts\n");
        usage_exit(argv[0]);
    }

    if( ! have_topology ) {
        fprintf(stderr, "Error: --topology option must be provided\n");
        usage_exit(argv[0]);
    }
   
    init_local();

    // read input hosts file(s)
    std::list< std::pair<std::string, unsigned> > hosts, cp_hosts, be_hosts;
    if( ! (have_hosts || have_be_hosts) ){
        infile = stdin;
        MRN::Tree::get_HostsFromFileStream(infile, hosts);
        have_hosts = true;
    }
    else {
        if( have_hosts ) {
            infile = fopen(hosts_file.c_str(), "r");
            if( !infile ){
                fprintf(stderr, "Error: open of %s failed with '%s'\n", 
                        hosts_file.c_str(), strerror(errno) );
                exit(-1);
            }
            MRN::Tree::get_HostsFromFileStream(infile, hosts);
        }
        else {
            if( have_cp_hosts ) {
                infile = fopen(cp_hosts_file.c_str(), "r");
                if( !infile ){
                    fprintf(stderr, "Error: open of %s failed with '%s'\n", 
                            cp_hosts_file.c_str(), strerror(errno) );
                    exit(-1);
                }
                MRN::Tree::get_HostsFromFileStream(infile, cp_hosts);
            }
            if( have_be_hosts ) {
                infile = fopen(be_hosts_file.c_str(), "r");
                if( !infile ){
                    fprintf(stderr, "Error: open of %s failed with '%s'\n", 
                            be_hosts_file.c_str(), strerror(errno) );
                    exit(-1);
                }
                MRN::Tree::get_HostsFromFileStream(infile, be_hosts);
            }
        }
    }

    // set fe to local host if not given on command line
    if( ! have_fe_host )
        XPlat::NetUtils::GetLocalHostName( fe_host );

    // generate tree
    MRN::Tree * tree=NULL;

    if( topology_type == "balanced" ) {
        //fprintf(stderr, "Creating balanced topology \"%s\" from \"%s\" to \"%s\"\n",
                //topology.c_str(),
                //( hosts_file == "" ? "stdin" : hosts_file.c_str() ),
                //( output_file == "" ? "stdout" : output_file.c_str() ) );
        if( have_hosts )
            tree = new MRN::BalancedTree( topology, fe_host, hosts, max_procs );
        else
            tree = new MRN::BalancedTree( topology, fe_host, be_hosts, cp_hosts,
                                          be_max_procs, cp_max_procs );
    }
    else if( topology_type == "knomial" ) {
        //fprintf(stderr, "Creating k-nomial topology \"%s\" from \"%s\" to \"%s\"\n",
                //topology.c_str(),
                //( hosts_file == "" ? "stdin" : hosts_file.c_str() ),
                //( output_file == "" ? "stdout" : output_file.c_str() ) );
        if( have_hosts )
            tree = new MRN::KnomialTree( topology, fe_host, hosts, max_procs );
        else
            tree = new MRN::KnomialTree( topology, fe_host, be_hosts, cp_hosts,
                                         be_max_procs, cp_max_procs );
    }
    else if( topology_type == "generic" ) {
        //fprintf(stderr, "Creating generic topology \"%s\" from \"%s\" to \"%s\"\n",
                //topology.c_str(),
                //( hosts_file == "" ? "stdin" : hosts_file.c_str() ),
                //( output_file == "" ? "stdout" : output_file.c_str() ) );
        if( have_hosts )
            tree = new MRN::GenericTree( topology, fe_host, hosts, max_procs );
        else
            tree = new MRN::GenericTree( topology, fe_host, be_hosts, cp_hosts,
                                         be_max_procs, cp_max_procs );
    }
    else {
        assert(!"internal error: unknown topology");
        exit(-1);
    }

    if( tree->is_Valid() ) {

        // set topology output file
        if( output_file.empty() ){
            outfile = stdout;
        }
        else{
            outfile = fopen(output_file.c_str(), "w");
            if( !outfile ){
                fprintf(stderr, "Error: open of %s for writing failed with '%s'\n", 
                        output_file.c_str(), strerror(errno) );
                exit(-1);
            }
        }

        tree->create_TopologyFile( outfile );
    }
    else {
        fprintf(stderr, "Generated tree is not valid. Please check your topology specification and options.\n");
        return -1;
    }
    return 0;
}