Esempio n. 1
0
void restart_logging (ice_config_t *config)
{
    if (strcmp (config->error_log, "-"))
    {
        char fn_error[FILENAME_MAX];
        snprintf (fn_error, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->error_log);
        log_set_filename (errorlog, fn_error);
        log_set_level (errorlog, config->loglevel);
        log_set_trigger (errorlog, config->logsize);
        log_set_archive_timestamp(errorlog, config->logarchive);
        log_reopen (errorlog);
    }

    if (strcmp (config->access_log, "-"))
    {
        char fn_error[FILENAME_MAX];
        snprintf (fn_error, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->access_log);
        log_set_filename (accesslog, fn_error);
        log_set_trigger (accesslog, config->logsize);
        log_set_archive_timestamp (accesslog, config->logarchive);
        log_reopen (accesslog);
    }

    if (config->playlist_log)
    {
        char fn_error[FILENAME_MAX];
        snprintf (fn_error, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->playlist_log);
        log_set_filename (playlistlog, fn_error);
        log_set_trigger (playlistlog, config->logsize);
        log_set_archive_timestamp (playlistlog, config->logarchive);
        log_reopen (playlistlog);
    }
}
Esempio n. 2
0
File: blockmgr.c Progetto: s3v3ns/sx
static void sighandler(int signum) {
    if (signum == SIGHUP || signum == SIGUSR1) {
	log_reopen();
	return;
    }
    terminate = 1;
}
/*
 * mo_rehash - oper message handler
 * 
 * parv[1] = 'm' flushes the MOTD cache and returns
 * parv[1] = 'l' reopens the log files and returns
 * parv[1] = 'q' to not rehash the resolver (optional)
 */
int mo_rehash(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
{
  int flag = 0;

  if (!HasPriv(sptr, PRIV_REHASH))
    return send_reply(sptr, ERR_NOPRIVILEGES);

  if (parc > 1) { /* special processing */
    if (*parv[1] == 'm') {
      send_reply(sptr, SND_EXPLICIT | RPL_REHASHING, ":Flushing MOTD cache");
      motd_recache(); /* flush MOTD cache */
      return 0;
    } else if (*parv[1] == 'l') {
      send_reply(sptr, SND_EXPLICIT | RPL_REHASHING, ":Reopening log files");
      log_reopen(); /* reopen log files */
      return 0;
    } else if (*parv[1] == 'q')
      flag = 2;
  }

  send_reply(sptr, RPL_REHASHING, configfile);
  sendto_opmask_butone(0, SNO_OLDSNO, "%C is rehashing Server config file",
		       sptr);

  log_write(LS_SYSTEM, L_INFO, 0, "REHASH From %#C", sptr);

  return rehash(cptr, flag);
}
Esempio n. 4
0
int
main(void)
{
	log_t *lh;

	if ((lh = log_open("test.log", 0600)) == NULL) {
		err(EX_IOERR, "Cannot open log file");
	}
	if (!log_verify(lh))
		err(errno, "Failed to verify integrity of log file");

	log_printf(lh, "opened file handle: %d , inode: %llu", lh->fd, lh->ino);
	printf("logfile: %s, handle: %d, inode: %llu, mode: %d\n", lh->path, lh->fd, lh->ino, lh->mode);

	log_reopen(&lh);
	if (!log_verify(lh))
		err(errno, "Failed to verify integrity of reopened log file");

	log_printf(lh, "reopened file handle: %d , inode: %llu", lh->fd, lh->ino);
	printf("logfile: %s, handle: %d, inode: %llu, mode: %d\n", lh->path, lh->fd, lh->ino, lh->mode);

	for (int i = 1; i <= 4; i++)
		log_tsprintf(lh, "This is a time stamped message %d", i);

	log_close(lh);

	return 0;
}
Esempio n. 5
0
/** Restart the server with a message.
 * @param[in] message Message to log and send to operators.
 */
void server_restart(const char *message)
{
  static int restarting = 0;

  /* inhibit sending any server notices; we may be in a loop */
  log_write(LS_SYSTEM, L_WARNING, LOG_NOSNOTICE, "Restarting Server: %s",
	    message);
  if (restarting++) /* increment restarting to prevent looping */
    return;

  sendto_opmask_butone(0, SNO_OLDSNO, "Restarting server: %s", message);
  Debug((DEBUG_NOTICE, "Restarting server..."));
  flush_connections(0);

  log_close();

  close_connections(!(thisServer.bootopt & (BOOT_TTY | BOOT_DEBUG | BOOT_CHKCONF)));

  reap_children();

  execv(SPATH, thisServer.argv);

  /* Have to reopen since it has been closed above */
  log_reopen();

  log_write(LS_SYSTEM, L_CRIT, 0, "execv(%s,%s) failed: %m", SPATH,
	    *thisServer.argv);

  Debug((DEBUG_FATAL, "Couldn't restart server \"%s\": %s",
         SPATH, (strerror(errno)) ? strerror(errno) : ""));
  exit(8);
}
Esempio n. 6
0
static void signal_handler(int signum) {
    /* On some implementations (i.e. linuxthreads), signals are delivered
     * to all threads.  We only want to handle each signal once for the
     * entire box, and we let the gwthread wrapper take care of choosing
     * one.
     */
    if (!gwthread_shouldhandlesignal(signum))
        return;

    switch (signum) {
        case SIGINT:
            if (sqlbox_status == SQL_RUNNING) {
                error(0, "SIGINT received, aborting program...");
                sqlbox_status = SQL_SHUTDOWN;
            }
            break;

        case SIGHUP:
            warning(0, "SIGHUP received, catching and re-opening logs");
            log_reopen();
            alog_reopen();
            break;
        /*
         * It would be more proper to use SIGUSR1 for this, but on some
         * platforms that's reserved by the pthread support.
         */
        case SIGQUIT:
           warning(0, "SIGQUIT received, reporting memory usage.");
           gw_check_leaks();
           break;
    }
}
Esempio n. 7
0
static int recheck_log_file (ice_config_t *config, int *id, const char *file)
{
    char fn [FILENAME_MAX];

    if (file == NULL || strcmp (file, "-") == 0)
    {
        log_close (*id);
        *id = -1;
        return 0;
    }
    snprintf (fn, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, file);
    if (*id < 0)
    {
        *id = log_open (fn);
        if (*id < 0)
        {
            char buf[1024];
            snprintf (buf,1024, "could not open log %s: %s", fn, strerror(errno));
            fatal_error (buf);
            return -1;
        }
        return 0;
    }
    log_set_filename (*id, fn);
    log_reopen (*id);
    return 0;
}
Esempio n. 8
0
/*
 * mo_rehash - oper message handler
 * 
 * parv[1] = 'm' flushes the MOTD cache and returns
 * parv[1] = 'l' reopens the log files and returns
 * parv[1] = 'q' to not rehash the resolver (optional)
 * parv[1] = 's' to reload SSL certificates
 * parv[1] = 'a' to restart the IAuth program
 */
int mo_rehash(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
{
  int flag = 0;

  if (!HasPriv(sptr, PRIV_REHASH) || ((parc == 3) && !HasPriv(sptr, PRIV_REMOTEREHASH)))
    return send_reply(sptr, ERR_NOPRIVILEGES);

  if ((parc == 3) && (hunt_server_cmd(sptr, CMD_REHASH, cptr, 1, "%s %C", 2, parc, parv) != HUNTED_ISME))
    return 0;

  if (parc == 2) { /* special processing */
    if (parv[1][1] == '\0') { /* one character server name */
      if (*parv[1] == 'm') {
        send_reply(sptr, SND_EXPLICIT | RPL_REHASHING, ":Flushing MOTD cache");
        motd_recache(); /* flush MOTD cache */
        return 0;
      } else if (*parv[1] == 'l') {
        send_reply(sptr, SND_EXPLICIT | RPL_REHASHING, ":Reopening log files");
        log_reopen(); /* reopen log files */
        return 0;
      } else if (*parv[1] == 'a') {
        send_reply(sptr, SND_EXPLICIT | RPL_REHASHING, ":Restarting IAuth");
        auth_restart(); /* Restart IAuth program */
        return 0;
#ifdef USE_SSL
      } else if (*parv[1] == 's') {
        send_reply(sptr, SND_EXPLICIT | RPL_REHASHING, ":Reloading SSL certificates");
        ssl_reinit();
        return 0;
#endif
      } else if (*parv[1] == 'q')
        flag = 2;
    }
    /*
     * Maybe the user wants to rehash another server with no parameters.
     * NOTE: Here we assume that there are no servers named
     * 'm', 'l', 's', or 'q'.
     */
    else
      if (HasPriv(sptr, PRIV_REMOTEREHASH)) {
        if (hunt_server_cmd(sptr, CMD_REHASH, cptr, 1, "%C", 1, parc, parv) != HUNTED_ISME)
          return 0;
      } else
        return send_reply(sptr, ERR_NOPRIVILEGES);
  }

  send_reply(sptr, RPL_REHASHING, configfile);
  sendto_opmask_butone(0, SNO_OLDSNO, "%C is rehashing Server config file",
		       sptr);

  log_write(LS_SYSTEM, L_INFO, 0, "REHASH From %#C", sptr);

  return rehash(cptr, flag);
}
Esempio n. 9
0
/**
 * The logging uses log_level to determine if an incoming message is to be included in the log.
 * A high log_level setting will include more messages.
 */
void ert_log_init_log( int log_level , const char * log_file_name, bool verbose){
  logh = log_open( NULL , DEFAULT_LOG_LEVEL );
  
  log_set_level(logh, log_level);
  if (log_file_name)
    log_reopen( logh , log_file_name);

  if (verbose)
    printf("Activity will be logged to ..............: %s \n",log_get_filename( logh ));
  log_add_message(logh , 1 , NULL , "ert configuration loaded" , false);
}
Esempio n. 10
0
int log_start(struct passwd *ent_pw, int errfd)
{
  int ret = 0;

  echo_stderr = option_bool(OPT_DEBUG);

  if (daemon->log_fac != -1)
    log_fac = daemon->log_fac;
#ifdef LOG_LOCAL0
  else if (option_bool(OPT_DEBUG))
    log_fac = LOG_LOCAL0;
#endif

  if (daemon->log_file)
    { 
      log_to_file = 1;
      daemon->max_logs = 0;
      if (strcmp(daemon->log_file, "-") == 0)
	{
	  log_stderr = 1;
	  echo_stderr = 0;
	  log_fd = dup(STDERR_FILENO);
	}
    }
  
  max_logs = daemon->max_logs;

  if (!log_reopen(daemon->log_file))
    {
      send_event(errfd, EVENT_LOG_ERR, errno, daemon->log_file ? daemon->log_file : "");
      _exit(0);
    }

  /* if queuing is inhibited, make sure we allocate
     the one required buffer now. */
  if (max_logs == 0)
    {  
      free_entries = safe_malloc(sizeof(struct log_entry));
      free_entries->next = NULL;
      entries_alloced = 1;
    }

  /* If we're running as root and going to change uid later,
     change the ownership here so that the file is always owned by
     the dnsmasq user. Then logrotate can just copy the owner.
     Failure of the chown call is OK, (for instance when started as non-root) */
  if (log_to_file && !log_stderr && ent_pw && ent_pw->pw_uid != 0 && 
      fchown(log_fd, ent_pw->pw_uid, -1) != 0)
    ret = errno;

  return ret;
}
Esempio n. 11
0
int ms_rehash(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
{
  int flag = 0;

  if ((parc > 2) && (hunt_server_cmd(sptr, CMD_REHASH, cptr, 1, "%s %C", 2, parc, parv) != HUNTED_ISME))
    return 0;

  /* OK, the message has been forwarded, but before we can act... */
  if (!feature_bool(FEAT_NETWORK_REHASH))
    return 0;

  if (parc > 1) { /* special processing */
    if (parv[1][1] == '\0') { /* one character server name */
      if (*parv[1] == 'm') {
        send_reply(sptr, SND_EXPLICIT | RPL_REHASHING, ":Flushing MOTD cache");
        motd_recache(); /* flush MOTD cache */
        return 0;
      } else if (*parv[1] == 'l') {
        send_reply(sptr, SND_EXPLICIT | RPL_REHASHING, ":Reopening log files");
        log_reopen(); /* reopen log files */
        return 0;
      } else if (*parv[1] == 'a') {
        send_reply(sptr, SND_EXPLICIT | RPL_REHASHING, ":Restarting IAuth");
        auth_restart(); /* Restart IAuth program */
        return 0;
#ifdef USE_SSL
      } else if (*parv[1] == 's') {
        send_reply(sptr, SND_EXPLICIT | RPL_REHASHING, ":Reloading SSL certificates");
        ssl_reinit();
        return 0;
#endif
      } else if (*parv[1] == 'q')
        flag = 2;
    }
    /*
     * Maybe the user wants to rehash another server with no parameters.
     * NOTE: Here we assume that there are no servers named
     * 'm', 'l', 's', or 'q'.
     */
    else if ((parc == 2) && (hunt_server_cmd(sptr, CMD_REHASH, cptr, 1, "%C", 1, parc, parv) != HUNTED_ISME))
      return 0;
  }

  send_reply(sptr, RPL_REHASHING, configfile);
  sendto_opmask_butone(0, SNO_OLDSNO, "%C [%s] is remotely rehashing Server config file",
                       sptr, cli_name(cli_user(sptr)->server));

  log_write(LS_SYSTEM, L_INFO, 0, "Remote REHASH From %#C [%s]", sptr, cli_name(cli_user(sptr)->server));

  return rehash(cptr, flag);
}
Esempio n. 12
0
static void log_flush() {
	if (log_fd >= 0 && log_bufi != 0) {
		unsigned int wd,wr,wo=0;

		do {
			assert(wo < log_bufi);
			wr = log_bufi - wo;
			wd = write(log_fd,log_buf+wo,wr);
			if (/*another way to check for wd == -1*/wd > sizeof(log_buf)) wd = 0;

			// note what we could write
			if (wd > 0) wo += wd;

			if (wd < wr) {
				// make sure DOS flushes to disk
				if (log_fd >= 0) {
					close(log_fd);
					log_fd = -1;
				}

				// strange behavior in MS-DOS 5.0: if there is not enough disk space for X bytes, then it will
				// write 0 bytes and return to us that it wrote 0 bytes. not what I expected, coming from the
				// Linux world that would write as much as it can before giving up. --J.C.
				if (errno == ENOSPC) {
					printf("\nWrite: out of space (%u / %u written)\n",wd,wr);
					printf("Unable to write full log. Swap floppies and hit ENTER or CTRL+C to quit.\n");
					printf("You will have to assemble the full file from fragments when this is done.\n");
					do_pause();

					log_incname();
					if (!log_reopen()) {
						printf("Unable to reopen log.\n");
						exit(1);
					}
				}
				else {
					printf("\nError writing log: %s.\n",strerror(errno));
					exit(1);
				}
			}
			else {
				break;
			}
		} while (wo < log_bufi);
	}

	log_bufi = 0;
}
Esempio n. 13
0
File: log.c Progetto: jokva/ert
log_type * log_open( const char * filename , int log_level) {
    log_type   *logh;

    logh = util_malloc(sizeof *logh );

    logh->log_level     = log_level;
    logh->filename      = NULL;
    logh->stream        = NULL;
#ifdef HAVE_PTHREAD
    pthread_mutex_init( &logh->mutex , NULL );
#endif
    if (filename != NULL && log_level > 0)
        log_reopen( logh , filename);

    return logh;
}
Esempio n. 14
0
void
klog_flush(void *arg)
{
    if (klogger == NULL) {
        return;
    }

    klog_size += log_flush(klogger);
    if (klog_size >= klog_max) {
        if (log_reopen(klogger, klog_backup) != CC_OK) {
            log_error("klog rotation failed to reopen log file, stop logging");
            klog_enabled = false;
        }
        klog_size = 0;
    }
}
Esempio n. 15
0
/*
 * Act upon receiving signals
 */
void
signal_handler(int sig)
{
    switch (sig) {

    case SIGHUP:
        if (!use_syslog)
            log_reopen(&lfh);	/* necessary for log file
						 * rotation */
        break;
    case SIGINT:
    case SIGTERM:
        daemon_shutdown();
        exit(EXIT_SUCCESS);
        break;
    default:
        break;
    }
}
Esempio n. 16
0
static void sig_handler(int signal)
{
	if(server_shutdown_flag)	/* Already shutting down */
		return;

	switch(signal)
	{
		case SIGINT:
		case SIGTERM:
			log_wr(ILOG, "Terminating by signal %d.", signal);
			server_shutdown();
			break;
		case SIGUSR1:
			log_reopen();
			break;
		default:
			log_wr(ELOG, "Process terminated with unexpected signal %d.", signal);
			server_shutdown();
			break;
	}
}
Esempio n. 17
0
void log_start(struct passwd *ent_pw)
{
  log_stderr = !!(daemon->options & OPT_DEBUG);

  if (daemon->log_fac != -1)
    log_fac = daemon->log_fac;
#ifdef LOG_LOCAL0
  else if (daemon->options & OPT_DEBUG)
    log_fac = LOG_LOCAL0;
#endif

  if (daemon->log_file)
    { 
      log_to_file = 1;
      daemon->max_logs = 0;
    }
  
  max_logs = daemon->max_logs;

  if (!log_reopen(daemon->log_file))
    die(_("cannot open %s: %s"), daemon->log_file ? daemon->log_file : "log", EC_FILE);
  
  /* If we're running as root and going to change uid later,
     change the ownership here so that the file is always owned by
     the dnsmasq user. Then logrotate can just copy the owner.
     Failure of the chown call is OK, (for instance when started as non-root) */
  if (log_to_file && ent_pw && ent_pw->pw_uid != 0)
    fchown(log_fd, ent_pw->pw_uid, -1);
 
  /* if queuing is inhibited, make sure we allocate
     the one required buffer now. */
  if (max_logs == 0)
    {  
      free_entries = safe_malloc(sizeof(struct log_entry));
      free_entries->next = NULL;
      entries_alloced = 1;
    }
}
Esempio n. 18
0
static void signal_handler(int signum) 
{
    /* 
     * On some implementations (i.e. linuxthreads), signals are delivered
     * to all threads.  We only want to handle each signal once for the
     * entire box, and we let the gwthread wrapper take care of choosing
     * one. */
    if (!gwthread_shouldhandlesignal(signum))
        return;
    
    switch (signum) {
        case SIGINT:
        case SIGTERM:
            if (program_status != shutting_down) {
                error(0, "SIGINT or SIGTERM received, let's die.");
                program_status = shutting_down;
                break;
            }
            break;
    
        case SIGHUP:
            warning(0, "SIGHUP received, catching and re-opening logs");
            config_reload(1);
            log_reopen();
            alog_reopen();
            break;
    
        /* 
         * It would be more proper to use SIGUSR1 for this, but on some
         * platforms that's reserved by the pthread support. 
         */
        case SIGQUIT:
            warning(0, "SIGQUIT received, reporting memory usage.");
            gw_check_leaks();
            break;
        }
}
Esempio n. 19
0
void reload_config(void)
{
	struct Interface *iface;

	flog(LOG_INFO, "attempting to reread config file");

	dlog(LOG_DEBUG, 4, "reopening log");
	if (log_reopen() < 0) {
		perror("log_reopen");
		exit(1);
	}

	iface=IfaceList;
	while(iface)
	{
		struct Interface *next_iface = iface->next;
		struct AdvPrefix *prefix;
		struct AdvRoute *route;
		struct AdvRDNSS *rdnss;
		struct AdvDNSSL *dnssl;

		dlog(LOG_DEBUG, 4, "freeing interface %s", iface->Name);

		prefix = iface->AdvPrefixList;
		while (prefix)
		{
			struct AdvPrefix *next_prefix = prefix->next;

			free(prefix);
			prefix = next_prefix;
		}

		route = iface->AdvRouteList;
		while (route)
		{
			struct AdvRoute *next_route = route->next;

			free(route);
			route = next_route;
		}

		rdnss = iface->AdvRDNSSList;
		while (rdnss)
		{
			struct AdvRDNSS *next_rdnss = rdnss->next;

			free(rdnss);
			rdnss = next_rdnss;
		}

		dnssl = iface->AdvDNSSLList;
		while (dnssl)
		{
			struct AdvDNSSL *next_dnssl = dnssl->next;
			int i;

			for (i = 0; i < dnssl->AdvDNSSLNumber; i++)
				free(dnssl->AdvDNSSLSuffixes[i]);
			free(dnssl->AdvDNSSLSuffixes);
			free(dnssl);

			dnssl = next_dnssl;
		}

		free(iface);
		iface = next_iface;
	}

	IfaceList = NULL;

	/* reread config file */
	if (readin_config(conf_file) < 0) {
		perror("readin_config failed.");
		exit(1);
	}

	/* XXX: fails due to lack of permissions with non-root user */
	config_interface();
	kickoff_adverts();

	flog(LOG_INFO, "resuming normal operation");
}
Esempio n. 20
0
/** Reload the configuration file.
 * @param cptr Client that requested rehash (if a signal, &me).
 * @param sig Type of rehash (0 = oper-requested, 1 = signal, 2 =
 *   oper-requested but do not restart resolver)
 * @return CPTR_KILLED if any client was K/G-lined because of the
 * rehash; otherwise 0.
 */
int rehash(struct Client *cptr, int sig)
{
  struct ConfItem** tmp = &GlobalConfList;
  struct ConfItem*  tmp2;
  struct Client*    acptr;
  int               i;
  int               ret = 0;
  int               found_g = 0;

  if (1 == sig)
    sendto_opmask_butone(0, SNO_OLDSNO,
                         "Got signal SIGHUP, reloading ircd conf. file");

  while ((tmp2 = *tmp)) {
    if (tmp2->clients) {
      /*
       * Configuration entry is still in use by some
       * local clients, cannot delete it--mark it so
       * that it will be deleted when the last client
       * exits...
       */
      if (CONF_CLIENT == (tmp2->status & CONF_CLIENT))
        tmp = &tmp2->next;
      else {
        *tmp = tmp2->next;
        tmp2->next = 0;
      }
      tmp2->status |= CONF_ILLEGAL;
    }
    else {
      *tmp = tmp2->next;
      free_conf(tmp2);
    }
  }
  conf_erase_crule_list();
  conf_erase_deny_list();
  conf_erase_webirc_list();
  conf_erase_shost_list();
  conf_erase_except_list();
  motd_clear();

  /*
   * delete the juped nicks list
   */
  clearNickJupes();

  clear_quarantines();

  class_mark_delete();
  mark_listeners_closing();
  auth_mark_closing();
  close_mappings();

  read_configuration_file();

  if (sig != 2)
    restart_resolver();

  log_reopen(); /* reopen log files */

  auth_close_unused();
  close_listeners();
  class_delete_marked();         /* unless it fails */

  /*
   * Flush out deleted I and P lines although still in use.
   */
  for (tmp = &GlobalConfList; (tmp2 = *tmp);) {
    if (CONF_ILLEGAL == (tmp2->status & CONF_ILLEGAL)) {
      *tmp = tmp2->next;
      tmp2->next = NULL;
      if (!tmp2->clients)
        free_conf(tmp2);
    }
    else
      tmp = &tmp2->next;
  }

  for (i = 0; i <= HighestFd; i++) {
    if ((acptr = LocalClientArray[i])) {
      assert(!IsMe(acptr));
      if (IsServer(acptr))
        det_confs_butmask(acptr, ~(CONF_UWORLD | CONF_ILLEGAL));
      /* Because admin's are getting so uppity about people managing to
       * get past K/G's etc, we'll "fix" the bug by actually explaining
       * whats going on.
       */
      if ((found_g = find_kill(acptr))) {
        sendto_opmask_butone(0, found_g > -1 ? SNO_GLINE : SNO_OPERKILL,
                             found_g == -2 ? "G-line active for %s%s" :
                             (found_g == -3 ? "Z-line active for %s%s" :
                             "K-line active for %s%s"),
                             IsUnknown(acptr) ? "Unregistered Client ":"",
                             get_client_name(acptr, SHOW_IP));
        if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
            (found_g == -3 ? "Z-lined" : "K-lined")) == CPTR_KILLED)
          ret = CPTR_KILLED;
      }
    }
  }

  attach_conf_uworld(&me);

  geoip_init();

  auth_send_event("rehash", NULL);

  return ret;
}
Esempio n. 21
0
int main(int argc, char *argv[], char *envp[])
{
	s_bforce_opts opts;
	int rc = 0;
	int ch = 0;
	opts.runmode = MODE_UNDEFINED;
	
	memset(&opts, '\0', sizeof(s_bforce_opts));
	
	// parsing
	
	while( (ch=getopt(argc, argv, "hfI:p:n:l:a:u:oiC:S:dq")) != EOF )
	{
		switch( ch ) {
		case 'h':
			usage();
			exit(BFERR_NOERROR);
		case 'f':
			if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_CALL_MODEM;
			if( opts.runmode != MODE_CALL_MODEM || opts.force ) { usage(); exit(BFERR_FATALERROR); }
			opts.force = 1;
			break;
		case 'I':
			if( opts.incname || !optarg ) { usage(); exit(BFERR_FATALERROR); }  //free(opts.incname);
			opts.incname = (char *)xstrcpy(optarg);
			break;
		case 'p':
			if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_CALL_MODEM;
			if( opts.runmode != MODE_CALL_MODEM || opts.device || !optarg ) { usage(); exit(BFERR_FATALERROR); }
			opts.device = (char *)xstrcpy(optarg);
			break;
		case 'n':
			if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_CALL_MODEM;
			if( opts.runmode != MODE_CALL_MODEM || opts.phone || !optarg ) { usage(); exit(BFERR_FATALERROR); }
			//if( opts.phone ) free(opts.phone);
			opts.phone = (char *)xstrcpy(optarg);
			break;
		case 'l':
			if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_CALL_MODEM;
			if( opts.runmode != MODE_CALL_MODEM || opts.hiddline || !optarg || ISDEC(optarg) ) { usage(); exit(BFERR_FATALERROR); }
			opts.hiddline = atoi(optarg);
			break;
		case 'a':
			if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_CALL_IP;
			if( opts.runmode != MODE_CALL_IP || opts.iphost || !optarg ) { usage(); exit(BFERR_FATALERROR); }
			opts.iphost = (char *)xstrcpy(optarg);
			break;
		case 'u':
			if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_CALL_IP;
			if( opts.runmode != MODE_CALL_IP || opts.ipproto || !optarg ) { usage(); exit(BFERR_FATALERROR); }
			opts.ipproto = (char *)xstrcpy(optarg);
			break;
		case 'o':
			if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_CALL_STDIO;
			if( opts.runmode != MODE_CALL_STDIO || opts.usestdio ) { usage(); exit(BFERR_FATALERROR); }
			opts.usestdio = TRUE;
			break;
		case 'i':
			//if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_ANSWER;
			//if( opts.runmode != MODE_ANSWER || opts.inetd ) { usage(); exit(BFERR_FATALERROR); }
			opts.inetd = 1;
			break;
		case 'C':
			if( opts.confname || !optarg ) { usage(); exit(BFERR_FATALERROR); }
			opts.confname = (char *)xstrcpy(optarg);
			break;
		case 'S':
			if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_ANSWER;
			if( opts.runmode != MODE_ANSWER || opts.connect || !optarg ) { usage(); exit(BFERR_FATALERROR); }
			opts.connect = (char *)xstrcpy(optarg);
			break;
		case 'd':
			if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_DAEMON;
			if( opts.runmode != MODE_DAEMON || opts.daemon || opts.quit ) { usage(); exit(BFERR_FATALERROR); }
			opts.daemon = 1;
			break;
		case 'q':
			if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_DAEMON;
			if( opts.runmode != MODE_DAEMON || opts.quit ) { usage(); exit(BFERR_FATALERROR); }
			opts.quit = 1;
			opts.daemon = 1;
			break;
		default :
			usage();
			exit(BFERR_FATALERROR);
		}
	}
	
	
	/* Expression checker use it, so init first */
	init_state(&state);
	
	/* Set space available for process title */
#ifndef HAVE_SETPROCTITLE
	setargspace(argv, envp);
#endif
	
	/* Initialise random number generation */
	(void)srand((unsigned)time(NULL));
	
	/* Initialise current locale */
	(void)setlocale(LC_ALL, "");
	
	/* Set secure process umask */
	(void)umask(~(S_IRUSR|S_IWUSR));
	
	/* Now process non-option arguments */
	if( opts.daemon == FALSE )
	{
		const char *p;
		s_faddr addr;
		s_falist **alist = &opts.addrlist;
		
		while( (optind < argc) && (p = argv[optind++]) )
		{
			for( ; *p == '*'; p++ );
		
			if( strcasecmp(p, "tsync") == 0 )
			{
				if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_ANSWER;
				if( opts.runmode != MODE_ANSWER || opts.stype ) { usage(); exit(BFERR_FATALERROR); }
				opts.stype = SESSION_FTSC;
			}
			else if( strcasecmp(p, "yoohoo") == 0 )
			{
				if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_ANSWER;
				if( opts.runmode != MODE_ANSWER || opts.stype ) { usage(); exit(BFERR_FATALERROR); }
				opts.stype = SESSION_YOOHOO;
			}
			else if( strcasecmp(p, "**EMSI_INQC816") == 0 )
			{
				if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_ANSWER;
				if( opts.runmode != MODE_ANSWER || opts.stype ) { usage(); exit(BFERR_FATALERROR); }
				opts.stype = SESSION_EMSI;
			}
			else if( strncasecmp(p, "emsi", 4) == 0 )
			{
				if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_ANSWER;
				if( opts.runmode != MODE_ANSWER || opts.stype ) { usage(); exit(BFERR_FATALERROR); }
				opts.stype = SESSION_EMSI;
			}
			else if( strcasecmp(p, "binkp") == 0 )
			{
				if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_ANSWER;
				if( opts.runmode != MODE_ANSWER || opts.stype ) { usage(); exit(BFERR_FATALERROR); }
				opts.stype = SESSION_BINKP;
			}
			else if( strcasecmp(p, "auto") == 0 )
			{
				if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_ANSWER;
				if( opts.runmode != MODE_ANSWER || opts.stype ) { usage(); exit(BFERR_FATALERROR); }
				opts.stype = SESSION_UNKNOWN;
			}
			else if( ftn_addrparse(&addr, p, FALSE) == 0 )
			{
				if( opts.runmode == MODE_UNDEFINED ) opts.runmode = MODE_CALL_DEFAULT;
				if( opts.runmode != MODE_CALL_DEFAULT && opts.runmode != MODE_CALL_IP &&
				    opts.runmode != MODE_CALL_MODEM && opts.runmode != MODE_CALL_STDIO ) { usage(); exit(BFERR_FATALERROR); }
				(*alist) = (s_falist*)xmalloc(sizeof(s_falist));
				memset(*alist, '\0', sizeof(s_falist));
				(*alist)->addr = addr;
				alist = &(*alist)->next;
			}
			else
			{
				printf("invalid address \"%s\"", p);
				usage();
				exit(BFERR_FATALERROR);
			}
		}
	}

	/*
	  if( opts.inetd && opts.runmode != MODE_ANSWER && opts.runmode != MODE_CALL_STDIO )
	  {
	  usage();
	  exit(BFERR_FATALERROR);
	  }
	*/


/*	if( (rc = log_open(BFORCE_LOGFILE, NULL, NULL)) ) //compiled in
	{
		log("can't continue without logging");
		gotoexit(BFERR_FATALERROR);
	}*/
	
	/* Process primary config file */
	if( opts.confname && *opts.confname )
		rc = conf_readconf(opts.confname, 0);
	else
		rc = conf_readconf(conf_getconfname(), 0);
	
	if( rc ) gotoexit(BFERR_FATALERROR);
	
	/* Process additional config file, ignore errors */
	if( opts.incname && *opts.incname )
		(void)conf_readconf(opts.incname, 1);
	
	/* Reopen log file if it was defined in config */
	if( log_reopen(log_getfilename(LOG_FILE_SESSION), NULL, NULL) )
	{
		log("can't continue without logging");
		gotoexit(BFERR_FATALERROR);
	}
	
	//char runmode_str[21];
	//snprintf(runmode_str, 20, "Run mode: %d", opts.runmode);
	//log(runmode_str);
	
	switch( opts.runmode )
	{
case MODE_DAEMON:
		log("Daemon mode");
		rc = bforce_daemon(&opts);
		break;
case MODE_CALL_DEFAULT:
case MODE_CALL_IP:
case MODE_CALL_MODEM:
case MODE_CALL_STDIO:
		log("Outgoing call");
		rc = bforce_master(&opts);
		break;
case MODE_ANSWER:
		log("Start answer");
		rc = bforce_slave(&opts);
		break;
default:
		log("Could not determine run mode");
	}

exit:
	
	deinit_conf();
	deinit_opts(&opts);
	
	/* Shutdown logging services */
	if( log_isopened() ) log_close();
        DEB((D_FREE, "good exit"));
#ifdef DEBUG
	if( debug_isopened() ) debug_close();
#endif
	exit(rc);
}
Esempio n. 22
0
File: net.c Progetto: dolfly/nmdb
static void logfd_reopen_sighandler(int fd, short event, void *arg)
{
	if (log_reopen())
		wlog("Log reopened\n");
}
Esempio n. 23
0
/* END:   Added by z67728, 2009/12/26 */
void reload_config(void)
{
	struct Interface *iface;

	flog(LOG_INFO, "attempting to reread config file");

	dlog(LOG_DEBUG, 4, "reopening log");
	if (log_reopen() < 0)
		exit(1);

	/* disable timers, free interface and prefix structures */
	for(iface=IfaceList; iface; iface=iface->next)
	{
		/* check that iface->tm was set in the first place */
		if (iface->tm.next && iface->tm.prev)
		{
			dlog(LOG_DEBUG, 4, "disabling timer for %s", iface->Name);
			clear_timer(&iface->tm);
		}
	}

	iface=IfaceList; 
	while(iface)
	{
		struct Interface *next_iface = iface->next;
		struct AdvPrefix *prefix;
		struct AdvRoute *route;
		struct AdvRDNSS *rdnss;

		dlog(LOG_DEBUG, 4, "freeing interface %s", iface->Name);
		
		prefix = iface->AdvPrefixList;
		while (prefix)
		{
			struct AdvPrefix *next_prefix = prefix->next;
			
			free(prefix);
			prefix = next_prefix;
		}
		
		route = iface->AdvRouteList;
		while (route)
		{
			struct AdvRoute *next_route = route->next;

			free(route);
			route = next_route;
		}
		
		rdnss = iface->AdvRDNSSList;
		while (rdnss) 
		{
			struct AdvRDNSS *next_rdnss = rdnss->next;
			
			free(rdnss);
			rdnss = next_rdnss;
		}	 

		free(iface);
		iface = next_iface;
	}

	IfaceList = NULL;

	/* reread config file */
	if (readin_config(conf_file) < 0)
		exit(1);

	/* XXX: fails due to lack of permissions with non-root user */
	config_interface();
	kickoff_adverts();

	flog(LOG_INFO, "resuming normal operation");
}
Esempio n. 24
0
void
xfrd_reopen_logfile()
{
	if (xfrd->nsd->file_rotation_ok)
		log_reopen(xfrd->nsd->log_filename, 0);
}
Esempio n. 25
0
static void log_write(void)
{
  ssize_t rc;
   
  while (entries)
    {
      /* The data in the payoad is written with a terminating zero character 
	 and the length reflects this. For a stream connection we need to 
	 send the zero as a record terminator, but this isn't done for a 
	 datagram connection, so treat the length as one less than reality 
	 to elide the zero. If we're logging to a file, turn the zero into 
	 a newline, and leave the length alone. */
      int len_adjust = 0;

      if (log_to_file)
	entries->payload[entries->offset + entries->length - 1] = '\n';
      else if (connection_type == SOCK_DGRAM)
	len_adjust = 1;

      /* Avoid duplicates over a fork() */
      if (entries->pid != getpid())
	{
	  free_entry();
	  continue;
	}

      connection_good = 1;

      if ((rc = write(log_fd, entries->payload + entries->offset, entries->length - len_adjust)) != -1)
	{
	  entries->length -= rc;
	  entries->offset += rc;
	  if (entries->length == len_adjust)
	    {
	      free_entry();
	      if (entries_lost != 0)
		{
		  int e = entries_lost;
		  entries_lost = 0; /* avoid wild recursion */
		  my_syslog(LOG_WARNING, _("overflow: %d log entries lost"), e);
		}	  
	    }
	  continue;
	}
      
      if (errno == EINTR)
	continue;

      if (errno == EAGAIN || errno == EWOULDBLOCK)
	return; /* syslogd busy, go again when select() or poll() says so */
      
      if (errno == ENOBUFS)
	{
	  connection_good = 0;
	  return;
	}

      /* errors handling after this assumes sockets */ 
      if (!log_to_file)
	{
	  /* Once a stream socket hits EPIPE, we have to close and re-open
	     (we ignore SIGPIPE) */
	  if (errno == EPIPE)
	    {
	      if (log_reopen(NULL))
		continue;
	    }
	  else if (errno == ECONNREFUSED || 
		   errno == ENOTCONN || 
		   errno == EDESTADDRREQ || 
		   errno == ECONNRESET)
	    {
	      /* socket went (syslogd down?), try and reconnect. If we fail,
		 stop trying until the next call to my_syslog() 
		 ECONNREFUSED -> connection went down
		 ENOTCONN -> nobody listening
		 (ECONNRESET, EDESTADDRREQ are *BSD equivalents) */
	      
	      struct sockaddr_un logaddr;
	      
#ifdef HAVE_SOCKADDR_SA_LEN
	      logaddr.sun_len = sizeof(logaddr) - sizeof(logaddr.sun_path) + strlen(_PATH_LOG) + 1; 
#endif
	      logaddr.sun_family = AF_UNIX;
	      strncpy(logaddr.sun_path, _PATH_LOG, sizeof(logaddr.sun_path));
	      
	      /* Got connection back? try again. */
	      if (connect(log_fd, (struct sockaddr *)&logaddr, sizeof(logaddr)) != -1)
		continue;
	      
	      /* errors from connect which mean we should keep trying */
	      if (errno == ENOENT || 
		  errno == EALREADY || 
		  errno == ECONNREFUSED ||
		  errno == EISCONN || 
		  errno == EINTR ||
		  errno == EAGAIN || 
		  errno == EWOULDBLOCK)
		{
		  /* try again on next syslog() call */
		  connection_good = 0;
		  return;
		}
	      
	      /* try the other sort of socket... */
	      if (errno == EPROTOTYPE)
		{
		  connection_type = connection_type == SOCK_DGRAM ? SOCK_STREAM : SOCK_DGRAM;
		  if (log_reopen(NULL))
		    continue;
		}
	    }
	}

      /* give up - fall back to syslog() - this handles out-of-space
	 when logging to a file, for instance. */
      log_fd = -1;
      my_syslog(LOG_CRIT, _("log failed: %s"), strerror(errno));
      return;
    }
}
Esempio n. 26
0
int main(int argc, char **argv)
{
    int cf_index;
    Cfg *cfg;

    bb_status = BB_RUNNING;
    
    gwlib_init();
    start_time = time(NULL);

    suspended = gwlist_create();
    isolated = gwlist_create();
    gwlist_add_producer(suspended);
    gwlist_add_producer(isolated);

    cf_index = get_and_set_debugs(argc, argv, check_args);

    if (argv[cf_index] == NULL)
        cfg_filename = octstr_create("kannel.conf");
    else
        cfg_filename = octstr_create(argv[cf_index]);
    cfg = cfg_create(cfg_filename);
    
    if (cfg_read(cfg) == -1)
        panic(0, "Couldn't read configuration from `%s'.", octstr_get_cstr(cfg_filename));

    dlr_init(cfg);
    
    report_versions("bearerbox");

    flow_threads = gwlist_create();
    
    if (init_bearerbox(cfg) == NULL)
        panic(0, "Initialization failed.");

    info(0, "----------------------------------------");
    info(0, GW_NAME " bearerbox II version %s starting", GW_VERSION);

    gwthread_sleep(5.0); /* give time to threads to register themselves */

    if (store_load(dispatch_into_queue) == -1)
        panic(0, "Cannot start with store-file failing");
    
    info(0, "MAIN: Start-up done, entering mainloop");
    if (bb_status == BB_SUSPENDED) {
        info(0, "Gateway is now SUSPENDED by startup arguments");
    } else if (bb_status == BB_ISOLATED) {
        info(0, "Gateway is now ISOLATED by startup arguments");
        gwlist_remove_producer(suspended);
    } else {
        smsc2_resume(1);
        gwlist_remove_producer(suspended);	
        gwlist_remove_producer(isolated);
    }

    while (bb_status != BB_SHUTDOWN && bb_status != BB_DEAD && 
           gwlist_producer_count(flow_threads) > 0) {
        /* debug("bb", 0, "Main Thread: going to sleep."); */
        /*
         * Not infinite sleep here, because we should notice
         * when all "flow threads" are dead and shutting bearerbox
         * down.
         * XXX if all "flow threads" call gwthread_wakeup(MAIN_THREAD_ID),
         * we can enter infinite sleep then.
         */
        gwthread_sleep(10.0);
        /* debug("bb", 0, "Main Thread: woken up."); */

        if (bb_todo == 0) {
            continue;
        }

        if (bb_todo & BB_LOGREOPEN) {
            warning(0, "SIGHUP received, catching and re-opening logs");
            log_reopen();
            alog_reopen();
            bb_todo = bb_todo & ~BB_LOGREOPEN;
        }

        if (bb_todo & BB_CHECKLEAKS) {
            warning(0, "SIGQUIT received, reporting memory usage.");
            gw_check_leaks();
            bb_todo = bb_todo & ~BB_CHECKLEAKS;
        }
    }

    if (bb_status == BB_SHUTDOWN || bb_status == BB_DEAD)
        warning(0, "Killing signal or HTTP admin command received, shutting down...");

    /* call shutdown */
    bb_shutdown();

    /* wait until flow threads exit */
    while (gwlist_consume(flow_threads) != NULL)
        ;

    info(0, "All flow threads have died, killing core");
    bb_status = BB_DEAD;
    httpadmin_stop();

    boxc_cleanup();
    smsc2_cleanup();
    store_shutdown();
    empty_msg_lists();
    gwlist_destroy(flow_threads, NULL);
    gwlist_destroy(suspended, NULL);
    gwlist_destroy(isolated, NULL);
    mutex_destroy(status_mutex);

    alog_close();		/* if we have any */
    bb_alog_shutdown();
    cfg_destroy(cfg);
    octstr_destroy(cfg_filename);
    dlr_shutdown();

    /* now really restart */
    if (restart)
        restart_box(argv);

    gwlib_shutdown();

    return 0;
}
Esempio n. 27
0
static void sighandler (gint number)
{
        log_reopen (_log);
}