Esempio n. 1
33
static void do_close(JCR *jcr)
{
    release_device(jcr->dcr);
    free_attr(attr);
    free_record(rec);
    free_jcr(jcr);
    dev->term();
}
Esempio n. 2
0
static void stream_done(libxl__egc *egc,
                        libxl__stream_read_state *stream, int rc)
{
    libxl__sr_record_buf *rec, *trec;

    assert(stream->running);
    assert(!stream->in_checkpoint);
    stream->running = false;

    if (stream->incoming_record)
        free_record(stream->incoming_record);

    if (stream->emu_carefd)
        libxl__carefd_close(stream->emu_carefd);

    /* If we started a conversion helper, we took ownership of its carefd. */
    if (stream->chs.v2_carefd)
        libxl__carefd_close(stream->chs.v2_carefd);

    /* The record queue had better be empty if the stream believes
     * itself to have been successful. */
    assert(LIBXL_STAILQ_EMPTY(&stream->record_queue) || stream->rc);

    LIBXL_STAILQ_FOREACH_SAFE(rec, &stream->record_queue, entry, trec)
        free_record(rec);

    check_all_finished(egc, stream, rc);
}
Esempio n. 3
0
static int check_file_sorted(const char *file_path)
{
    FILE *f;
    void *record = NULL;
    int record_size;
    unsigned nrecords = (unsigned) (sizeof(data) / sizeof(int));
    unsigned i;

    f = fopen(file_path, "rb");
    cb_assert(f != NULL);

    for (i = 0; i < nrecords; ++i) {
        record_size = read_record(f, &record, NULL);
        cb_assert(record_size == sizeof(int));
        if (*((int *) record) != sorted_data[i]) {
            fclose(f);
            free_record(record, NULL);
            return 0;
        }
        free_record(record, NULL);
    }

    /* Check file has no extra (duplicated or garbage) records. */
    cb_assert(read_record(f, &record, NULL) == 0);

    fclose(f);

    return 1;
}
Esempio n. 4
0
static unsigned long check_file_sorted(const char *file_path)
{
    FILE *f;
    void *a = NULL, *b;
    int record_size;
    unsigned long num_records = 0;

    f = fopen(file_path, "rb");
    cb_assert(f != NULL);

    record_size = read_record(f, &a, NULL);
    cb_assert(record_size > 0);
    num_records += 1;

    while (record_size > 0) {
        record_size = read_record(f, &b, NULL);
        cb_assert(record_size >= 0);

        if (record_size > 0) {
            num_records += 1;
            cb_assert(compare_records(a, b, NULL) < 0);
            free_record(a, NULL);
            a = b;
        }
    }

    free_record(a, NULL);
    fclose(f);

    return num_records;
}
Esempio n. 5
0
int
check_proxyname(glite_renewal_core_context ctx, char *datafile, char *jobid, char **filename)
{
   proxy_record record;
   FILE *meta_fd = NULL;
   char proxy[FILENAME_MAX];
   char *p;
   int ret, i;
   char *basename;
   char *line = ctx->buffer;

   memset(&record, 0, sizeof(record));

   meta_fd = fopen(datafile, "r");
   if (meta_fd == NULL) {
      glite_renewal_core_set_err(ctx, "Cannot open meta file %s (%s)",
	           datafile, strerror(errno));
      return errno;
   }

   basename = strdup(datafile);
   p = basename + strlen(basename) - strlen(".data");
   *p = '\0';
   while (fgets(line, ctx->bufsize, meta_fd) != NULL) {
      free_record(ctx, &record);
      p = strchr(line, '\n');
      if (p)
	 *p = '\0';
      ret = decode_record(ctx, basename, line, &record);
      if (ret)
	 continue; /* XXX exit? */
      for (i = 0; i < record.jobids.len; i++) {
	 if (strcmp(jobid, record.jobids.val[i]) == 0) {
	    snprintf(proxy, sizeof(proxy), "%s/%s", repository, datafile);
	    p = strrchr(proxy, '.');
	    sprintf(p, ".%d", record.suffix);
	    *filename = strdup(proxy);
            free_record(ctx, &record);
	    fclose(meta_fd);
	    free(basename);
	    return 0;
	 }
      }
   }
   free_record(ctx, &record);
   fclose(meta_fd);
   free(basename);
   return EDG_WLPR_ERROR_PROTO_PARSE_NOT_FOUND;
}
Esempio n. 6
0
int DbDumperWriter::write_record(bool flush)
{
  int ret = OB_SUCCESS;
  RecordInfo * rec = NULL; 
  struct timespec timeout;
  UNUSED(flush);

  timeout.tv_sec = time(NULL) + kSemWaitTimeout;
  timeout.tv_nsec = 0;

  if (!records_.empty()) {
    CThreadGuard gard(&records_lock_);
    //no problem doing this, because only one thread is waiting on the queue
    rec = records_.front();
  } else if (running_) {
    sem_timedwait(&sem_empty_, &timeout);
  }

  if (rec != NULL) {
    CThreadGuard gard(&records_lock_);

    if (file_ == NULL || (ret = file_->Append(rec->buf, rec->length)) != OB_SUCCESS) {
      TBSYS_LOG(ERROR, "Write record failed, ret:%d, path:%s, len:%d", ret, path_.c_str(), rec->length);
    } else {
      writen_lines_++;
      records_.pop_front();
      free_record(rec);
    }
  }

  return ret;
}
Esempio n. 7
0
/* store_result() - store record from database in B-tree
 * this function is also a valid callback for use with sqlite3_exec()
 * pextra is again unused
 *
 * needs to return zero (nonzero aborts SQL query)
 */
int store_result(void * pextra, int nfields, char ** arrvalues, char ** arrfieldnames) {
    int n;

    /* allocate record on heap */
    struct s_record * prec = alloc_record();


    prec->irecord = record_count+1;
    for (n = 0; n < nfields; n++) {
        if (strcasecmp(arrfieldnames[n], "MovieTitle") == 0)
            prec->name = strdup(arrvalues[n]); /* key */
        else if (strcasecmp(arrfieldnames[n], "MovieCategory") == 0)
            prec->category = strdup(arrvalues[n]);
        else if (strcasecmp(arrfieldnames[n], "ProductionYear") == 0)
            prec->year = atoi(arrvalues[n]);
        else if (strcasecmp(arrfieldnames[n], "Format") == 0)
            prec->format = strdup(arrvalues[n]);
        else if (strcasecmp(arrfieldnames[n], "Language") == 0)
            prec->language = strdup(arrvalues[n]);
        else if (strcasecmp(arrfieldnames[n], "Web") == 0)
            prec->url = strdup(arrvalues[n]);
    }

    /* add record to B-tree */
    if (add_element(prec->name, prec) != NULL) {
        /* element already exists -- don't add record */
        printf("Duplicate record exists: "); /* diagnostic value only */
        display_record(prec, stdout);
        free_record(prec);
    }

    return 0;
}
Esempio n. 8
0
void
clear_variables(void)
{
	int i;
	assert(initialized);

	/* free memory */
	for(i = 0; i < nvars; i++)
	{
		if(vars[i].name == NULL)
			continue;

		if(vars[i].from_parent)
		{
			set_envvar(vars[i].name, vars[i].initial);
		}
		else
		{
			env_remove(vars[i].name);
		}
		free_record(&vars[i]);
	}

	nvars = 0;
	free(vars);
	vars = NULL;
}
Esempio n. 9
0
/*
 * Free up all aspects of the given dcr -- i.e. dechain it,
 *  release allocated memory, zap pointers, ...
 */
void free_dcr(DCR *dcr)
{
   JCR *jcr;

   P(dcr->m_mutex);
   jcr = dcr->jcr;

   locked_detach_dcr_from_dev(dcr);

   if (dcr->block) {
      free_block(dcr->block);
   }

   if (dcr->rec) {
      free_record(dcr->rec);
   }

   if (jcr && jcr->dcr == dcr) {
      jcr->dcr = NULL;
   }

   if (jcr && jcr->read_dcr == dcr) {
      jcr->read_dcr = NULL;
   }

   V(dcr->m_mutex);

   pthread_mutex_destroy(&dcr->m_mutex);
   pthread_mutex_destroy(&dcr->r_mutex);

   delete dcr;
}
Esempio n. 10
0
DataRecord* decompress_record(DataRecord *r)
{
    if (r->flag & COMPRESS_FLAG) {
        char scratch[QLZ_SCRATCH_DECOMPRESS];
        int csize = qlz_size_compressed(r->value);
        if (csize != r->vsz) {
            fprintf(stderr, "broken compressed data: %d != %d, flag=%x\n", csize, r->vsz, r->flag);
            goto DECOMP_END;
        }
        int size = qlz_size_decompressed(r->value);
        char *v = malloc(size);
        if (v == NULL) {
            fprintf(stderr, "malloc(%d)\n", size);
            goto DECOMP_END;
        }
        int ret = qlz_decompress(r->value, v, scratch);
        if (ret != size) {
            fprintf(stderr, "decompress %s failed: %d != %d\n", r->key, ret, size);
            goto DECOMP_END;
        }
        if (r->free_value) {
            free(r->value);
        }
        r->value = v;
        r->free_value = true;
        r->vsz = size;
        r->flag &= ~COMPRESS_FLAG;
    }
    return r;

DECOMP_END:
    free_record(r); 
    return NULL;
}
Esempio n. 11
0
DataRecord* bc_get(Bitcask *bc, const char* key)
{
    Item *item = ht_get(bc->tree, key);
    if (NULL == item) return NULL;
    if (item->ver < 0){
        free(item);
        return NULL;
    }
    
    int bucket = item->pos & 0xff;
    uint32_t pos = item->pos & 0xffffff00;
    if (bucket > bc->curr) {
        fprintf(stderr, "BUG: invalid bucket %d > %d\n", bucket, bc->curr);
        ht_remove(bc->tree, key);
        free(item);
        return NULL;
    }

    DataRecord* r = NULL;
    if (bucket == bc->curr) {
        pthread_mutex_lock(&bc->buffer_lock);
        if (bucket == bc->curr && pos >= bc->wbuf_start_pos){
            int p = pos - bc->wbuf_start_pos;
            r = decode_record(bc->write_buffer + p, bc->wbuf_curr_pos - p, true);
        }
        pthread_mutex_unlock(&bc->buffer_lock);
        
        if (r != NULL){
            free(item);
            return r;
        }
    }
        
    char fname[20], data[255];
    const char * path = mgr_base(bc->mgr);
    sprintf(fname, DATA_FILE, bucket);
    sprintf(data, "%s/%s", path, fname);
    int fd = open(data, O_RDONLY);
    if (-1 == fd){
        goto GET_END;
    }
    
    r = fast_read_record(fd, pos, true);
    if (NULL == r){
        fprintf(stderr, "Bug: get %s failed in %s %d %d\n", key, path, bucket, pos); 
    }else{
         // check key
        if (strcmp(key, r->key) != 0){
            fprintf(stderr, "Bug: record %s is not expected %s\n", r->key, key);
            free_record(r);
            r = NULL;
        } 
    }
GET_END:
    if (NULL == r)
        ht_remove(bc->tree, key);
    if (fd != -1) close(fd);
    free(item);
    return r;
}
Esempio n. 12
0
static int
fgraph_ent_handler(struct trace_seq *s, struct tep_record *record,
		   struct tep_event *event, void *context)
{
	struct tracecmd_ftrace *finfo = context;
	struct tep_record *rec;
	unsigned long long val, pid;
	int cpu;

	ret_event_check(finfo, event->tep);

	if (tep_get_common_field_val(s, event, "common_pid", record, &pid, 1))
		return trace_seq_putc(s, '!');

	if (tep_get_field_val(s, event, "func", record, &val, 1))
		return trace_seq_putc(s, '!');

	rec = tracecmd_peek_next_data(tracecmd_curr_thread_handle, &cpu);
	if (rec)
		rec = get_return_for_leaf(s, cpu, pid, val, rec, finfo);

	if (rec) {
		/*
		 * If this is a leaf function, then get_return_for_leaf
		 * returns the return of the function
		 */
		print_graph_entry_leaf(s, event, record, rec, finfo);
		free_record(rec);
	} else
		print_graph_nested(s, event, record);

	return 0;
}
Esempio n. 13
0
char *hs_get(HStore *store, char *key, int *vlen, uint32_t *flag)
{
    if (!key || !store) return NULL;

    if (key[0] == '@'){
        char *r = hs_list(store, key+1);
        if (r) *vlen = strlen(r);
        *flag = 0;
        return r;
    }
    
    bool info = false;
    if (key[0] == '?'){
        info = true;
        key ++;
    }
    int index = get_index(store, key);
    DataRecord *r = bc_get(store->bitcasks[index], key);
    if (r == NULL){
        return NULL;
    }
    
    char *res = NULL;
    if (info){
        res = malloc(256);
        if (!res) {
            free_record(r);
            return NULL;
        }
        uint16_t hash = 0;
        if (r->version > 0){
            hash = gen_hash(r->value, r->vsz);
        }
        *vlen = snprintf(res, 255, "%d %u %u %u %u", r->version, 
            hash, r->flag, r->vsz, r->tstamp);
        *flag = 0;
    }else if (r->version > 0){
        res = record_value(r);
        r->value = NULL;
        *vlen = r->vsz;
        *flag = r->flag;
    }
    free_record(r);
    return res;
}
Esempio n. 14
0
/*
 * Write a Record to the block
 *
 * Returns: false means the block could not be written to tape/disk.
 *          true on success (all bytes written to the block).
 */
bool DCR::write_record()
{
   bool retval = false;
   bool translated_record = false;
   char buf1[100], buf2[100];

   /*
    * Perform record translations.
    */
   before_rec = rec;
   after_rec = NULL;
   if (generate_plugin_event(jcr, bsdEventWriteRecordTranslation, this) != bRC_OK) {
      goto bail_out;
   }

   /*
    * The record got translated when we got an after_rec pointer after calling the
    * bsdEventWriteRecordTranslation plugin event. If no translation has taken place
    * we just point the after_rec pointer to same DEV_RECORD as in the before_rec pointer.
    */
   if (!after_rec) {
      after_rec = before_rec;
   } else {
      translated_record = true;
   }

   while (!write_record_to_block(this, after_rec)) {
      Dmsg2(850, "!write_record_to_block data_len=%d rem=%d\n",
            after_rec->data_len, after_rec->remainder);
      if (!write_block_to_device()) {
         Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
               dev->print_name(), dev->bstrerror());
         goto bail_out;
      }
   }

   jcr->JobBytes += after_rec->data_len;   /* increment bytes this job */
   if (jcr->RemainingQuota && jcr->JobBytes > jcr->RemainingQuota) {
      Jmsg0(jcr, M_FATAL, 0, _("Quota Exceeded. Job Terminated.\n"));
      goto bail_out;
   }

   Dmsg4(850, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
         FI_to_ascii(buf1, after_rec->FileIndex), after_rec->VolSessionId,
         stream_to_ascii(buf2, after_rec->Stream, after_rec->FileIndex), after_rec->data_len);

   retval = true;

bail_out:
   if (translated_record) {
      copy_record_state(before_rec, after_rec);
      free_record(after_rec);
      after_rec = NULL;
   }

   return retval;
}
Esempio n. 15
0
static void do_trace_mem(struct tracecmd_input *handle)
{
	struct pevent *pevent = tracecmd_get_pevent(handle);
	struct event_format *event;
	struct pevent_record *record;
	int missed_events = 0;
	int cpus;
	int cpu;
	int ret;

	ret = tracecmd_init_data(handle);
	if (ret < 0)
		die("failed to init data");

	if (ret > 0)
		die("trace-cmd mem does not work with latency traces\n");

	cpus = tracecmd_cpus(handle);

	/* Need to get any event */
	for (cpu = 0; cpu < cpus; cpu++) {
		record = tracecmd_peek_data(handle, cpu);
		if (record)
			break;
	}
	if (!record)
		die("No records found in file");

	ret = pevent_data_type(pevent, record);
	event = pevent_data_event_from_type(pevent, ret);

	common_type_field = pevent_find_common_field(event, "common_type");
	if (!common_type_field)
		die("Can't find a 'type' field?");

	update_kmalloc(pevent);
	update_kmalloc_node(pevent);
	update_kfree(pevent);
	update_kmem_cache_alloc(pevent);
	update_kmem_cache_alloc_node(pevent);
	update_kmem_cache_free(pevent);

	while ((record = tracecmd_read_next_data(handle, &cpu))) {

		/* record missed event */
		if (!missed_events && record->missed_events)
			missed_events = 1;

		process_record(pevent, record);
		free_record(record);
	}

	sort_list();
	print_list();
}
Esempio n. 16
0
/*
 * Setup DCR with a new device.
 */
void setup_new_dcr_device(JCR *jcr, DCR *dcr, DEVICE *dev, BLOCKSIZES *blocksizes)
{
   dcr->jcr = jcr;                 /* point back to jcr */

   /*
    * Set device information, possibly change device
    */
   if (dev) {
      /*
       * Set wanted blocksizes
       */
      if (blocksizes) {
         dev->min_block_size = blocksizes->min_block_size;
         dev->max_block_size = blocksizes->max_block_size;
      }

      if (dcr->block) {
         free_block(dcr->block);
      }
      dcr->block = new_block(dev);

      if (dcr->rec) {
         free_record(dcr->rec);
         dcr->rec = NULL;
      }
      dcr->rec = new_record();

      if (dcr->attached_to_dev) {
         detach_dcr_from_dev(dcr);
      }

      /*
       * Use job spoolsize prior to device spoolsize
       */
      if (jcr && jcr->spool_size) {
         dcr->max_job_spool_size = jcr->spool_size;
      } else {
         dcr->max_job_spool_size = dev->device->max_job_spool_size;
      }

      dcr->device = dev->device;
      dcr->set_dev(dev);
      attach_dcr_to_dev(dcr);

      /*
       * Initialize the auto deflation/inflation which can
       * be disabled per DCR when we want to. e.g. when we want to
       * send the data as part of a replication stream in which we
       * don't want to first inflate the data to then again
       * do deflation for sending it to the other storage daemon.
       */
      dcr->autodeflate = dcr->device->autodeflate;
      dcr->autoinflate = dcr->device->autoinflate;
   }
}
Esempio n. 17
0
DataRecord* fast_read_record(int fd, off_t offset, bool decomp)
{
    DataRecord *r = (DataRecord*) malloc(PADDING + sizeof(char*));
    r->value = NULL;
   
    if (pread(fd, &r->crc, PADDING, offset) != PADDING) {
        fprintf(stderr, "read record faied\n");         
        goto READ_END;
    }

    int ksz = r->ksz, vsz = r->vsz;
    if (ksz < 0 || ksz > 200 || vsz < 0 || vsz > 100 * 1024 * 1024){
        fprintf(stderr, "invalid ksz=: %d, vsz=%d\n", ksz, vsz);
        goto READ_END;
    }
  
    uint32_t crc_old = r->crc;
    int read_size = PADDING - (sizeof(DataRecord) - sizeof(char*)) - ksz;
    if (vsz < read_size) {
        r->value = r->key + ksz + 1;
        r->free_value = false;
        memmove(r->value, r->key + ksz, vsz);
    }else{
        r->value = malloc(vsz);
        r->free_value = true;
        memcpy(r->value, r->key + ksz, read_size);
        int need = vsz - read_size;
        int ret = 0;
        if (need > 0 && need != (ret=pread(fd, r->value + read_size, need, offset+PADDING))) {
            r->key[ksz] = 0; // c str    
            fprintf(stderr, "read record %s faied: %d < %d @%ld\n", r->key, ret, need, offset); 
            goto READ_END;
        }
    }
    r->key[ksz] = 0; // c str    

    uint32_t crc = crc32(0, (char*)(&r->tstamp), 
                    sizeof(DataRecord) - sizeof(char*) - sizeof(uint32_t) + ksz);
    crc = crc32(crc, r->value, vsz);
    if (crc != crc_old){
        fprintf(stderr, "%s @%ld crc32 check failed %d != %d\n", r->key, offset, crc, r->crc);
        goto READ_END;
    }

    if (decomp) {
        r = decompress_record(r);
    }
    return r;
    
READ_END:
    free_record(r);
    return NULL; 
}
Esempio n. 18
0
/* free_tnode() - frees a node in the B-tree,
 * its associated record, and all its children from memory
 */
void free_tnode(p_tnode pnode) {
    unsigned int n;
    for (n = 0; n < 2*T; n++) {
        if (pnode->children[n] != NULL)
            free_tnode(pnode->children[n]);
    }

    for (n = 0; n < pnode->nkeys; n++) {
        if (pnode->values[n])
            free_record(pnode->values[n]);
    }

    free(pnode);
}
Esempio n. 19
0
/*
 * Create a new Device Control Record and attach
 *   it to the device (if this is a real job).
 * Note, this has been updated so that it can be called first
 *   without a DEVICE, then a second or third time with a DEVICE,
 *   and each time, it should cleanup and point to the new device.
 *   This should facilitate switching devices.
 * Note, each dcr must point to the controlling job (jcr).  However,
 *   a job can have multiple dcrs, so we must not store in the jcr's
 *   structure as previously. The higher level routine must store
 *   this dcr in the right place
 *
 */
DCR *new_dcr(JCR *jcr, DCR *dcr, DEVICE *dev, bool writing)
{
   DEVICE *odev;
   if (!dcr) {
      dcr = (DCR *)malloc(sizeof(DCR));
      memset(dcr, 0, sizeof(DCR));
      dcr->tid = pthread_self();
      dcr->spool_fd = -1;
   }
   dcr->jcr = jcr;                 /* point back to jcr */
   odev = dcr->dev;
   if (dcr->attached_to_dev && odev) {
      Dmsg2(100, "Detach 0x%x from olddev %s\n", dcr, odev->print_name());
      odev->detach_dcr_from_dev(dcr);
   }
   ASSERT2(!dcr->attached_to_dev, "DCR is attached. Wrong!");
   /* Set device information, possibly change device */
   if (dev) {
      dcr->free_blocks();
      dcr->block = new_block(dev);
      dcr->ameta_block = dcr->block;
      if (dcr->rec) {
         free_record(dcr->rec);
      }
      dcr->rec = new_record();
      /* Use job spoolsize prior to device spoolsize */
      if (jcr && jcr->spool_size) {
         dcr->max_job_spool_size = jcr->spool_size;
      } else {
         dcr->max_job_spool_size = dev->device->max_job_spool_size;
      }
      dcr->device = dev->device;
      dcr->set_dev(dev);
      Dmsg2(100, "Attach 0x%x to dev %s\n", dcr, dev->print_name());
      dev->attach_dcr_to_dev(dcr);
   }
   if (writing) {
      dcr->set_writing();
   } else {
      dcr->clear_writing();
   }
   return dcr;
}
Esempio n. 20
0
static void
init_var(const char *env)
{
	envvar_t *record;
	char name[VAR_NAME_MAX + 1];
	char *p = strchr(env, '=');
	assert(p != NULL);

	snprintf(name, MIN(sizeof(name), p - env + 1), "%s", env);
	record = get_record(name);
	if(record == NULL)
		return;
	record->from_parent = 1;

	(void)replace_string(&record->initial, p + 1);
	(void)replace_string(&record->val, p + 1);
	if(record->initial == NULL || record->val == NULL)
	{
		free_record(record);
	}
}
Esempio n. 21
0
/*
 * Free up all aspects of the given dcr -- i.e. dechain it,
 *  release allocated memory, zap pointers, ...
 */
void free_dcr(DCR *dcr)
{
   JCR *jcr;

   jcr = dcr->jcr;

   if (dcr->dev) {
      dcr->dev->detach_dcr_from_dev(dcr);
   }

   dcr->free_blocks();
   if (dcr->rec) {
      free_record(dcr->rec);
   }
   if (jcr && jcr->dcr == dcr) {
      jcr->dcr = NULL;
   }
   if (jcr && jcr->read_dcr == dcr) {
      jcr->read_dcr = NULL;
   }
   free(dcr);
}
Esempio n. 22
0
/* searches for variable and creates new record if it didn't existed */
static envvar_t *
get_record(const char *name)
{
	envvar_t *p = NULL;
	int i;

	/* search for existent variable */
	for(i = 0; i < nvars; i++)
	{
		if(vars[i].name == NULL)
			p = &vars[i];
		else if(strcmp(vars[i].name, name) == 0)
			return &vars[i];
	}

	if(p == NULL)
	{
		/* try to reallocate list of variables */
		p = realloc(vars, sizeof(*vars)*(nvars + 1));
		if(p == NULL)
			return NULL;
		vars = p;
		p = &vars[nvars];
	}

	/* initialize new record */
	p->initial = strdup("");
	p->name = strdup(name);
	p->val = strdup("");
	p->from_parent = 0;
	p->removed = 0;
	if(p->initial == NULL || p->name == NULL || p->val == NULL)
	{
		free_record(p);
		return NULL;
	}
	nvars++;
	return p;
}
Esempio n. 23
0
void scanDataFileBefore(HTree* tree, int bucket, const char* path, time_t before)
{
    MFile *f = open_mfile(path);
    if (f == NULL) return;
    
    fprintf(stderr, "scan datafile %s before %ld\n", path, before);
    char *p = f->addr, *end = f->addr + f->size;
    int broken = 0;
    while (p < end) {
        DataRecord *r = decode_record(p, end-p, false);
        if (r != NULL) {
            if (r->tstamp >= before ){
                break;
            }
            uint32_t pos = p - f->addr;
            p += record_length(r); 
            r = decompress_record(r);
            uint16_t hash = gen_hash(r->value, r->vsz);
            if (r->version > 0){
                uint16_t hash = gen_hash(r->value, r->vsz);
                ht_add2(tree, r->key, r->ksz, pos | bucket, hash, r->version);            
            }else{
                ht_remove2(tree, r->key, r->ksz);
            }
            free_record(r);
        } else {
            broken ++;
            if (broken > 40960) { // 10M
                fprintf(stderr, "unexpected broken data in %s at %ld\n", path, p - f->addr - broken * PADDING);
                break;
            }
            p += PADDING;
        }
    }

    close_mfile(f);
}
Esempio n. 24
0
/* Get proxy record from the index file. If no suffix is defined return a free 
   record with the smallest index */
static int
get_record_ext(glite_renewal_core_context ctx, FILE *fd, const char *basename, proxy_record *record, int *last_used_suffix)
{
   int last_suffix = -1;
   int first_unused = -1;
   int ret;
   char *p;
   proxy_record tmp_record;
   time_t current_time;
   int line_num = 0;
   char *line = ctx->buffer;

   assert(record != NULL);
   memset(&tmp_record, 0, sizeof(tmp_record));

   current_time = time(NULL);
   while (fgets(line, ctx->bufsize, fd) != NULL) {
      line_num++;
      free_record(ctx, &tmp_record);
      p = strchr(line, '\n');
      if (p)
	 *p = '\0';
      ret = decode_record(ctx, basename, line, &tmp_record);
      if (ret) {
	 edg_wlpr_Log(ctx, LOG_WARNING, "Skipping invalid entry at line %d", line_num);
	 continue;
      }
      if (record->suffix >= 0) {
	 if (record->suffix == tmp_record.suffix) {
	    free_record(ctx, record);
	    *record = tmp_record;
	    return 0;
	 } else
	    continue;
      }
      if (tmp_record.suffix > last_suffix)
	 last_suffix = tmp_record.suffix;

      /* if no particular suffix was specified get the first free record 
	 available */
      if (tmp_record.jobids.len >= MAX_PROXIES || tmp_record.unique)
	 continue;

      if (tmp_record.jobids.len == 0) {
	  if (first_unused == -1)
	      first_unused = tmp_record.suffix;
	  continue;
      }

      if (record->unique)
	 continue;

      if (tmp_record.jobids.len > 0) {
	  if (record->myproxy_server &&
		  strcmp(record->myproxy_server, tmp_record.myproxy_server) != 0)
	      continue;

	  if (record->fqans == NULL || tmp_record.fqans == NULL) {
	      if (record->fqans != tmp_record.fqans)
		  continue;
	  } else
	      if (strcmp(record->fqans, tmp_record.fqans) != 0)
		  continue;
      }

      if (tmp_record.jobids.len > 0 &&
          current_time + condor_limit + RENEWAL_CLOCK_SKEW > tmp_record.end_time) {

	 /* skip expired proxy (or ones that are going to expire soon),
	    leaving it untouched (it will be removed after next run of the 
	    renewal process) */

	 continue;
      }

      free_record(ctx, record);
      *record = tmp_record;
      return 0;
   }

   if (last_used_suffix)
      *last_used_suffix = last_suffix;

   if (record->suffix >= 0) {
      edg_wlpr_Log(ctx, LOG_DEBUG, "Requested suffix %d not found in meta file",
	           record->suffix);
   } else {
       record->suffix = first_unused;
       record->next_renewal = record->end_time = 0;
   }

   free_record(ctx, &tmp_record);

   return EDG_WLPR_ERROR_PROTO_PARSE_NOT_FOUND;
}
Esempio n. 25
0
void
update_db(glite_renewal_core_context ctx, edg_wlpr_Request *request, edg_wlpr_Response *response)
{
   FILE *fd = NULL;
   int tmp_fd = -1;
   int suffix = -1;
   char tmp_file[FILENAME_MAX];
   char cur_proxy[FILENAME_MAX];
   char datafile[FILENAME_MAX];
   char *new_line = NULL;
   char *basename, *proxy = NULL;
   char **entry;
   proxy_record record;
   int ret;
   char *p;
   time_t current_time;
   char *line = ctx->buffer;

   memset(&record, 0, sizeof(record));

   edg_wlpr_Log(ctx, LOG_DEBUG, "UPDATE_DB request for %s", request->proxy_filename);

   chdir(repository);
   basename = request->proxy_filename;

   snprintf(datafile, sizeof(datafile), "%s.data", basename);
   fd = fopen(datafile, "r");
   if (fd == NULL) {
      edg_wlpr_Log(ctx, LOG_ERR, "Cannot open meta file %s (%s)",
	           datafile, strerror(errno));
      ret = errno;
      return;
   }

   snprintf(tmp_file, sizeof(tmp_file), "%s.XXXXXX", datafile);
   tmp_fd = mkstemp(tmp_file);
   if (tmp_fd < 0) {
      edg_wlpr_Log(ctx, LOG_ERR, "Cannot create temporary file (%s)",
	           strerror(errno));
      ret = errno;
      goto end;
   }

   entry = request->entries;
   if (entry) {
      p = strchr(*entry, ':');
      *p = '\0';
      suffix = atoi(*entry);
      proxy = p+1;
   }

   current_time = time(NULL);

   while (fgets(line, ctx->bufsize, fd) != NULL) {
      free_record(ctx, &record);
      p = strchr(line, '\n');
      if (p)
	 *p = '\0';
      ret = decode_record(ctx, basename, line, &record);
      if (ret)
	 goto end;
      
      if (record.suffix > suffix && entry && *entry) {
	 do {
	    entry++;
	    if (entry == NULL || *entry == NULL) {
	       suffix = -1;
	       break;
	    }
	    
	    p = strchr(*entry, ':');
	    suffix = atoi(*entry);
	    proxy = p+1;
	 } while (record.suffix > suffix);
      }

      if (record.suffix == suffix) {
	 snprintf(cur_proxy, sizeof(cur_proxy), "%s.%d", basename, suffix);
	 if (proxy == NULL || *proxy == '\0') {
	    /* if proxy isn't specified use file registered currently and
	     * reschedule renewal */
	    if (record.end_time < current_time) {
	       char *server;
	       char jobids[FILENAME_MAX];
	       /* remove file with expired proxy and clean the record in db */
	       unlink(cur_proxy);
	       server = strdup(record.myproxy_server);
	       snprintf(jobids, sizeof(jobids), "%s.%u.jobs", basename, record.suffix);
	       unlink(jobids);
	       free_record(ctx, &record);
	       record.suffix = suffix;
	       record.myproxy_server = server;
	       edg_wlpr_Log(ctx, LOG_WARNING, "Removed expired proxy %s", cur_proxy);
	    } else
	       set_renewal_times(ctx, cur_proxy, &record);
	 } else {
	    ret = set_renewal_times(ctx, proxy, &record);
	    (ret == 0) ? rename(proxy, cur_proxy) : unlink(proxy);
	 }
      }
      glite_renewal_core_reset_err(ctx);
      
      ret = encode_record(ctx, &record, &new_line);
      if (ret)
	 goto end;

      dprintf(tmp_fd, "%s\n", new_line);
      free(new_line);
      new_line = NULL;
   }
   free_record(ctx, &record);

   close(tmp_fd);
   fclose(fd);

   rename(tmp_file, datafile);

   return;

end:
   if (fd)
      fclose(fd);
   unlink(tmp_file);
   if (tmp_fd > 0)
      close(tmp_fd);
   free_record(ctx, &record);

   return;
}
Esempio n. 26
0
int
decode_record(glite_renewal_core_context ctx, const char *basename, char *line, proxy_record *record)
{
   /* line must be ended with '\0' */
   int ret;
   size_t len;

   assert(line != NULL);
   assert(record != NULL);

   memset(record, 0, sizeof(*record));

   len = strlen(line) + 1;

   ret = edg_wlpr_GetTokenInt(ctx, line, len, "suffix=", SEPARATORS, 0,
			      &record->suffix);
   if (ret)
      return ret;

#if 0
   ret = edg_wlpr_GetTokenInt(ctx, line, len, "counter=", SEPARATORS, 0, 
	                      &record->counter);
   if (ret)
      goto end;
#endif

   ret = edg_wlpr_GetTokenInt(ctx, line, len, "unique=", SEPARATORS, 0,
			      &record->unique);
   if (ret)
      goto end;

   ret = edg_wlpr_GetTokenInt(ctx, line, len, "voms_exts=", SEPARATORS, 0,
	 		      &record->voms_exts);

   edg_wlpr_GetToken(line, len, "fqans=", SEPARATORS, 0, &record->fqans);

   ret = edg_wlpr_GetToken(line, len, "server=", SEPARATORS, 0,
	 		   &record->myproxy_server);
   if (ret)
      goto end;

   ret = edg_wlpr_GetTokenInt(ctx, line, len, "next_renewal=", SEPARATORS, 0,
	 		      (int *)&record->next_renewal);
   if (ret)
      goto end;

   ret = edg_wlpr_GetTokenInt(ctx, line, len, "end_time=", SEPARATORS, 0,
	 		      (int *)&record->end_time);
   if (ret)
      goto end;

   ret = get_jobids(ctx, basename, line, len, record);
   if (ret)
      goto end;

end:
   if (ret)
      free_record(ctx, record);

   return ret;
}
Esempio n. 27
0
int
unlet_variables(const char *cmd)
{
	int error = 0;
	assert(initialized);

	while(*cmd != '\0')
	{
		envvar_t *record;

		char name[VAR_NAME_MAX + 1];
		char *p;
		int envvar = 1;

		/* check if its environment variable */
		if(*cmd != '$')
			envvar = 0;
		else
			cmd++;

		/* copy variable name */
		p = name;
		while(*cmd != '\0' && char_is_one_of(ENV_VAR_NAME_CHARS, *cmd) &&
				p - name < sizeof(name) - 1)
			*p++ = *cmd++;
		*p = '\0';

		if(*cmd != '\0' && !isspace(*cmd))
		{
			text_buffer_add("Trailing characters");
			error++;
			break;
		}

		cmd = skip_whitespace(cmd);

		/* currently we support only environment variables */
		if(!envvar)
		{
			text_buffer_addf("%s: %s", "Unsupported variable type", name);

			cmd = skip_non_whitespace(cmd);
			error++;
			continue;
		}

		/* test for empty variable name */
		if(name[0] == '\0')
		{
			text_buffer_addf("%s: %s", "Unsupported variable name", "empty name");
			error++;
			continue;
		}

		record = find_record(name);
		if(record == NULL || record->removed)
		{
			text_buffer_addf("%s: %s", "No such variable", name);
			error++;
			continue;
		}

		if(record->from_parent)
			record->removed = 1;
		else
			free_record(record);
		env_remove(name);
	}

	return error;
}
Esempio n. 28
0
void
register_proxy(glite_renewal_core_context ctx, edg_wlpr_Request *request, edg_wlpr_Response *response)
{
   proxy_record record;
   int ret;
   FILE *meta_fd = NULL;
   int last_suffix;
   char *basename = NULL;
   char filename[FILENAME_MAX];
   time_t end_time_x509, end_time_voms;

   assert(request != NULL);
   assert(response != NULL);

   memset(&record, 0, sizeof(record));
   memset(response, 0, sizeof(*response));

   if (request->proxy_filename == NULL || request->jobid == NULL) {
      edg_wlpr_Log(ctx, LOG_ERR, "Registration request doesn't contain registration information");
      return; /*  EINVAL; */
   }

   edg_wlpr_Log(ctx, LOG_DEBUG,
		"Registering proxy from %s belonging to job %s",
		request->proxy_filename, request->jobid);

   ret = get_base_filename(ctx, request->proxy_filename, &basename);
   if (ret)
      goto end;

   ret = open_metafile(ctx, basename, &meta_fd);
   if (ret)
      goto end;

   if (voms_enabled) {
       record.fqans = get_voms_fqans(ctx, request->proxy_filename);
       record.voms_exts = (record.fqans != NULL);
   }

   /* Find first free record */
   record.suffix = -1;
   record.myproxy_server = strdup(request->myproxy_server);
   ret = get_record_ext(ctx, meta_fd, basename, &record, &last_suffix);
   fclose(meta_fd); meta_fd = NULL;
   if (ret && ret != EDG_WLPR_ERROR_PROTO_PARSE_NOT_FOUND)
      goto end;

   if (ret == EDG_WLPR_ERROR_PROTO_PARSE_NOT_FOUND || record.jobids.len == 0 || request->unique) {
      /* create a new proxy file in the repository */
      int suffix;

      suffix = (record.jobids.len == 0 && record.suffix >= 0) ? 
	         record.suffix : last_suffix + 1;
      snprintf(filename, sizeof(filename), "%s.%d", basename, suffix);
      ret = copy_file(ctx, request->proxy_filename, filename);
      if (ret)
	 goto end;
      ret = set_renewal_times(ctx, filename, &record);
      if (ret)
	 goto end;
      record.suffix = suffix;
      ret = realloc_prd_list(ctx, &record.jobids);
      if (ret)
	 goto end;
      record.jobids.val[record.jobids.len - 1] = strdup(request->jobid);
      record.unique = request->unique;
      edg_wlpr_Log(ctx, LOG_DEBUG, "Created a new proxy file in repository (%s)",
	           filename);
   } else {
      ret = get_times(ctx, request->proxy_filename, &end_time_x509, &end_time_voms);
      if (ret)
	  goto end;

      snprintf(filename, sizeof(filename), "%s.%d", basename, record.suffix);
      if (record.end_time < end_time_x509 &&
	      (end_time_voms == 0 || record.next_renewal < end_time_voms)) {
	  ret = copy_file(ctx, request->proxy_filename, filename);
	  if (ret)
	      goto end;
	  edg_wlpr_Log(ctx, LOG_DEBUG, "Proxy %s of job %s has replaced the registered one.",
		       request->proxy_filename, request->jobid);
	  schedule_renewal(ctx, end_time_x509, end_time_voms, &record);
      }

      ret = realloc_prd_list(ctx, &record.jobids);
      if (ret)
	 goto end;
      record.jobids.val[record.jobids.len - 1] = strdup(request->jobid);
      edg_wlpr_Log(ctx, LOG_DEBUG, "Incremented counter on %s", filename);
   }

   ret = store_record(ctx, basename, &record);

end:
   if (meta_fd) {
      fclose(meta_fd);
   }

   if (basename)
      free(basename);

   if (ret == 0) {
      ret = filename_to_response(ctx, filename, response);
      edg_wlpr_Log(ctx, LOG_NOTICE,
                   "Proxy %s of job %s has been registered as %s",
		   request->proxy_filename, request->jobid, filename);
   } else
      edg_wlpr_Log(ctx, LOG_ERR, "Failed to register proxy %s: %s",
                   request->proxy_filename,
                   glite_renewal_core_get_err(ctx));

   record_to_response(ctx, ret, &record, response);
   free_record(ctx, &record);
}
Esempio n. 29
0
File: bls.c Progetto: AlD/bareos
/* List just block information */
static void do_blocks(char *infname)
{
   DEV_BLOCK *block = dcr->block;
   char buf1[100], buf2[100];
   for ( ;; ) {
      if (!dcr->read_block_from_device(NO_BLOCK_NUMBER_CHECK)) {
         Dmsg1(100, "!read_block(): ERR=%s\n", dev->bstrerror());
         if (dev->at_eot()) {
            if (!mount_next_read_volume(dcr)) {
               Jmsg(jcr, M_INFO, 0, _("Got EOM at file %u on device %s, Volume \"%s\"\n"),
                  dev->file, dev->print_name(), dcr->VolumeName);
               break;
            }
            /* Read and discard Volume label */
            DEV_RECORD *record;
            record = new_record();
            dcr->read_block_from_device(NO_BLOCK_NUMBER_CHECK);
            read_record_from_block(dcr, record);
            get_session_record(dev, record, &sessrec);
            free_record(record);
            Jmsg(jcr, M_INFO, 0, _("Mounted Volume \"%s\".\n"), dcr->VolumeName);
         } else if (dev->at_eof()) {
            Jmsg(jcr, M_INFO, 0, _("End of file %u on device %s, Volume \"%s\"\n"),
               dev->file, dev->print_name(), dcr->VolumeName);
            Dmsg0(20, "read_record got eof. try again\n");
            continue;
         } else if (dev->is_short_block()) {
            Jmsg(jcr, M_INFO, 0, "%s", dev->errmsg);
            continue;
         } else {
            /* I/O error */
            display_tape_error_status(jcr, dev);
            break;
         }
      }
      if (!match_bsr_block(bsr, block)) {
         Dmsg5(100, "reject Blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
            block->BlockNumber, block->block_len, block->BlockVer,
            block->VolSessionId, block->VolSessionTime);
         continue;
      }
      Dmsg5(100, "Blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
        block->BlockNumber, block->block_len, block->BlockVer,
        block->VolSessionId, block->VolSessionTime);
      if (verbose == 1) {
         read_record_from_block(dcr, rec);
         Pmsg9(-1, _("File:blk=%u:%u blk_num=%u blen=%u First rec FI=%s SessId=%u SessTim=%u Strm=%s rlen=%d\n"),
              dev->file, dev->block_num,
              block->BlockNumber, block->block_len,
              FI_to_ascii(buf1, rec->FileIndex), rec->VolSessionId, rec->VolSessionTime,
              stream_to_ascii(buf2, rec->Stream, rec->FileIndex), rec->data_len);
         rec->remainder = 0;
      } else if (verbose > 1) {
         dump_block(block, "");
      } else {
         printf(_("Block: %d size=%d\n"), block->BlockNumber, block->block_len);
      }

   }
   return;
}
Esempio n. 30
0
static int
store_record(glite_renewal_core_context ctx, char *basename, proxy_record *record)
{
   int stored = 0;
   FILE *fd = NULL;
   int temp;
   char *new_line = NULL;
   int ret, i;
   char *p;
   proxy_record tmp_record;
   char tmp_file[FILENAME_MAX];
   char meta_file[FILENAME_MAX];
   int line_num = 0;
   char *line = ctx->buffer;

   assert (record != NULL);

   memset(&tmp_record, 0, sizeof(tmp_record));

   snprintf(meta_file, sizeof(meta_file), "%s.data", basename);
   snprintf(tmp_file, sizeof(tmp_file), "%s.XXXXXX", meta_file);

   temp = mkstemp(tmp_file);
   if (temp < 0)
      return errno;

   fd = fopen(meta_file, "r");
   if (fd == NULL) {
      ret = errno;
      goto end;
   }
   while (fgets(line, ctx->bufsize, fd) != NULL) {
      line_num++;
      free_record(ctx, &tmp_record);
      p = strchr(line, '\n');
      if (p)
	 *p = '\0';
      ret = decode_record(ctx, basename, line, &tmp_record);
      if (ret) {
	 edg_wlpr_Log(ctx, LOG_WARNING, "Removing invalid entry at line %d in %s", line_num, basename);
	 continue;
      }
      if (record->suffix == tmp_record.suffix &&
	  record->unique == tmp_record.unique) {
	 free_record(ctx, &tmp_record);
	 tmp_record = *record;
	 tmp_record.myproxy_server = strdup(record->myproxy_server);
	 tmp_record.jobids.len = 0;
	 tmp_record.jobids.val = NULL;
	 for (i = 0; i < record->jobids.len; i++) {
	    realloc_prd_list(ctx, &tmp_record.jobids);
	    tmp_record.jobids.val[tmp_record.jobids.len - 1] = 
	       strdup(record->jobids.val[i]);
	 }
	 if (record->fqans)
	     tmp_record.fqans = strdup(record->fqans);
	 stored = 1;
      }
      ret = encode_record(ctx, &tmp_record, &new_line);
      if (ret)
	 goto end;
      dprintf(temp, "%s\n", new_line);
      save_jobids(ctx, basename, &tmp_record);
      free(new_line);
      new_line = NULL;
   }
   if (! stored) {
      ret = encode_record(ctx, record, &new_line);
      if (ret)
	 goto end;
      ret = dprintf(temp, "%s\n", new_line);
      save_jobids(ctx, basename, record);
      free(new_line);
      new_line = NULL;
   }
   fclose(fd); fd = NULL;
   close(temp);

   ret = rename(tmp_file, meta_file);
   if (ret)
      ret = errno;

end:
   free_record(ctx, &tmp_record);
   if (fd)
      fclose(fd);
   close(temp);
   return ret;
}