livestats::livestats(const std::string plogfile) {
	reltimes times;
	timestamp timestamp;
	ConfigVAR *trafficstats;
	std::vector<std::string> confkeys;
		
	struct stat st;
	std::vector<std::string> keys;
	std::vector<std::string> ::iterator i,ki;
	std::map<std::string,bool> :: iterator cw;
	logfile = plogfile;

	if(stat(logfile.c_str(), &st)) { // this stat fails
		syslog(LOG_INFO,"No %s so cannot restore saved values", logfile.c_str()); 
		sleep(1);
		// no saved file no init possible
	}
	else {
		trafficstats = new ConfigVAR(logfile);
		confkeys = trafficstats->keys();

		// do we have a last_update key?
		if(((*trafficstats)["last_update"]) != "") {
			timestamp.t.tv_sec = (time_t)safeatoi((*trafficstats)["last_update"].c_str());
			timestamp.t.tv_usec = 0;
			keys = times.in_range(timestamp);

			// for each key we can transfer info from the old file into
			// our current one
			// keys are this_week etc

			// so now go through all the various patterns in the file
			// seeing which contain key phrases - if they do then
			// that whole data item can be imported

			// outer loop : what is in the config file
			for(i = confkeys.begin(); i != confkeys.end(); i++) {
				// inner loop the time range patterns
				for(ki = keys.begin(); ki != keys.end(); ki++) {
				// have we found time range as a substring of key from config file?
		
					if(i->find(*ki) != std::string::npos) {
						// one of our qualifying keys

						// only saved to nearest kb
						std::string sval = (*trafficstats)[ i->c_str() ];
						bytecount_t val =  safeatoi( sval.c_str() ) * SAVE_SCALE_FACTOR;
						// syslog(LOG_INFO,"using saved value for %s = %lld", i->c_str(), val); 
						byte_counts[*i] = val;
					}
					// dont bother restoring bitrates!
				}

			} // for(i = confkeys.begin(); i != confkeys.end(); i++)
		}
		delete trafficstats;
	}
}
Beispiel #2
0
int signalprocess( const char * pid_file, int signal )
{
	FILE *file = NULL;
	char buffer[STRING_SIZE];
	int pid = 0;
	
	memset(buffer, 0, STRING_SIZE);
	
	if ((file = fopen(pid_file, "r")) == NULL)
	{
		return -1;
	}
	if (fgets(buffer, STRING_SIZE - 1, file) == NULL)
	{
		fclose(file);
		return -2;
	}
	stripnl(buffer);
	pid = safeatoi(buffer);
	if (pid <= 1)
	{
		fclose(file);
		return -3;
	}

	fclose(file);

	int error = kill( pid, signal );
	return( error );
}
//#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@>
int snet2cidr(const std::string & argc)
{
 int rval       = 0;
 int count      = 0;
 int counter    = 0;
 int data       = 0;
 char largc[16] = "";
 char * ref     = largc;
 bool done      = false;

 strncpy((char*) largc, argc.c_str(), 15);

 // copy 15 characters (xxx.xxx.xxx.xxx) break them at the periods, and
 // select the possible binary values of netmask converting to the CIDR

 for (counter = 0, ref = strtok((char*) largc, "."); (!done) &&
  ref != NULL; ref = strtok(NULL, "."))
 {
  data = safeatoi (ref);

  // Let's check for some odd numbers to get things correct
  // If we get a value that is greater than 255 we are in deep doo-doo

  if (data > 255) return -1;  // A return of -1 should alert the caller

  if (data > 248 && data < 252) data = 248;
  if (data > 240 && data < 248) data = 240;
  if (data > 224 && data < 240) data = 224;
  if (data > 192 && data < 224) data = 192;
  if (data > 128 && data < 192) data = 128;

  switch (data)
  {
   case 255: count+= 8; break;  // 11111111  Get the next nybble
   case 254: count++;           // 11111110  Anything less than 255
   case 253:                    // 11111101  will be the last nybble
   case 252: count++;           // 11111100  
   case 248: count++;           // 111110XX  These are all handled by
   case 240: count++;           // 11110XXX  the IF statements above
   case 224: count++;           // 1110XXXX
   case 192: count++;           // 110XXXXX
   case 128: count++;           // 10XXXXXX
   default: done = true;        // 0XXXXXXX
  }
  if ( ++counter > 3) done = true;
 }
 rval = count;

 return rval;
}
int shutdown_command(std::vector<std::string> & parameters, std::string & response, char action)
{
	/* the parameter given to this function should be a time */
	/* to wait before shutting down                          */

	int time_hours = -1;
	int time_to_wait = -1;

	if (parameters.size() >= 1)
	{
		const char * position = parameters[ 0 ].c_str();
	
		while (*position)
		{
			if (((*position) < '0' || (*position) > '9') && ((*position) != ':'))
			{
				response = "Invalid time identifier";
				return 1;
			} 
			else if ((*position) == ':')
				time_hours = 1;
			position++;
		}
	}

	if (parameters.size() >= 1) time_to_wait = safeatoi(parameters[ 0 ]);

	char shutdown_time[ 255 ];

	if (time_hours == 1){
		snprintf(shutdown_time, sizeof(shutdown_time) - 1, "%s", parameters[ 0 ].c_str());
	} else if (time_to_wait > 0){
		snprintf(shutdown_time, sizeof(shutdown_time) - 1, "+%d", time_to_wait);
	} else {
		snprintf(shutdown_time, sizeof(shutdown_time) - 1, "now");
	}	

	unlink("/var/state/smoothd/shutdown");
	simplesecuresysteml("/sbin/shutdown", "-c", NULL);

	if (action != 'c')
	{
		pid_t child;
	
		child = fork();
		char command[ 255 ];
		FILE * output = NULL;
		
		switch (child)
		{
			case -1:
				syslog(LOG_ERR, "Unable to create subprocess for shutdown sequence");
				return 1;
			case 0:
				syslog(LOG_ERR, "Spawning child process\n");
				switch (fork())
				{
					case -1:
						syslog(LOG_ERR, "Unable to create subprocess for shutdown sequence (2)");
						_exit(1);
						
					case 0:
						snprintf(command, sizeof(command) - 1, "-%c", action);
						output = fopen("/var/state/smoothd/shutdown", "w");

						if (output)
						{
							if (time_hours == 1)
								fprintf(output, "%s", shutdown_time);
							else
							{
								char tstr[ 256 ];
								time_t t;
								time(&t);
								t += (time_to_wait*60);
								strftime(tstr, 256, "%H:%M", localtime(&t));
								fprintf(output, "%s", tstr);
							}
							fclose(output);
						}

						syslog(LOG_ERR, "Scheduling Shutdown \"shutdown -%c %s\"", action, shutdown_time);
						simplesecuresysteml("/sbin/shutdown", command, shutdown_time, NULL);
						_exit(1);
						
					default:
						_exit(0);
				}
				
			default:
				waitpid(child, NULL, 0);
		}
	}

	response = "Shutdown Scheduled";

	return 0;
}