Esempio n. 1
0
/*
 * snmp_collator_create_semaphore()
 *
 * Create a semaphore to synchronize access to the stats file with
 * the SNMP sub-agent.  NSPR doesn't support a trywait function
 * for semaphores, so we just use POSIX semaphores directly.
 */
static void
snmp_collator_create_semaphore()
{
    /* First just try to create the semaphore.  This should usually just work. */
    if ((stats_sem = sem_open(stats_sem_name, O_CREAT | O_EXCL, SLAPD_DEFAULT_FILE_MODE, 1)) == SEM_FAILED) {
        if (errno == EEXIST) {
            /* It appears that we didn't exit cleanly last time and left the semaphore
             * around.  Recreate it since we don't know what state it is in. */
            if (sem_unlink(stats_sem_name) != 0) {
                LDAPDebug( LDAP_DEBUG_ANY, "Failed to delete old semaphore for stats file (%s). "
                          "Error %d (%s).\n", szStatsFile, errno, slapd_system_strerror(errno) );
                exit(1);
            }

            if ((stats_sem = sem_open(stats_sem_name, O_CREAT | O_EXCL, SLAPD_DEFAULT_FILE_MODE, 1)) == SEM_FAILED) {
                /* No dice */
                LDAPDebug( LDAP_DEBUG_ANY, "Failed to create semaphore for stats file (%s). Error %d (%s).\n",
                         szStatsFile, errno, slapd_system_strerror(errno) );
                exit(1);
            }
        } else {
            /* Some other problem occurred creating the semaphore. */
            LDAPDebug( LDAP_DEBUG_ANY, "Failed to create semaphore for stats file (%s). Error %d.(%s)\n",
                     szStatsFile, errno, slapd_system_strerror(errno) );
            exit(1);
        }
    }

    /* If we've reached this point, everything should be good. */
    return;
}
Esempio n. 2
0
char*
slapi_ch_strdup ( const char* s1)
{
    char* newmem;
	
	/* strdup pukes on NULL strings...bail out now */
	if(NULL == s1)
		return NULL;
	newmem = strdup (s1);
    if (newmem == NULL) {
		int	oserr = errno;
        oom_occurred();

		slapi_log_error( SLAPI_LOG_FATAL, SLAPD_MODULE,
		    "strdup of %lu characters failed; OS error %d (%s)%s\n",
			(unsigned long)strlen(s1), oserr, slapd_system_strerror( oserr ),
			oom_advice );
		exit (1);
    }
	if(!counters_created)
	{
		create_counters();
		counters_created= 1;
	}
    PR_INCREMENT_COUNTER(slapi_ch_counter_strdup);
    PR_INCREMENT_COUNTER(slapi_ch_counter_created);
    PR_INCREMENT_COUNTER(slapi_ch_counter_exist);
#if defined(_WIN32) && defined(DEBUG)
	if(recording)
	{
		add_memory_record(newmem,strlen(s1)+1);
	}
#endif
    return newmem;
}
Esempio n. 3
0
int snmp_collator_start()
{

  int err;
  char *statspath = config_get_rundir();
  char *instdir = config_get_configdir();
  char *instname = NULL;

  /*
   * Get directory for our stats file
   */
  if (NULL == statspath) {
     statspath = slapi_ch_strdup("/tmp");
  }

  instname = PL_strrstr(instdir, "slapd-");
  if (!instname) {
      instname = PL_strrstr(instdir, "/");
      if (instname) {
          instname++;
      }
  }
  PR_snprintf(szStatsFile, sizeof(szStatsFile), "%s/%s%s",
              statspath, instname, AGT_STATS_EXTENSION);
  PR_snprintf(stats_sem_name, sizeof(stats_sem_name), "/%s%s",
              instname, AGT_STATS_EXTENSION);
  tmpstatsfile = szStatsFile;
  slapi_ch_free_string(&statspath);
  slapi_ch_free_string(&instdir);

  /* open the memory map */
  if ((err = agt_mopen_stats(tmpstatsfile, O_RDWR,  &hdl) != 0))
  {
    if (err != EEXIST)      /* Ignore if file already exists */
    {
      slapi_log_error(SLAPI_LOG_FATAL, "snmp collator", "Failed to open stats file (%s) "
                      "(error %d): %s.\n", szStatsFile, err, slapd_system_strerror(err));
      exit(1);
    }
  }

  /* Create semaphore for stats file access */
  snmp_collator_create_semaphore();

  /* point stats struct at mmap data */
  stats = (struct agt_stats_t *) mmap_tbl [hdl].fp;

  /* initialize stats data */
  snmp_collator_init();

  /* Arrange to be called back periodically to update the mmap'd stats file. */
  snmp_eq_ctx = slapi_eq_repeat(snmp_collator_update, NULL, (time_t)0,
                                SLAPD_SNMP_UPDATE_INTERVAL);
  return 0;
}
Esempio n. 4
0
struct berval*
slapi_ch_bvdup (const struct berval* v)
{
    struct berval* newberval = ber_bvdup ((struct berval *)v);
    if (newberval == NULL) {
		int	oserr = errno;

	  	oom_occurred();
		slapi_log_error( SLAPI_LOG_FATAL, SLAPD_MODULE,
		    "ber_bvdup of %lu bytes failed; OS error %d (%s)%s\n",
			(unsigned long)v->bv_len, oserr, slapd_system_strerror( oserr ),
			oom_advice );
		exit( 1 );
    }
    return newberval;
}
Esempio n. 5
0
static int
slapi_ch_munmap_no_roundup(void **start, unsigned long len)
{
	void  *realstart = *start - sizeof(unsigned long);
	int    reallen = len + sizeof(unsigned long);
	int    rc = munmap(realstart, reallen);
	if (0 != rc) {
		int	oserr = errno;

		slapi_log_err(SLAPI_LOG_ERR, SLAPD_MODULE,
		    "munmap of %lu bytes failed; OS error %d (%s)\n",
			len, oserr, slapd_system_strerror( oserr ) );
		/* Leaked. This should not happen */
	}
	*start = NULL;
	return rc;
}
Esempio n. 6
0
int
slapi_ch_munmap(void **start, unsigned long len)
{
	long   sc_page_size = config_get_system_page_size();
	int	   sc_page_bits = config_get_system_page_bits();
	unsigned long roundup = (len&(sc_page_size-1))?(((len>>sc_page_bits)+1)<<sc_page_bits):len;
	void  *realstart = *start - sizeof(unsigned long);
	int    rc = munmap(realstart, roundup);
	if (0 != rc) {
		int	oserr = errno;

		slapi_log_err(SLAPI_LOG_ERR, SLAPD_MODULE,
		    "munmap of %lu bytes failed; OS error %d (%s)\n",
			roundup, oserr, slapd_system_strerror( oserr ) );
		/* Leaked. This should not happen */
	}
	*start = NULL;
	return rc;
}
Esempio n. 7
0
static char *
slapi_ch_calloc_core( unsigned long	lsize )
{
	char	*newmem;

	if ( (newmem = (char *) calloc( 1, lsize )) == NULL ) {
		int	oserr = errno;
	
		oom_occurred();
		slapi_log_err(SLAPI_LOG_ERR, SLAPD_MODULE,
		    "calloc of %lu bytes failed; OS error %d (%s)%s\n",
			lsize, oserr, slapd_system_strerror( oserr ), oom_advice );
		exit( 1 );
	}
	*(unsigned long *)newmem = lsize;
	newmem += sizeof(unsigned long);

	return newmem;
}
Esempio n. 8
0
char *
slapi_ch_calloc(
    unsigned long	nelem,
    unsigned long	size
)
{
	char	*newmem;

	if (size <= 0) {
		log_negative_alloc_msg( "calloc", "bytes", size );
		return 0;
	}

	if (nelem <= 0) {
		log_negative_alloc_msg( "calloc", "elements", nelem );
		return 0;
	}

	if ( (newmem = (char *) calloc( nelem, size )) == NULL ) {
		int	oserr = errno;

	  	oom_occurred();
		slapi_log_error( SLAPI_LOG_FATAL, SLAPD_MODULE,
		    "calloc of %lu elems of %lu bytes failed; OS error %d (%s)%s\n",
			nelem, size, oserr, slapd_system_strerror( oserr ), oom_advice );
		exit( 1 );
	}
	if(!counters_created)
	{
		create_counters();
		counters_created= 1;
	}
    PR_INCREMENT_COUNTER(slapi_ch_counter_calloc);
    PR_INCREMENT_COUNTER(slapi_ch_counter_created);
    PR_INCREMENT_COUNTER(slapi_ch_counter_exist);
#if defined(_WIN32) && defined(DEBUG)
	if(recording)
	{
		add_memory_record(newmem,size);
	}
#endif
	return( newmem );
}
Esempio n. 9
0
char *
slapi_ch_mmap(unsigned long len)
{
	char   *newmem;
	long   sc_page_size = config_get_system_page_size();
	int	   sc_page_bits = config_get_system_page_bits();
	unsigned long roundup = (len&(sc_page_size-1))?(((len>>sc_page_bits)+1)<<sc_page_bits):len;
	if ( (newmem = (char *)mmap(NULL, roundup, PROT_READ | PROT_WRITE,
			MAP_PRIVATE | MAP_ANONYMOUS, -1, 0/*ignored */)) == MAP_FAILED ) {
		int	oserr = errno;

	  	oom_occurred();
		slapi_log_err(SLAPI_LOG_ERR, SLAPD_MODULE,
		    "mmap of %lu bytes failed; OS error %d (%s)%s\n",
			roundup, oserr, slapd_system_strerror( oserr ), oom_advice );
		exit( 1 );
	}
	*(unsigned long *)newmem = roundup;
	newmem += sizeof(unsigned long);
	return( newmem );
}
Esempio n. 10
0
static char *
slapi_ch_mmap_no_roundup( unsigned long	size)
{
	char	*newmem;
	unsigned long	mysize;

	if ( (newmem = (char *)mmap(NULL, size + sizeof(unsigned long),
					PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
					-1, 0/*ignored */)) == MAP_FAILED ) {
		int	oserr = errno;
	
		oom_occurred();
		slapi_log_err(SLAPI_LOG_ERR, SLAPD_MODULE,
		    "mmap of %lu bytes failed; OS error %d (%s)%s\n",
			size + sizeof(unsigned long), oserr,
			slapd_system_strerror( oserr ), oom_advice );
		exit( 1 );
	}
	*(unsigned long *)newmem = size;
	newmem += sizeof(unsigned long);

	return newmem;
}