Пример #1
0
void do_choice(int choice)
{
	printf("is in the process of the first %d a choice \n", choice);

	switch (choice)
	{
		case INSERT :
			do_insert();
			break;
		case QUERY:
			do_query();
			break;
		case UPDATE:
			do_update();
			break;
		case DELETE:
			do_delete();
			break;
		case LIST:
			do_list_all();
			break;
		case QUIT:
			do_quit();
			break;
		default:
			break;
	}
}
Пример #2
0
int main(int argc, char **argv)
{
	int opt;
	int op;
#define OP_NONE		0
#define OP_LIST		1
#define OP_LIST_ALL	2
#define OP_FLUSH	3
#define OP_FLUSH_ALL	4
#define OP_DESTROY	5
#define OP_DESTROY_ALL	6
#define OP_ADD		7
#define OP_DEL		8
#define OP_CHECK	9
#define OP_NEW		10
#define OP_NEW_ALL	11
#define OP_HIGHEST	12
	char *op_pool;

	fd = socket(AF_INET, SOCK_DGRAM, 0);
	if (fd < 0) {
		fprintf(stderr, "cannot get DGRAM socket: %s\n",
			strerror(errno));
		exit(1);
	}
	op_pool = 0;
	op = OP_NONE;
	/* GRRR. I thought getopt() would allow an "L*" specifier, for an -L
	 * taking an optional argument. Does not work. Bad.
	 * Adding -l for -L without argument, also -f/-F and -x/-X.
	 */
	while (EOF != (opt=getopt( argc, argv, "HhnvuqA:D:C:N:t:L:F:X:lfxB")))
	switch(opt) {
		case 'l':
			if (op != OP_NONE) usage("conflicting operations");
			op = OP_LIST_ALL;
			break;
		case 'L':
			if (op != OP_NONE) usage("conflicting operations");
			op = OP_LIST;
			op_pool = optarg;
			break;
		case 'f':
			if (op != OP_NONE) usage("conflicting operations");
			op = OP_FLUSH_ALL;
			break;
		case 'F':
			if (op != OP_NONE) usage("conflicting operations");
			op = OP_FLUSH;
			op_pool = optarg;
			break;
		case 'x':
			if (op != OP_NONE) usage("conflicting operations");
			op = OP_DESTROY_ALL;
			break;
		case 'X':
			if (op != OP_NONE) usage("conflicting operations");
			op = OP_DESTROY;
			op_pool = optarg;
			break;
		case 'A':
			if (op != OP_NONE) usage("conflicting operations");
			op = OP_ADD;
			op_pool = optarg;
			break;
		case 'D':
			if (op != OP_NONE) usage("conflicting operations");
			op = OP_DEL;
			op_pool = optarg;
			break;
		case 'C':
			if (op != OP_NONE) usage("conflicting operations");
			op = OP_CHECK;
			op_pool = optarg;
			break;
		case 'B':
			if (op != OP_NONE) usage("conflicting operations");
			op = OP_NEW_ALL;
			break;
		case 'N':
			if (op != OP_NONE) usage("conflicting operations");
			op = OP_NEW;
			op_pool = optarg;
			break;
		case 'H':
			if (op != OP_NONE) usage("conflicting operations");
			op = OP_HIGHEST;
			break;
		case 't':
			flag_t = optarg;
			break;
		case 'n':
			flag_n = 1;
			break;
		case 'v':
			flag_v = 1;
			break;
		case 'u':
			flag_u = 1;
			break;
		case 'q':
			flag_q = 1;
			break;
		case 'h':
			usage(0);
		default:
			usage("bad option");
	}
	if (op == OP_NONE)
		usage("no operation specified");
	if (op == OP_LIST_ALL) {
		do_list_all();
		return 0;
	}
	if (op == OP_LIST) {
		do_list(get_index(op_pool));
		return 0;
	}
	if (op == OP_FLUSH_ALL) {
		do_flush_all();
		return 0;
	}
	if (op == OP_FLUSH) {
		do_flush(get_index(op_pool));
		return 0;
	}
	if (op == OP_DESTROY_ALL) {
		do_destroy_all();
		return 0;
	}
	if (op == OP_DESTROY) {
		do_destroy(get_index(op_pool));
		return 0;
	}
	if (op == OP_CHECK) {
		if (optind >= argc)
			usage("missing address to check");
		return do_check(get_index(op_pool), argv[optind]);
	}
	if (op == OP_NEW_ALL) {
		do_new_all();
		return 0;
	}
	if (op == OP_NEW) {
		do_new(get_index(op_pool), argc-optind, argv+optind);
		return 0;
	}
	if (op == OP_ADD) {
		if (optind >= argc)
			usage("missing address to add");
		return do_adddel(get_index(op_pool),
				argv[optind], IP_POOL_ADD_ADDR);
	}
	if (op == OP_DEL) {
		if (optind >= argc)
			usage("missing address to delete");
		return do_adddel(get_index(op_pool),
				argv[optind], IP_POOL_DEL_ADDR);
	}
	if (op == OP_HIGHEST) {
		printf("%d\n", high_nr());
		return 0;
	}
	usage("no operation specified");
	return 0;
}
Пример #3
0
int main(int argc, char *argv[])
{
   int rc;
   FILE *fs = NULL;
   int32_t error_code;

#ifdef HAVE_LOCALE_H
   setlocale(LC_ALL, "");
#endif

   /* Log initially to stderr */
   log.OpenLog(stderr, LOG_ERR);
   /* parse the command line */
   if ((error_code = parse_cmdline(argc, argv)) != 0) {
      print_help();
      return 1;
   }
   /* Check for --version flag */
   if (cmdl.print_version) {
      print_version();
      return 0;
   }
   /* Check for --help flag */
   if (cmdl.print_help) {
      print_help();
      return 0;
   }
   /* Read vchanger config file */
   if (!conf.Read(cmdl.config_file)) {
      return 1;
   }
   /* User:group from cmdline overrides config file values */
   if (cmdl.runas_user.size()) conf.user = cmdl.runas_user;
   if (cmdl.runas_group.size()) conf.group = cmdl.runas_group;
   /* Pool from cmdline overrides config file */
   if (!cmdl.pool.empty()) conf.def_pool = cmdl.pool;
   /* If root, try to run as configured user:group */
   rc = drop_privs(conf.user.c_str(), conf.group.c_str());
   if (rc) {
      fprintf(stderr, "Error %d attempting to run as user '%s'", rc, conf.user.c_str());
      return 1;
   }
   /* Start logging to log file specified in configuration file */
   if (!conf.logfile.empty()) {
      fs = fopen(conf.logfile.c_str(), "a");
      if (fs == NULL) {
         fprintf(stderr, "Error opening opening log file\n");
         return 1;
      }
      log.OpenLog(fs, conf.log_level);
   }
   /* Validate and commit configuration parameters */
   if (!conf.Validate()) {
      return 1;
   }
   /* Initialize changer. A lock file is created to serialize access
    * to the changer. As a result, changer initialization may block
    * for up to 30 seconds, and may fail if a timeout is reached */
   if (changer.Initialize()) {
      fprintf(stderr, "%s\n", changer.GetErrorMsg());
      return 1;
   }

   /* Perform command */
   switch (cmdl.command) {
   case CMD_LIST:
      log.Debug("==== preforming LIST command pid=%d", getpid());
      error_code = do_list_cmd();
      break;
   case CMD_SLOTS:
      log.Debug("==== preforming SLOTS command pid=%d", getpid());
      error_code = do_slots_cmd();
      break;
   case CMD_LOAD:
      log.Debug("==== preforming LOAD command pid=%d", getpid());
      error_code = do_load_cmd();
      break;
   case CMD_UNLOAD:
      log.Debug("==== preforming UNLOAD command pid=%d", getpid());
      error_code = do_unload_cmd();
      break;
   case CMD_LOADED:
      log.Debug("==== preforming LOADED command pid=%d", getpid());
      error_code = do_loaded_cmd();
      break;
   case CMD_LISTALL:
      log.Debug("==== preforming LISTALL command pid=%d", getpid());
      error_code = do_list_all();
      break;
   case CMD_LISTMAGS:
      log.Debug("==== preforming LISTMAGS command pid=%d", getpid());
      error_code = do_list_magazines();
      break;
   case CMD_CREATEVOLS:
      log.Debug("==== preforming CREATEVOLS command pid=%d", getpid());
      error_code = do_create_vols();
      break;
   case CMD_REFRESH:
      log.Debug("==== preforming REFRESH command pid=%d", getpid());
      error_code = 0;
      log.Info("  SUCCESS pid=%d", getpid());
      break;
   }

   /* If there was an error, then exit */
   if (error_code) {
      changer.Unlock();
      return error_code;
   }

   /* If not updating Bacula, then exit */
   if (conf.bconsole.empty()) {
      /* Bacula interaction via bconsole is disabled, so log warnings */
      if (changer.NeedsUpdate())
         log.Error("WARNING! 'update slots' needed in bconsole pid=%d", getpid());
      if (changer.NeedsLabel())
         log.Error("WARNING! 'label barcodes' needed in bconsole pid=%d", getpid());
      changer.Unlock();
      return 0;
   }

   /* Update Bacula via bconsole */
#ifndef HAVE_WINDOWS_H
   changer.UpdateBacula();
#else
   /* Auto-update of bacula not working for Windows */
   if (changer.NeedsUpdate())
      log.Error("WARNING! 'update slots' needed in bconsole");
   if (changer.NeedsLabel())
      log.Error("WARNING! 'label barcodes' needed in bconsole");
   return 0;
#endif

   changer.Unlock();
   return 0;
}
Пример #4
0
int main (int argc, char **argv)
#endif
{
  int test_fd, listen_fd, cli_fd;
  socklen_t  cliaddr_len;
  struct sockaddr_un test_addr, listen_addr, cli_addr;
  const int on = 1;
  int nbytes;
  struct ucred cli_cred;
  int ucred_len = sizeof(cli_cred);
  struct be_msg smsg, rmsg;
  struct stat listen_stat;

  openlog("pvc2684d",LOG_PERROR, LOG_DAEMON);

  bzero(group_head.name,MAX_GROUPNAME_LEN);
  group_head.head = NULL;
  group_head.next = NULL;

  bzero(&test_addr, sizeof(test_addr));
  test_addr.sun_family = AF_LOCAL;
  strncpy(test_addr.sun_path, BRPVC_SOCKPATH, sizeof(test_addr.sun_path) -1);
  memcpy(&listen_addr, &test_addr, sizeof(test_addr));

  if( (test_fd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) 
    do_error(LOG_ERR,"Couldn't create initial socket: %s",strerror(errno));

  /* Check for already running daemon  */

  if(connect(test_fd, (struct sockaddr *) &test_addr, sizeof(test_addr))) {
    if(errno == ECONNREFUSED)
      unlink(BRPVC_SOCKPATH);
  }
  close(test_fd);

  if( (listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0)
    do_error(LOG_ERR,"Couldn't create server socket: %s",strerror(errno));

  if( bind(listen_fd, (struct sockaddr *) &listen_addr, SUN_LEN(&listen_addr)) ) {
    do_error(LOG_WARNING,"Another b2684d is running");
    exit(-1);
  }
  
  
  if(stat(BRPVC_SOCKPATH, &listen_stat))
    do_error(LOG_ERR,"Can't fstat listen socket: %s",strerror(errno));

  if(chmod(BRPVC_SOCKPATH, listen_stat.st_mode | S_IWOTH))
    do_error(LOG_ERR,"Can't fchmod listen socket: %s",strerror(errno));

  if( listen(listen_fd, 5) )
    do_error(LOG_ERR,"listen() on server socket failed: %s",strerror(errno));
  
  while(1) {
    cliaddr_len = sizeof(cli_addr);
    if((cli_fd = accept(listen_fd, (struct sockaddr *) &cli_addr, &cliaddr_len)) < 0) {
      if(errno == EINTR)
	continue;
      else
	do_error(LOG_ERR,"accept() on server socket failed: %s",strerror(errno));
    }
    if( setsockopt(cli_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0 )
      do_error(LOG_ERR,"setsockopt() on client socket failed: %s",strerror(errno));
    
    while((nbytes = recv(cli_fd, &rmsg, sizeof(rmsg), 0)) > 0) {
      switch (rmsg.msgtype) {
	
      case HELLO:
	
	if ( getsockopt(cli_fd, SOL_SOCKET, SO_PEERCRED, &cli_cred, &ucred_len) < 0 )
	  do_error(LOG_ERR,"getsockopt() for credentials failed: %s",strerror(errno));
	
	smsg.msgtype = OK;
	if( send(cli_fd, &smsg, sizeof(smsg), 0) < 0 )
	  do_error(LOG_ERR,"Couldn't send OK message to new client: %s",strerror(errno)); 

	break;
      
      case ADD:

	do_add(cli_fd,&rmsg,&cli_cred);
	break;
      
      case DELETE:

	do_delete(cli_fd,&rmsg,&cli_cred);
	break;
      
      case DELETE_GROUP:

	do_delete_group(cli_fd,rmsg.name,&cli_cred);
	break;
      
      case LIST_GROUP:
	 
	do_list_group(cli_fd,&rmsg);
	break;
      
      case LIST_ALL:

	do_list_all(cli_fd,&rmsg);
	break;

      case MEM_STATS:
	
	if( send(cli_fd, &memstat, sizeof(memstat), 0) < 0 )
	  do_error(LOG_ERR,"Couldn't send MEM_STAT message: %s",strerror(errno)); 
	break;
      
      default:
	smsg.msgtype = UNKNOWN_CMD;
	if( send(cli_fd, &smsg, sizeof(smsg), 0) < 0 )
	  do_error(LOG_ERR,"Couldn't send UNKNOWN_COMMAND message: %s",strerror(errno)); 
      }
    }
    close(cli_fd);
  }
}