示例#1
0
文件: server.c 项目: 2hdddg/tsrv
bool server_start(int port)
{
    bool success =
        _create_socket(port) &&
        _bind_to_socket(port) &&
        _listen_on_socket();

    if (!success){
        _close_socket();
        return false;
    }

    log_trace("Accepting connections on port %d", port);
    while(success){
        int acceptedfd;
        success = _accept_on_socket(&acceptedfd);
        if (success){
            session_start(acceptedfd);
        }
        else{
            break;
        }
    }
    log_print(LOG_INFO, "Stopped accepting connections on port %d", port);
    _close_socket();
    return true;
}
示例#2
0
VSL::VSOCKET accept(VSL::VSOCKET socket) {
    LOCK(mu,0);
    shared_ptr<Node> con = _find_socket(socket);
    if(con) {
        unique_ptr<Node> client = con->accept();
        if(client) {
            VSOCKET s = _create_socket();
            sockets[s] = move(client);
            return s;
        }
        return 0; // listening but no connection
    }
    return -1; // invalid descriptor
}
示例#3
0
static int
_domain_socket_create(const char *dir, const char *nodename,
		     uint32_t jobid, uint32_t stepid)
{
	int fd;
	char *name = NULL;
	struct stat stat_buf;

	/*
	 * Make sure that "dir" exists and is a directory.
	 */
	if (stat(dir, &stat_buf) < 0) {
		error("Domain socket directory %s: %m", dir);
		return -1;
	} else if (!S_ISDIR(stat_buf.st_mode)) {
		error("%s is not a directory", dir);
		return -1;
	}

	/*
	 * Now build the name of socket, and create the socket.
	 */
	xstrfmtcat(name, "%s/%s_%u.%u", dir, nodename, jobid, stepid);

	/*
	 * First check to see if the named socket already exists.
	 */
	if (stat(name, &stat_buf) == 0) {
		/* Vestigial from a slurmd crash or job requeue that did not
		 * happen properly (very rare conditions). Unlink the file
		 * and recreate it.
		 */
		if (unlink(name) != 0) {
			error("%s: failed unlink(%s) job %u step %u %m",
			      __func__, name, jobid, stepid);
			xfree(name);
			errno = ESLURMD_STEP_EXISTS;
			return -1;
		}
	}

	fd = _create_socket(name);
	if (fd < 0)
		fatal("Could not create domain socket: %m");

	chmod(name, 0777);
	socket_name = name;
	return fd;
}
示例#4
0
文件: djbmx.c 项目: stanguy/scripts
void sw_init( socket_work* sw, mx_list_t* ml)
{
    GList* iter;
    struct sockaddr_in sin;
    sin.sin_port = htons( 25 );
    sin.sin_family = AF_INET;
    bzero(&sin.sin_zero, sizeof(sin.sin_zero));

    for ( iter = g_list_first( ml->mxs ) ;
          NULL != iter ;
          iter = g_list_next( iter ) ) {
        int cret ;
        int sock = _create_socket( iter->data );
        // at this point, we have a non-blocking socket ready to use
        
        struct in_addr* in = (struct in_addr*)(iter->data);
        sin.sin_addr.s_addr = in->s_addr;

        cret = connect( sock, (struct sockaddr*)&sin, sizeof(sin) ) ;

        if ( ( -1 == cret ) && ( EINPROGRESS == errno ) ) {
            // connection is happening in the background
            // prepare this socket for select
            FD_SET( sock, &sw->readset );
            sw->max_socket = MAX( sock, sw->max_socket );
            sw->socket_list[ sw->waiting_sockets ] = sock;
            sw->waiting_sockets++;
        } else if ( cret == 0 ) {
            // connection already successful
            ml->mx_ok++;
            close( sock );
        } else {
            // connection already failed
        }
    }

}
示例#5
0
static int _sock_dup(StdFileDes *fp)
{ struct SocketSettings *lss;
  StdFileDes *fp2;
  int rc;

  if ((fp2=_create_socket(fp->lx_family, fp->lx_domain, fp->lx_protocol)) == NULL)
    return -1;

  switch (lss=_lx_get_socket_settings(),lss->lx_network_type) {

    case LX_AS225:
      /* only INET-225 has dup */
      if (lss->lx_SocketBase->lib_Version >= 8)
        rc = SOCK_dup(fp->lx_sock);
      else
        rc = fp->lx_sock;
    break;

    case LX_AMITCP:
      rc = TCP_Dup2Socket(fp->lx_sock, -1);
    break;

    default:
      rc = -1;
    break;
  }

  if (rc >= 0) {
    fp2->lx_sock = rc; rc = fp2->lx_pos;
  }
  else {
    fp2->lx_inuse = 0;
  }

  return rc;
}
示例#6
0
VSL::VSOCKET socket() {
    LOCK(mu,0);
    VSOCKET sock = _create_socket();
    sockets[sock] = shared_ptr<Node>();
    return sock;
}
示例#7
0
static int init_inet_daemon(int *argc,char ***argv,struct SocketSettings *lss)
{ StdFileDes *fp = NULL;
  int sock, err, i;

  lss->lx_isdaemon = 0;

  if (lss->lx_network_type == LX_AS225) {

    /* code loosely derived from timed.c from AS225r2 */
    /* this program was called from inetd if :
     * 1> the first arg is a valid protocol(call getprotobyname)
     * 2> inetd is started - FindPort("inetd") returns non-NULL
     * NOT 3> argv[0] is the program found in inetd.conf for the program (scan inetd.conf)
     */

    /* save a little time with this comparison */
    if (err=-1,*argc >= 4) {

      struct servent *serv1 = SOCK_getservbyname((*argv)[1],"tcp");
      struct servent *serv2 = SOCK_getservbyname((*argv)[1],"udp");

      if ((serv1 || serv2) && FindPort("inetd")) {

        long sock_arg;

        sock_arg = atol((*argv)[2]);
        lss->lx_sockid = atoi((*argv)[3]);
        sock = SOCK_inherit((void *)sock_arg);
        if (sock >= 0) {
          lss->lx_isdaemon = 1; /* I was started from inetd */
          do {
            if ((fp=_create_socket(AF_INET, SOCK_STREAM, 0)) == NULL)
              break;
            fp->lx_sock = sock;
            /* get rid of the args that AS225 put in */
            for (i=1; i < (*argc)-3; i++)
              (*argv)[i] = (*argv)[i+3];
            (*argc) -= 3;
            err = 0;
          } while (0);
        }
      }
    }

    return ((errno=err) ? -1 : fp->lx_pos);
  }

  if (lss->lx_network_type == LX_AMITCP) {
    struct Process *me = (struct Process *)FindTask(NULL);
    struct DaemonMessage *dm = (struct DaemonMessage *)me->pr_ExitData;

    DB( BUG("daemon message is %lx\n", dm); )
    if (dm == NULL) {
      /*
      * No DaemonMessage, return error code - probably not an inet daemon
      */
      return -1;
    }

    /*
     * Obtain the server socket
     */
    sock = TCP_ObtainSocket(dm->dm_Id, dm->dm_Family, dm->dm_Type, 0);
    DB( BUG("Obtained socket is %ld\n", sock); )

    if (sock < 0) {