Example #1
0
int main(int argc, char **argv)
{

	int status;
	int client_fd = -1;

	/*
	 * on err, this exits...
	 */
	parseArgs( argc, argv);


	/*
	 * Initialization...
	 */
	gBuffers[0] = malloc(gBufferSize);
	gBuffers[1] = malloc(gBufferSize);
	if ( ( NULL == gBuffers[0] ) || ( NULL == gBuffers[1] ) ) {
		CapError(-1, CAP_SEV_FATAL, gProgramName, 
				 "out of memory, -b %d failed",gBufferSize);
		exit(1);
	}
	*(gBuffers[0]) = '\0';
	*(gBuffers[1]) = '\0';


	if (gInetdMode) {
		/*
		 * The socket to the client is open on file descriptor 0.
		 * Handle one client and then exit.
		 */
		client_fd = 0;
	}

	if (gDaemonMode) {
		/*
		 * Convert this process into a standard unix type daemon process.
		 * It will be running in the background with no controlling terminal
		 * and using syslog to report error messages.
		 */
		status = CapDaemonize();
		if (status < 0)	{
			CapError(-1, CAP_SEV_FATAL, gProgramName, NULL);
			exit(1);
		}
	}

	/*
	 * open the configuration and data files
	 */
    gChannels = create_channels(gConfigFile, client_fd);
	if ( NULL == gChannels ) {
		CapError(-1, CAP_SEV_FATAL, gProgramName, NULL);
		exit(1);
	}

	if ( 0 == strcmp(gDataPath,"-") ) {
		gDataFile = stdin;
	} else {
		/*
		 * RFE: delay opening until first read
		 */
		gDataFile = fopen(gDataPath, "r");
		if ( !gReadReopen && (NULL == gDataFile) ) {
			CapError(-1, CAP_SEV_FATAL, gProgramName, NULL);
			exit(1);
		}
	}

	if ( NULL != gDataFile ) 
		nonBlockFifo(-1);
			
	if (gInetdMode)
	{
		/*
		 * The socket to the client is already open on file descriptor 0.
		 * Handle one client and then exit.
		 */
		status = handle_client(client_fd);
		exit(status);
	}
	else
	{
		while (1)
		{
			/*
			 * Set up the server socket and wait for a connection.
			 */
			client_fd = CapServe(gServerName);
			if (client_fd < 0)
			{
				CapError(-1, CAP_SEV_FATAL, gProgramName, NULL);
				exit(1);
			}
      
			/* Handle client requests */
			status = handle_client(client_fd);

			if (status < 0)
			{
				CapError(-1, CAP_SEV_FATAL, gProgramName, NULL);
			}

			/* Shutdown the client */
			closesocket(client_fd);
			client_fd = -1;

			/* Go back and wait for another connection request */
			continue;
		}
	}
}
Example #2
0
int main(int argc, char **argv)
{
  int opt;
#ifndef _WIN32
  char *cptr;
#endif
  int status;
  int client_fd = -1;


  /*
   * Grab a copy of the program name
   */
#ifdef _WIN32
  _splitpath (argv[0], NULL, NULL, program, NULL);
#else
  cptr = strrchr(argv[0], '/');
  if (cptr)
  {
    strcpy(program, (cptr + 1));
  }
  else
  {
    strcpy(program, argv[0]);
  }
#endif //WIN32

  /*
   * Parse the options
   */
#ifdef DEVEL
  while ((opt = getopt(argc, argv, "hdvDin:")) != -1)
#else /* DEVEL */
  while ((opt = getopt(argc, argv, "hdn:")) != -1)
#endif /* DEVEL */
  {
    switch (opt)
    {
      case 'h':
	show_usage++;
	break;

      case 'd':
	daemon_mode++;
	break;

      case 'D':
	debug_mode++;
	break;

      case 'i':
	inetd_mode++;
	break;

      case 'n':
	server_name = optarg;
	break;

      case 'v':
	verbose++;
	break;

      case GETOPTHUH:
	show_usage++;
    }
  }

  /*
   * Check for errors
   */
  if (daemon_mode && inetd_mode) show_usage++;
  if (optind < argc) show_usage++;

  if (show_usage)
  {
    fprintf(stderr, "Usage:\n");
#ifdef DEVEL
    fprintf(stderr, "    %s [-hdvDi] [-n name]\n", program);
#else /* DEVEL */
    fprintf(stderr, "    %s [-hd] [-n name]\n", program);
#endif /* DEVEL */
    fprintf(stderr, "\n");
    fprintf(stderr, "        -h        Print this help message\n");
    fprintf(stderr, "        -d        Run as a daemon in the background\n");
#ifdef DEVEL
    fprintf(stderr, "        -i        Run from inetd\n");
    fprintf(stderr, "        -D        Set the debug flag\n");
    fprintf(stderr, "        -v        Set the vebose flag\n");
#endif /* DEVEL */
    fprintf(stderr, "        -n name   Set the UNIX socket name to name\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "Defaults:\n");
    fprintf(stderr, "    %s -n %s\n", program, program);
    fprintf(stderr, "\n");

    exit(1);
  }

  if (inetd_mode)
  {
    /*
     * The socket to the client is open on file descriptor 0.
     * Handle one client and then exit.
     */
    client_fd = 0;
  }

  if (daemon_mode)
  {
    /*
     * Convert this process into a standard unix type daemon process.
     * It will be running in the background with no controlling terminal
     * and using syslog to report error messages.
     */
    status = CapDaemonize();
    if (status < 0)
    {
      CapError(-1, CAP_SEV_FATAL, program, NULL);
      exit(1);
    }
  }

  if (inetd_mode)
  {
    /*
     * The socket to the client is already open on file descriptor 0.
     * Handle one client and then exit.
     */
    status = handle_client(client_fd);
    exit(status);
  }
  else
  {
    while (1)
    {
      /*
       * Set up the server socket and wait for a connection.
       */
      client_fd = CapServe(server_name);
      if (client_fd < 0)
      {
		CapError(-1, CAP_SEV_FATAL, program, NULL);
		exit(1);
      }
      
      /* Handle client requests */
      status = handle_client(client_fd);

      if (status < 0)
      {
		CapError(-1, CAP_SEV_FATAL, program, NULL);
      }

      /* Shutdown the client */
      closesocket(client_fd);
      client_fd = -1;

      /* Go back and wait for another connection request */
      continue;
    }
  }
  return 0;
}