Beispiel #1
0
void
server_start(uint32_t port, uint32_t numConn, int proc_id)
{
   int res = -1;


   gClientId = proc_id;
   gNumConn = numConn;
   
   /* Open up a server socket for incoming connections */

   gSocket = socket(AF_INET, SOCK_STREAM, 0);

   if (gSocket < 0) {
      inform_user("ERROR: Failed to open socket!\n");
      return;
   }

   /* Set reusable socket */
   int reuse = 1;
   setsockopt(gSocket, SOL_SOCKET, SO_REUSEADDR, (char*) &reuse, sizeof(reuse));

   
   struct sockaddr_in servAddr;
   memset(&servAddr, 0, sizeof(struct sockaddr_in));

   servAddr.sin_family = AF_INET;
   servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
   servAddr.sin_port = htons(port);
   
   res = bind(gSocket, (struct sockaddr*) &servAddr, sizeof(servAddr));
   
   if (res < 0) {
      inform_user("ERROR: Failed to bind socket!\n");
      return;
   }

   res = listen(gSocket, gNumConn);

   if (res < 0) {
      inform_user("ERROR: Failed to listen on socket!\n");
      return;
   }

   inform_user("* Server socket bound to port %u\n", port);

   /* Enter eternal loop */
   eternal_loop();
}
Beispiel #2
0
static void scan_and_generate(FILE *infile)
{
  extern FILE *yyin;
  
  fileOpened = 0;
  lineno = 1;
  column = 0;
  tokens_seen = 0;

  /* normal interaction on yyin and outfile from now on */
  inform_user("parsing %s ...\n", inPath);
  log_printf("parsing %s ...\n", inPath);
  yyin = infile;
  currentFile = inPath;
  linebuf[0] = '\0';
  yyparse();
  log_printf("finished yyparse()\n");
  currentFile = "";
  log_printf("expanding classes...\n");
  while (!class_queue_empty()) {
    syntaxelem_t *elt = dequeue_class();
    generate_skel(elt);
  }

  log_printf("closing %s\n", outPath);
  free(outPath);
  outPath = NULL;
  if (fileOpened) {
    fflush(outfile);
    fclose(outfile);
    outfile = NULL;
  }
  log_printf("done with %s\n", inPath);
}
Beispiel #3
0
static void scan_existing_skeleton()
{
  extern FILE *yyin;
  FILE *existing;

  log_printf("checking for existence of %s ...\n", outPath);
  if ((existing = fopen(outPath, "r")) == NULL)
    return;

  log_printf("%s exists, scanning skeleton...\n", outPath);
  inform_user("scanning %s ...\n", outPath);

  lineno = 1;
  column = 0;
  tokens_seen = 0;

  yyin = existing;
  currentFile = outPath;
  linebuf[0] = '\0';
  yyparse();
  log_printf("finished yyparse()\n");
  currentFile = "";

  fclose(existing);
  log_printf("done scanning skeleton...\n");
}
Beispiel #4
0
static void*
recv_thread(void* data)
{
   client_data_t* cliData = (client_data_t*) data;

   inform_user("* Recv from client %d\n", cliData->clientNum);

   int    res     = 0;
   char   buff[0xFFFF];
   size_t buffLen = 0xFFFF;
   
   /* Enter loop to handle data from client */
   while (1) {
      res = recv(cliData->sock, buff, buffLen, 0);

      if (res == 0) {
         /* Remote side has closed down... */
         pthread_mutex_lock(&gMutex);
         gNumClosed++;
         pthread_mutex_unlock(&gMutex);
         pthread_exit(cliData);
      }

      /* Update number of received bytes */
      pthread_mutex_lock(&gMutexRecv);
      gRecvBytes += res;
      gRecvBytesSinceStart += res;
      pthread_mutex_unlock(&gMutexRecv);
   }
   
   pthread_exit(cliData);
}
int main(void)
{
  int ret;
  char msg[] = "Hi there\n";


  ret = inform_user (msg);

  if ( ret < 0 )
    {
      fprintf(stderr, "Uh oh, output error is encountered\n");
    }
  /* we now know that ret is positive */
  else if ( (unsigned int)ret != strlen(msg) )
    {
      /*
       *
       * In this example we will not show you a nice solution to the
       * problem of failed printf. One solution would be to loop (a
       * limited amount) and print the remaining characters... but for
       * now, this will do fine.
       *
       */
      fprintf (stderr, "Message was only partly printed\n");
      fprintf (stderr, " * Characters printed:     %d\n", ret);
      fprintf (stderr, " * Message length:         %lu\n", strlen(msg));
      fprintf (stderr, " * Remaining characters:   %lu\n", strlen(msg)-(unsigned int)ret);
    }
  
  return 0;
}
Beispiel #6
0
/*
 ****************************************************************
 *	Envia a mensagem para todos os usuários			*
 ****************************************************************
 */
void
send_warning (FILE *utmp_fp, const char *nodename, int rest_time)
{
	UTMP		utmp;

	rewind (utmp_fp);

	while (fread (&utmp, sizeof (UTMP), 1, utmp_fp) == 1)
	{
		if (utmp.ut_name[0] == '\0')
			continue;

		inform_user (&utmp, nodename, rest_time);

	}	/* end for (EVER) */

}	/* end send_warning */
Beispiel #7
0
int main(int argc, char **argv) 
{ 
    extern int optind;
    extern char *optarg;
    int c, err_flag = 0;
    char *ext;

#ifdef SGDEBUG
    char logfilename_buffer[_MAX_PATH];
#ifdef WIN32
    DWORD tmpPathLen;
#endif /* WIN32 */
#endif /* SGDEBUG */

#ifdef SGDEBUG
#ifdef WIN32
    tmpPathLen = GetTempPath(_MAX_PATH, logfilename_buffer);
    if (logfilename_buffer[tmpPathLen - 1] != '\\')
	strcat(logfilename_buffer, "\\");
#else /* !WIN32 */
    strcpy(logfilename_buffer, "/tmp/");
#endif /* WIN32 */

    strcat(logfilename_buffer, sg_getlogin());
    strcat(logfilename_buffer, "-");
    strcat(logfilename_buffer, logfilename);

    if (!log_open(logfilename_buffer)) {
      /* open failed */
      fatal(1, "%s: cannot write to %s\n", progname, logfilename_buffer);
    }
#endif /* SGDEBUG */

    while ((c = getopt(argc, argv, OPTS)) != EOF) {
	switch (c) {
	case 'h':
	    opt_h = 1; break;
	case 'q':
	    opt_q = 1; break;
	case 'v':
	    opt_v = 1; break;
	case 'g':
	    opt_g = 1; break;
	case 'a':
	    opt_a = 1; break;
	case 'e':
 	    opt_e = optarg; 
	    if (opt_e[0] == '.')
	      opt_e++;
	    break;
	case 'r':
	    opt_r = 1; break;
	case 'i':
	    opt_i = 1; break;
	case 'c':
	    if (opt_d) err_flag = 1;
	    opt_c = 1; break;
	case 'd':
	    if (opt_c) err_flag = 1;
	    opt_d = 1; break;
	case 'b':
	    if (opt_f || opt_s || opt_n) err_flag = 1;
	    opt_b = 1; break;
	case 'f':
	    if (opt_b || opt_s || opt_n) err_flag = 1;
	    opt_f = 1; break;
	case 's':
	    if (opt_f || opt_b || opt_n) err_flag = 1;
	    opt_s = 1; break;
	case 'n':
	    if (opt_f || opt_s || opt_b) err_flag = 1;
	    opt_n = 1; break;
	default:
            err_flag = 1;
	}
    }
    
    if (opt_h || opt_v || err_flag) {
      inform_user("%s version %s (build %d).\n",
		  progname, progver, revision());
      if (opt_h || err_flag)
	fatal(err_flag, usage, progname);
      else
	fatal(0, "%s\n%s", copyright, version_info);
    }

    if (!(opt_b || opt_f || opt_s || opt_n))
	opt_b = 1;

    /* done setting options */
    if (argc == optind) {
	/* read from stdin and stdout */
        outfile = stdout;
	fprintf(outfile, "/* %s: reading from stdin */\n", progname);
	using_stdio = 1;
	inPath = "stdin";
	outPath = strdup("stdout");
	
        opt_q = 1;  /* force quiet mode */
	log_printf("initting...\n");
	init_tables();
	scan_and_generate(stdin);
	log_printf("freeing memory...\n");
	free_tables();

    } else {
        inform_user("%s version %s (build %d).\n",
		    progname, progver, revision());

	/* each bloody file from the command line */
	while (optind < argc) {
  	    FILE *infile;
	    log_printf("working on %s\n", argv[optind]);
	    /* open for read */
	    infile = fopen(argv[optind], "r");
	    if (infile == NULL) {
	      /* open failed */
	      fatal(1, "%s: cannot open %s\n", progname, argv[optind]);
	    }

	    inPath = basename(argv[optind]);
	    outPath = (char *)malloc(strlen(inPath) + strlen(opt_e) + 2);
	    strcpy(outPath, inPath);

	    /* tie off .h, .hh, .hpp, or .hxx extension */
	    if (((ext = strrchr(outPath, '.')) != NULL) &&
		(((strlen(ext) == 2) &&
		  ((ext[1] == 'H') || (ext[1] == 'h'))) ||
		 (((strlen(ext) == 3) &&
		   ((ext[1] == 'H') || (ext[1] == 'h')) &&
		   ((ext[2] == 'H') || (ext[2] == 'h')))) ||
		 ((strlen(ext) == 4) &&
		  ((ext[1] == 'H') || (ext[1] == 'h')) &&
		  ((((ext[2] == 'P') || (ext[2] == 'p')) &&
		    ((ext[3] == 'P') || (ext[3] == 'p'))) ||
		   (((ext[2] == 'X') || (ext[2] == 'x')) &&
		    ((ext[3] == 'X') || (ext[3] == 'x')))))))
		*ext = '\0';
	    
	    assert(opt_e[0] != '.');
	    strcat(outPath, ".");
	    strcat(outPath, opt_e);

	    log_printf("initting...\n");
	    init_tables();
	    scan_existing_skeleton();
	    scan_and_generate(infile);
	    log_printf("freeing memory...\n");
	    free_tables();
	    clear_skeleton_queue();
	    fclose(infile);
	    optind++;
	}
    }

#ifdef SGDEBUG
    log_flush();
    log_close();
#endif /* SGDEBUG */

    return 0;
}
Beispiel #8
0
static void print_function(syntaxelem_t *elt)
{
    syntaxelem_t *e;

    if (find_skeleton(elt)) {
      log_printf("find_skeleton() returned true for this elt:\n");
      print_se(elt);
      return;
    }

    new_functions++;
    if (!fileOpened) {
	fileOpened = 1;

	if (using_stdio) {
	  fileExisted = 0;
	} else {
	  /* test for existence */
	  outfile = fopen(outPath, "r");
	  if (outfile != NULL) {
	    fileExisted = 1;
	    fclose(outfile);
	  }

	  /* do the fopen */
	  log_printf("writing to %s\n", outPath);
	  outfile = fopen(outPath, "a");
	  if (outfile == NULL) {
	    /* open failed */
	    fatal(1, "%s: cannot open %s\n", progname, outPath);
	  }
	}
  
	if (!fileExisted) file_hdr();
    }

    inform_user("%*s%s\n", inform_indent, "", elt->name);
    function_hdr(elt);
    for (e = elt->parent; e != NULL; e = e->parent) {
      if (e->templ) {
	fprintf(outfile, "%s\n", e->templ);
	break;
      }
    }

    {  /* scope for local vars */
	char *arg_str;

	fprintf(outfile, "%s%s%s::%s(",
		elt->ret_type, (strcmp(elt->ret_type, "") ? "\n" : ""),
		elt->parent->name, elt->name);

	arg_str = args_to_string(
	    elt->args, 
	    opt_a ? strlen(elt->parent->name) + strlen(elt->name) + 3 : 0);
	
	fprintf(outfile, "%s)", arg_str);
	free(arg_str);

	if (elt->throw_decl)
	    fprintf(outfile, " %s", elt->throw_decl);

	if (elt->const_flag)
	    fprintf(outfile, " const");

	fprintf(outfile, "\n{\n");
    }

    debug_printf(elt);
    fprintf(outfile, "}\n\n\n");
}
Beispiel #9
0
static void
eternal_loop(void)
{
   /* Start polling the server socket and accept incoming connections. */

   struct pollfd fd[1];
   int res = 0;
   int numConnected = 0;

   fd[0].fd = gSocket;
   fd[0].events = POLLIN;

   /* Start througput display thread */
   res = pthread_create(&gTpThread, NULL, tp_thread, NULL);
   
   if (res < 0) {
      inform_user("ERROR: Failed to start througput thread!\n");
      return;
   }

   if (get_aggregator_port() != -1) {
	   aggregator_register(gClientId, "127.0.0.1");
   }
   
   while (1) {
      res = poll(fd, 1, -1);

      if (res < 0) {
         inform_user("ERROR: Failed to poll socket!\n");
         return;
      }

      if (res > 0 && fd[0].revents == POLLIN) {
         /* Accept incoming connections */
         struct sockaddr_in cliAddr;
         uint32_t cliLen = sizeof(struct sockaddr_in);
         
         res = accept(fd[0].fd, (struct sockaddr*) &cliAddr, &cliLen);
         
         if (res < 0) {
            inform_user("ERROR: Failed to accept connection!\n");
            return;
         }

         pthread_mutex_lock(&gMutexRecv);
         gNumConnSinceStart++;
         pthread_mutex_unlock(&gMutexRecv);
         
         if (gWantedTp == 0) {
            close(res);
            continue;
         }
         
         client_data_t* cliData =
            (client_data_t*) malloc(sizeof(client_data_t));

         memcpy(&cliData->sinAddr, &cliAddr, sizeof(struct sockaddr_in));

         cliData->sock      = res;
         cliData->clientNum = numConnected;
         

         if (gWantedTp != 0) {
            /* Start client thread */
            res = pthread_create(&cliData->thread, NULL,
                                 recv_thread, (void*) cliData);
            
            if (res < 0) {
               inform_user("ERROR: Failed to start client thread!\n");
               return;
            }
         }

         numConnected++;
      }

      if (gNumClosed == gNumConn) {
         inform_user("* All clients closed!\n");
         return;
      }
   }
}