gint wr_equal_term(glb* g, gint x, gint y, int uniquestrflag) { gptr db; gint encx,ency; gptr xptr,yptr; int xlen,ylen,uselen,i,ilimit; gint eqencx; // used by the WR_EQUAL_TERM macro #ifdef DEBUG printf("wr_equal_term called with x %d and y %d\n",x,y); #endif // first check if immediately same: return 1 if yes if (x==y) return 1; // handle immediate check cases: for these bit suffixes x is equal to y iff x==y encx=(x&NORMALPTRMASK); if ((encx==LONGSTRBITS && uniquestrflag) || encx==SMALLINTBITS || encx==NORMALPTRMASK) return 0; // immediate value: must be unequal since x==y checked before if (!isptr(x) || !isptr(y)) return 0; // here both x and y are ptr types // quick type check: last two bits if (((x)&NONPTRBITS)!=((y)&NONPTRBITS)) return 0; // if one is datarec, the other must be as well if (!isdatarec(x)) { if (isdatarec(y)) return 0; // neither x nor y are datarecs // need to check pointed values if (wr_equal_ptr_primitives(g,x,y,uniquestrflag)) return 1; else return 0; } else { if (!isdatarec(y)) return 0; // both x and y are datarecs db=g->db; xptr=decode_record(db,x); yptr=decode_record(db,y); xlen=get_record_len(xptr); ylen=get_record_len(yptr); if (g->unify_samelen) { if (xlen!=ylen) return 0; uselen=xlen; } else { if (xlen<=ylen) uselen=xlen; else uselen=ylen; } if (g->unify_maxuseterms) { if (((g->unify_maxuseterms)+(g->unify_firstuseterm))<uselen) uselen=(g->unify_firstuseterm)+(g->unify_maxuseterms); } ilimit=RECORD_HEADER_GINTS+uselen; for(i=RECORD_HEADER_GINTS+(g->unify_firstuseterm); i<ilimit; i++) { encx=*(xptr+i); ency=*(yptr+i); if (!WR_EQUAL_TERM(g,encx,ency,uniquestrflag)) return 0; } return 1; } }
static gint wr_occurs_in(glb* g, gint x, gint y, gptr vb) { void* db=g->db; gptr yptr; gint yi; int ylen,ilimit,i; gint tmp; // used by VARVAL_F #ifdef DEBUG printf("wr_occurs_in called with x %d ",x); wr_print_term(g,x); printf(" and y %d ",y); wr_print_term(g,y); printf("\n"); #endif yptr=decode_record(db,y); ylen=get_record_len(yptr); if (g->unify_maxuseterms) { if (((g->unify_maxuseterms)+(g->unify_firstuseterm))<ylen) ylen=(g->unify_firstuseterm)+(g->unify_maxuseterms); } ilimit=RECORD_HEADER_GINTS+ylen; for(i=RECORD_HEADER_GINTS+(g->unify_firstuseterm); i<ilimit; i++) { yi=*(yptr+i); if (isvar(yi)) yi=VARVAL_F(yi,vb); if (x==yi) return 1; if (isdatarec(yi)) { if (wr_occurs_in(g,x,yi,vb)) return 1; } } // passed vector ok, not finding x inside return 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; }
gint wr_equal_term_macroaux(glb* g, gint x, gint y, int uniquestrflag) { gptr db; gint encx,ency; gptr xptr,yptr; int xlen,ylen,uselen,i,ilimit; gint eqencx; // used by WR_EQUAL_TERM macro #ifdef DEBUG printf("wr_equal_term_macroaux called with x %d and y %d\n",x,y); #endif if (!isdatarec(x)) { if (isdatarec(y)) return 0; // neither x nor y are datarecs // need to check pointed values if (wr_equal_ptr_primitives(g,x,y,uniquestrflag)) return 1; else return 0; } else { // both x and y are datarecs db=g->db; xptr=decode_record(db,x); yptr=decode_record(db,y); xlen=get_record_len(xptr); ylen=get_record_len(yptr); if (g->unify_samelen) { if (xlen!=ylen) return 0; uselen=xlen; } else { if (xlen<=ylen) uselen=xlen; else uselen=ylen; } if (g->unify_maxuseterms) { if (((g->unify_maxuseterms)+(g->unify_firstuseterm))<uselen) uselen=(g->unify_firstuseterm)+(g->unify_maxuseterms); } ilimit=RECORD_HEADER_GINTS+uselen; for(i=RECORD_HEADER_GINTS+(g->unify_firstuseterm); i<ilimit; i++) { encx=*(xptr+i); ency=*(yptr+i); if (!WR_EQUAL_TERM(g,encx,ency,uniquestrflag)) return 0; } return 1; } }
void xheader_decode_global (struct xheader *xhdr) { if (xhdr->size) { char *p = xhdr->buffer + BLOCKSIZE; xheader_list_destroy (&global_header_override_list); while (decode_record (xhdr, &p, decg, &global_header_override_list)) continue; } }
void xheader_decode (struct tar_stat_info *st) { run_override_list (keyword_global_override_list, st); run_override_list (global_header_override_list, st); if (st->xhdr.size) { char *p = st->xhdr.buffer + BLOCKSIZE; while (decode_record (&st->xhdr, &p, decx, st)) continue; } run_override_list (keyword_override_list, st); }
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; }
int vrpn_Tracker_Dyna::get_report(void) { int ret; if (status == vrpn_TRACKER_SYNCING) { if ((ret=vrpn_read_available_characters(serial_fd, buffer, 1)) != 1 || (buffer[0] & llll_OOOO) != lOOO_OOOO) { return 0; } vrpn_gettimeofday(×tamp, NULL); status = vrpn_TRACKER_PARTIAL; bufcount= ret; } if (status == vrpn_TRACKER_PARTIAL) { ret=vrpn_read_available_characters(serial_fd, &(buffer[bufcount]), reportLength-bufcount); if (ret < 0) { fprintf(stderr,"%s@%d: Error reading\n", __FILE__, __LINE__); status = vrpn_TRACKER_FAIL; return 0; } bufcount += ret; if (bufcount < reportLength) { // Not done -- go back for more return 0; } } if (!valid_report()) { //fprintf(stderr,"no valid report"); bufcount = 0; status = vrpn_TRACKER_SYNCING; return 0; } //else fprintf(stderr,"got valid report"); decode_record(); status = vrpn_TRACKER_SYNCING; bufcount=0; return 1; }
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); }
void xheader_decode (struct tar_stat_info *st) { run_override_list (keyword_global_override_list, st); run_override_list (global_header_override_list, st); if (st->xhdr.size) { char *p = st->xhdr.buffer + BLOCKSIZE; while (decode_record (&st->xhdr, &p, decx, st)) continue; } run_override_list (keyword_override_list, st); /* The archived (effective) file size is always set directly in tar header field, possibly overridden by "size" extended header - in both cases, result is now decoded in st->stat.st_size */ st->archive_file_size = st->stat.st_size; /* The real file size (given by stat()) may be redefined for sparse files in "GNU.sparse.realsize" extended header */ if (st->real_size_set) st->stat.st_size = st->real_size; }
uint32_t optimizeDataFile(HTree* tree, int bucket, const char* path, const char* hintpath, int limit, uint32_t max_data_size, int last_bucket, const char *lastdata, const char *lasthint) { MFile *f = open_mfile(path); if (f == NULL) return -1; FILE *new_df = NULL; char tmp[255], *hintdata = NULL; uint32_t hint_used=0, hint_size = 0, old_data_size=0; if (lastdata != NULL) { new_df = fopen(lastdata, "ab"); old_data_size = ftello(new_df); if (old_data_size > 0) { HintFile *hint = open_hint(lasthint, NULL); if (hint == NULL) { fprintf(stderr, "open last hint file %s failed\n", lasthint); close_mfile(f); return 0; } hint_size = hint->size * 2; if (hint_size < 4096) hint_size = 4096; hintdata = malloc(hint_size); memcpy(hintdata, hint->buf, hint->size); hint_used = hint->size; close_hint(hint); } else { hint_size = 4096; hintdata = malloc(hint_size); hint_used = 0; } } else { sprintf(tmp, "%s.tmp", path); new_df = fopen(tmp, "wb"); hintdata = malloc(1<<20); hint_size = 1<<20; } if (new_df == NULL){ fprintf(stderr, "open new datafile failed\n"); close_mfile(f); return -1; } HTree *cur_tree = ht_new(0,0); int deleted = 0, broken = 0; char *p = f->addr, *end = f->addr + f->size; while (p < end) { DataRecord *r = decode_record(p, end-p, false); if (r == NULL) { broken ++; if (broken > 40960) { // 10M // TODO: delete broken keys from htree fprintf(stderr, "unexpected broken data in %s at %ld\n", path, p - f->addr - broken * PADDING); break; } p += PADDING; continue; } Item *it = ht_get2(tree, r->key, r->ksz); uint32_t pos = p - f->addr; if (it && it->pos == (pos | bucket) && (it->ver > 0 || limit > 0)) { uint32_t new_pos = ftello(new_df); if (new_pos + record_length(r) > max_data_size) { fprintf(stderr, "optimize %s into %s failed\n", path, lastdata); free(hintdata); ht_destroy(cur_tree); close_mfile(f); ftruncate(new_df, old_data_size); fclose(new_df); return 0; // overflow } uint16_t hash = it->hash; ht_add2(cur_tree, r->key, r->ksz, new_pos | last_bucket, hash, it->ver); // append record to hint file int hsize = sizeof(HintRecord) - NAME_IN_RECORD + r->ksz + 1; if (hint_used + hsize > hint_size) { hint_size *= 2; hintdata = realloc(hintdata, hint_size); } HintRecord *hr = (HintRecord*)(hintdata + hint_used); hr->ksize = r->ksz; hr->pos = new_pos >> 8; hr->version = it->ver; hr->hash = hash; memcpy(hr->key, r->key, r->ksz + 1); hint_used += hsize; if (write_record(new_df, r) != 0) { fprintf(stderr, "write error: %s\n", path); free(hintdata); ht_destroy(cur_tree); close_mfile(f); fclose(new_df); return -1; } }else{ if (it && it->pos == (pos | bucket) && it->ver < 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; }
/* 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; }
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; }
gint wr_match_term_aux(glb* g, gint x, gint y, int uniquestrflag) { gptr db; gint xval,encx,ency; gptr xptr,yptr; int xlen,ylen,uselen,ilimit,i; gint eqencx; // used by WR_EQUAL_TERM macro #ifdef DEBUG printf("wr_match_term_aux called with x %d ",x); wr_print_term(g,x); printf(" and y %d ",y); wr_print_term(g,y); printf("\n"); #endif // check x var case immediately if (isvar(x)) { xval=VARVAL_DIRECT(x,(g->varbanks)); if (xval==UNASSIGNED) { // previously unassigned var: assign now and return SETVAR(x,y,g->varbanks,g->varstack,g->tmp_unify_vc); return 1; } else { // xval must now be equal to y, else match fails if (WR_EQUAL_TERM(g,xval,y,uniquestrflag)) return 1; return 0; } } // now x is not var if (!isdatarec(x)) { if (WR_EQUAL_TERM(g,x,y,uniquestrflag)) return 1; } if (!isdatarec(y)) return 0; // x is datarec but y is not // now x and y are different datarecs if (1) { db=g->db; xptr=decode_record(db,x); yptr=decode_record(db,y); xlen=get_record_len(xptr); ylen=get_record_len(yptr); if (g->unify_samelen) { if (xlen!=ylen) return 0; uselen=xlen; } else { if (xlen<=ylen) uselen=xlen; else uselen=ylen; } if (g->unify_maxuseterms) { if (((g->unify_maxuseterms)+(g->unify_firstuseterm))<uselen) uselen=(g->unify_firstuseterm)+(g->unify_maxuseterms); } ilimit=RECORD_HEADER_GINTS+uselen; for(i=RECORD_HEADER_GINTS+(g->unify_firstuseterm); i<ilimit; i++) { encx=*(xptr+i); ency=*(yptr+i); if (!wr_match_term_aux(g,encx,ency,uniquestrflag)) return 0; } return 1; } }
gint wr_unify_term_aux(glb* g, gint x, gint y, int uniquestrflag) { gptr db; gint encx,ency; gint tmp; // used by VARVAL_F macro gptr xptr,yptr; int xlen,ylen,uselen,ilimit,i; #ifdef DEBUG printf("wr_unify_term_aux called with x %d ",x); wr_print_term(g,x); printf(" and y %d ",y); wr_print_term(g,y); printf("\n"); #endif // first check if immediately same: return 1 if yes if (x==y) return 1; // second, fetch var values for var args if (isvar(x)) x=VARVAL_F(x,(g->varbanks)); if (isvar(y)) y=VARVAL_F(y,(g->varbanks)); // check again if same if (x==y) return 1; // go through the ladder of possibilities // knowing that x and y are different if (!isdatarec(x)) { // x is a primitive if (!isdatarec(y)) { // both x and y are primitive if (isvar(x)) { SETVAR(x,y,g->varbanks,g->varstack,g->tmp_unify_vc); // set occcheck only if y is a var too if (g->tmp_unify_do_occcheck && isvar(y)) (g->tmp_unify_occcheck)=1; return 1; } else if (isvar(y)) { SETVAR(y,x,g->varbanks,g->varstack,g->tmp_unify_vc); // do not set occcheck here: x is a constant! return 1; } else { // x and y are constants if (wr_equal_ptr_primitives(g,x,y,uniquestrflag)) return 1; else return 0; } // x is primitive, but y is not } else if (isvar(x)) { // x is var, y is non-primitive if (g->tmp_unify_occcheck && wr_occurs_in(g,x,y,(gptr)(g->varbanks))) { return 0; } else { SETVAR(x,y,g->varbanks,g->varstack,g->tmp_unify_vc); if (g->tmp_unify_do_occcheck) (g->tmp_unify_occcheck)=1; return 1; } } else { // x is a constant, but y is non-primitive return 0; } // x is not primitive } else if (isvar(y)) { // x is non-primitive, y is var if (g->tmp_unify_occcheck && wr_occurs_in(g,y,x,(gptr)(g->varbanks))) { return 0; } else { SETVAR(y,x,g->varbanks,g->varstack,g->tmp_unify_vc); if (g->tmp_unify_do_occcheck) (g->tmp_unify_occcheck)=1; return 1; } // x is not primitive, y is non-var } else if (!isdatarec(y)) { // x is not primitive, y is constant return 0; } else { // x and y are both complex terms db=g->db; xptr=decode_record(db,x); yptr=decode_record(db,y); xlen=get_record_len(xptr); ylen=get_record_len(yptr); if (g->unify_samelen) { if (xlen!=ylen) return 0; uselen=xlen; } else { if (xlen<=ylen) uselen=xlen; else uselen=ylen; } if (g->unify_maxuseterms) { if (((g->unify_maxuseterms)+(g->unify_firstuseterm))<uselen) uselen=(g->unify_firstuseterm)+(g->unify_maxuseterms); } ilimit=RECORD_HEADER_GINTS+uselen; for(i=RECORD_HEADER_GINTS+(g->unify_firstuseterm); i<ilimit; i++) { encx=*(xptr+i); ency=*(yptr+i); if (encx!=ency && !wr_unify_term_aux(g,encx,ency,uniquestrflag)) return 0; } return 1; } }
static void check_renewal(glite_renewal_core_context ctx, char *datafile, int force_renew, int *num_renewed) { char line[1024]; proxy_record record; char *p; int ret, i; time_t current_time; FILE *meta_fd = NULL; char basename[FILENAME_MAX]; edg_wlpr_Request request; edg_wlpr_Response response; char *new_proxy = NULL; char *entry = NULL; char **tmp; int num = 0; assert(datafile != NULL); *num_renewed = 0; memset(&record, 0, sizeof(record)); memset(basename, 0, sizeof(basename)); memset(&request, 0, sizeof(request)); memset(&response, 0, sizeof(response)); strncpy(basename, datafile, sizeof(basename) - 1); p = basename + strlen(basename) - strlen(".data"); if (strcmp(p, ".data") != 0) { glite_renewal_log(ctx, LOG_ERR, "Meta filename doesn't end with '.data'"); return; } *p = '\0'; request.command = EDG_WLPR_COMMAND_UPDATE_DB; request.proxy_filename = strdup(basename); meta_fd = fopen(datafile, "r"); if (meta_fd == NULL) { glite_renewal_log(ctx, LOG_ERR, "Cannot open meta file %s (%s)", datafile, strerror(errno)); return; } current_time = time(NULL); glite_renewal_log(ctx, LOG_DEBUG, "Reading metafile %s", datafile); while (fgets(line, sizeof(line), meta_fd) != NULL) { free_record(ctx, &record); p = strchr(line, '\n'); if (p) *p = '\0'; ret = decode_record(ctx, line, &record); if (ret) continue; /* XXX exit? */ if (record.jobids.len == 0) /* no jobid registered for this proxy */ continue; if (current_time + RENEWAL_CLOCK_SKEW >= record.end_time || record.next_renewal <= current_time || force_renew) { ret = EDG_WLPR_PROXY_EXPIRED; if ( record.end_time + RENEWAL_CLOCK_SKEW >= current_time) { /* only try renewal if the proxy hasn't already expired */ ret = renew_proxy(ctx, &record, basename, &new_proxy); } /* if the proxy wasn't renewed have the daemon planned another renewal */ asprintf(&entry, "%d:%s", record.suffix, (ret == 0) ? new_proxy : ""); if (new_proxy) { free(new_proxy); new_proxy = NULL; } tmp = realloc(request.entries, (num + 2) * sizeof(*tmp)); if (tmp == NULL) { free_record(ctx, &record); return; } request.entries = tmp; request.entries[num] = entry; request.entries[num+1] = NULL; num++; } } free_record(ctx, &record); if (num > 0) { ret = edg_wlpr_RequestSend(&request, &response); if (ret != 0) glite_renewal_log(ctx, LOG_ERR, "Failed to send update request to master (%d)", ret); else if (response.response_code != 0) glite_renewal_log(ctx, LOG_ERR, "Master failed to update database (%d)", response.response_code); /* delete all tmp proxy files which may survive */ for (i = 0; i < num; i++) { p = strchr(request.entries[i], ':'); if (p+1) unlink(p+1); } } fclose(meta_fd); edg_wlpr_CleanResponse(&response); edg_wlpr_CleanRequest(&request); *num_renewed = num; return; }