/* token thread start */ void token_thread_init(){ token_thread = (LIBEVENT_TOKEN_THREAD *)calloc(1, sizeof(LIBEVENT_TOKEN_THREAD)); token_thread->base = event_base_new(); if (! token_thread->base) { d_log("error Can't allocate event base"); exit(1); } token_efd = eventfd(0, 0); if(token_efd == -1){ DEBUG("token_efd error"); exit(1); } /* Listen for notifications from other threads */ event_set(&token_thread->notify_event, token_efd, EV_READ | EV_PERSIST, libevent_token_thread, token_thread); event_base_set(token_thread->base, &token_thread->notify_event); if (event_add(&token_thread->notify_event, 0) == -1) { d_log("Can't monitor libevent notify pipe"); exit(1); } setup_token(main_token_thread, token_thread); }
/* * Modified : 01.16.2002 Author : Dark0n3 * * Description : Deletes all -missing files with preparsed sfv. */ void delete_sfv(const char *path, struct VARS *raceI) { char *f = 0, missing_fname[NAME_MAX]; FILE *sfvfile; SFVDATA sd; if (!(sfvfile = fopen(path, "r"))) { d_log("delete_sfv: Couldn't fopen %s: %s\n", path, strerror(errno)); remove_lock(raceI); exit(EXIT_FAILURE); } while (fread(&sd, sizeof(SFVDATA), 1, sfvfile)) { snprintf(missing_fname, NAME_MAX, "%s-missing", sd.fname); if ((f = findfilename(missing_fname, f, raceI))) { if (unlink(missing_fname) < 0) d_log("delete_sfv: Couldn't unlink missing-indicator '%s': %s\n", missing_fname, strerror(errno)); } } ng_free(f); fclose(sfvfile); }
/* * Modified : 01.16.2002 Author : Dark0n3 * * Description : Reads name of old race leader and writes name of new leader * into temporary file. * */ void read_write_leader(const char *path, struct VARS *raceI, struct USERINFO *userI) { int fd; struct stat sb; if ((fd = open(path, O_CREAT | O_RDWR, 0666)) == -1) { d_log("read_write_leader: open(%s): %s\n", path, strerror(errno)); return; } if (!update_lock(raceI, 1, 0)) { d_log("read_write_leader: Lock is suggested removed. Will comply and exit\n"); remove_lock(raceI); exit(EXIT_FAILURE); } fstat(fd, &sb); if (sb.st_size == 0) { *raceI->misc.old_leader = '\0'; } else { if (read(fd, &raceI->misc.old_leader, 24) == -1) { d_log("read_write_leader: read() failed: %s\n", strerror(errno)); } lseek(fd, 0L, SEEK_SET); } if (write(fd, userI->name, 24) != 24) d_log("read_write_leader: write failed: %s\n", strerror(errno)); close(fd); }
void f_merge_compression(struct o_stream *merge_A, struct o_stream *merge_B, struct o_string *output) { struct s_exception *exception = NULL; struct s_singleton_file_header merge_A_header, merge_B_header; struct o_stream *output_stream; d_try { output_stream = f_stream_new_file(NULL, output, "wb", 0777); if ((merge_A->m_read_raw(merge_A, (unsigned char *)&(merge_A_header), sizeof(struct s_singleton_file_header))) && (merge_B->m_read_raw(merge_B, (unsigned char *)&(merge_B_header), sizeof(struct s_singleton_file_header)))) { if ((merge_A_header.endian_check == (unsigned short)d_compress_endian) && (merge_B_header.endian_check == (unsigned short)d_compress_endian)) { if ((merge_A_header.high_treshold == merge_B_header.high_treshold) && (merge_A_header.low_treshold == merge_B_header.low_treshold)) { output_stream->m_write(output_stream, sizeof(struct s_singleton_file_header), &merge_A_header); p_merge_compression(merge_A, merge_B, output_stream); } else d_log(e_log_level_ever, "Different threshold (-T,-t)"); } else d_log(e_log_level_ever, "Wrong file format. Maybe this isn't a compressed data file ...\n"); } d_release(output_stream); } d_catch(exception) { d_exception_dump(stderr, exception); d_raise; } d_endtry; }
int filebanned_match(const char *filename) { int fd; char buf[500]; FILE *fname_fd; char fbuf[strlen(filename)+1]; bzero(fbuf, sizeof(fbuf)); strncpy(fbuf, filename, sizeof(fbuf) - 1); if ((fd = open(banned_filelist, O_RDONLY)) == -1) { d_log("filebanned_match: failed to open banned_filelist - open(%s): %s\n", banned_filelist, strerror(errno)); return 0; } if ((fname_fd = fdopen(fd, "r")) == NULL) { d_log("filebanned_match: failed to open banned_filelist - fdopen(%s): %s\n", banned_filelist, strerror(errno)); return 0; } strtolower(fbuf); while ((fgets(buf, sizeof(buf), fname_fd))) { buf[strlen(buf) - 1] = '\0'; if ( *buf == '\0' || *buf == ' ' || *buf == '\t' || *buf == '#' ) continue; strtolower(buf); if (!fnmatch(buf, fbuf, 0)) { close(fd); d_log("filebanned_match: found match: %s\n", fbuf); return 1; } } return 0; }
void update_sfvdata(const char *path, const char *fname, const unsigned int crc) { int fd, count; SFVDATA sd; if ((fd = open(path, O_RDWR, 0666)) == -1) { d_log("update_sfvdata: Failed to open sfvdata (%s): %s\n", path, strerror(errno)); return; } sd.crc32 = crc; count = 0; while (read(fd, &sd, sizeof(SFVDATA))) { if (!strcasecmp(fname, sd.fname)) { sd.crc32 = crc; break; } count++; } lseek(fd, sizeof(SFVDATA) * count, SEEK_SET); if (write(fd, &sd, sizeof(SFVDATA)) != sizeof(SFVDATA)) d_log("update_sfvdata: write failed: %s\n", strerror(errno)); close(fd); }
void NullTexture::_load(DataStreamPtr stream) { if (mLoadState != Resource::LOADING) return ; if (stream == NULL) { d_log("!: Load texture '%s' failed.", mSourceName.c_str()); return ; } d_assert (mPixelData == NULL && mLockFlag == 0); Image image; if (IMG_Load(image, stream)) { mPixelData = image.pixels; mWidth = image.width; mHeight = image.height; mFormat = image.format; mMipmaps = 1; } else { d_log ("?: image '%s' load failed.", mSourceName.c_str()); } }
/* * Modified : 2002.01.16 Author : Dark0n3 * Modified : 2011.08.10 by : Sked * Description : Reads crc for current file from preparsed sfv file. */ unsigned int readsfv(const char *path, struct VARS *raceI, int getfcount) { unsigned int crc = 0; FILE *sfvfile; DIR *dir; SFVDATA sd; if (!(sfvfile = fopen(path, "r"))) { d_log("readsfv: Failed to open sfv (%s): %s\n", path, strerror(errno)); return 0; } if (!update_lock(raceI, 1, 0)) { d_log("readsfv: Lock is suggested removed. Will comply and exit\n"); remove_lock(raceI); exit(EXIT_FAILURE); } raceI->misc.release_type = raceI->data_type; d_log("readsfv: Reading data from sfv for (%s)\n", raceI->file.name); dir = opendir("."); raceI->total.files = 0; while (fread(&sd, sizeof(SFVDATA), 1, sfvfile)) { raceI->total.files++; if (lenient_compare(raceI->file.name, sd.fname)) { d_log("readsfv: crc read from sfv-file (%s): %.8x\n", sd.fname, (unsigned int)sd.crc32); crc = (unsigned int)sd.crc32; strncpy(raceI->file.unlink, sd.fname, sizeof(raceI->file.unlink)); } if (getfcount && findfile(dir, sd.fname)) raceI->total.files_missing--; } closedir(dir); fclose(sfvfile); raceI->total.files_missing += raceI->total.files; if (raceI->total.files_missing < 0) { d_log("readsfv: GAKK! raceI->total.files_missing %d < 0\n", raceI->total.files_missing); raceI->total.files_missing = 0; } return crc; }
/* create a comma separated list of dirnames - used for affils * needs a bit of work yet... */ void create_dirlist(const char *dirnames, char *affillist, const int limit) { DIR *dir; struct dirent *dp; char *p = 0, *s = 0, *t = 0; int n = 0, m = 0, q = strlen(dirnames); char *dlist = NULL; dlist = ng_realloc2(dlist, q+4, 1, 1, 1); // dlist = malloc(q+4); // bzero(dlist, q+4); s = dlist; t = dlist; memcpy(dlist, dirnames, q); while (m <= q) { if (*s == '\0' || *s == ' ') { *s = '\0'; if (!strlen(t) || !(dir = opendir(t))) { ng_free(dlist); return; } rewinddir(dir); while ((dp = readdir(dir))) { if (!strncmp(dp->d_name, ".", 1)) continue; n = strlen(affillist); if ((unsigned)(n + strlen(dp->d_name)) < (unsigned)limit) { p = affillist + n; if (n) { *p = ','; p++; } memcpy(p, dp->d_name, strlen(dp->d_name)); } else { d_log("create_dirlist: Too many dirs - unable to add more.\n"); d_log("create_dirlist: List so far = '%s'\n", affillist); ng_free(dlist); return; } } closedir(dir); d_log("create_dirlist: List so far = '%s'\n", affillist); s++; t = s; } s++; m++; } ng_free(dlist); return; }
void f_memory_destroy(void) { struct s_memory_tail *tail; struct s_memory_head *head; unsigned int elements = 0; while (v_memory_root) { head = v_memory_root; tail = (struct s_memory_tail *)((void *)v_memory_root + sizeof(struct s_memory_head) + head->dimension); d_log(e_log_level_high, "pointer %p (%hu bytes) is still here (allocated in %s::%d) [0x%x-0x%x]", ((void *)head + sizeof(struct s_memory_head)), head->dimension, tail->file, tail->line, head->checksum, tail->checksum); v_memory_root = head->next; ++elements; free(head); } if (elements > 0) d_log(e_log_level_medium, "%d pointer(s) %s still here", elements, ((elements == 1) ? "was" : "were")); }
void save_pid(const pid_t pid, const char *pid_file) { FILE *fp; if (pid_file == NULL) return; if ((fp = fopen(pid_file, "w")) == NULL) { d_log("Could not open the pid file for writing"); return; } fprintf(fp,"%ld\n", (long)pid); if (fclose(fp) == -1) { d_log("Could not close the pid file "); return; } }
void f_check_compression(struct o_string *data) { struct o_stream *stream; struct s_singleton_file_header file_header; struct s_singleton_event_header event_header; struct s_singleton_cluster_details *clusters; struct s_exception *exception = NULL; int events = 0; d_try { stream = f_stream_new_file(NULL, data, "rb", 0777); if ((stream->m_read_raw(stream, (unsigned char *)&(file_header), sizeof(struct s_singleton_file_header)))) { if (file_header.endian_check == (unsigned short)d_compress_endian) { fprintf(stdout, "[HIGH: %02f | LOW: %02f]\n", file_header.high_treshold, file_header.low_treshold); while ((clusters = f_decompress_event(stream, &event_header))) { fprintf(stdout, "\r[readed events: %d (clusters: %d)]", events++, event_header.clusters); fflush(stdout); d_free(clusters); } fprintf(stdout, "\n[OK]\n"); } else d_log(e_log_level_ever, "Wrong file format. Maybe this isn't a compressed data file ...\n"); } d_release(stream); } d_catch(exception) { d_exception_dump(stderr, exception); d_raise; } d_endtry; }
int main (int argc, char *argv[]) { struct o_string *calibration = NULL, *data = NULL, *output = NULL, *output_cn = NULL, *merge_A = NULL, *merge_B = NULL; struct o_stream *stream, *stream_merge_A, *stream_merge_B; struct s_exception *exception = NULL; int arguments = 0, flags[d_trb_event_channels], backup; float high_treshold = 8.0f, low_treshold = 3.0f, pedestal[d_trb_event_channels], sigma_raw[d_trb_event_channels], sigma[d_trb_event_channels], gain[d_trb_event_channels]; d_try { d_compress_argument(arguments, "-mA", merge_A, d_string_pure, "No merge file A specified (-mA)"); d_compress_argument(arguments, "-mB", merge_B, d_string_pure, "No merge file B specified (-mB)"); d_compress_argument(arguments, "-tA", v_cut_tA, atoi, "No cut for file A specified (-tA)"); d_compress_argument(arguments, "-TA", v_cut_TA, atoi, "No cut for file A specified (-TA)"); d_compress_argument(arguments, "-tB", v_cut_tB, atoi, "No cut for file B specified (-tB)"); d_compress_argument(arguments, "-TB", v_cut_TB, atoi, "No cut for file B specified (-TB)"); d_compress_argument(arguments, "-c", calibration, d_string_pure, "No calibration file specified (-c)"); d_compress_argument(arguments, "-d", data, d_string_pure, "No data file specified (-d)"); d_compress_argument(arguments, "-o", output, d_string_pure, "No output file specified (-o)"); d_compress_argument(arguments, "-T", high_treshold, atof, "No high treshold specified: using default one (8.0)"); d_compress_argument(arguments, "-t", low_treshold, atof, "No low treshold specified: using default one (3.0)"); d_compress_argument(arguments, "-max-cn", max_common_noise, atof, "No maximum CN specified (-max-cn)"); d_compress_argument(arguments, "-max-strips", max_strips, atoi, "No maximum number of strips per cluster specified (-max-strips)"); d_compress_argument(arguments, "-min-strips", min_strips, atoi, "No minimum number of strips per cluster specified (-min-strips)"); d_compress_argument(arguments, "-min-sn", min_signal_over_noise, atof, "No minimum signal over noise value specified (-min-sn)"); d_compress_argument(arguments, "-r", min_strip, atoi, "No range (lower strip) specified: using default one (0) (-r)"); d_compress_argument(arguments, "-R", max_strip, atoi, "No range (upper strip) specified: using default one (384) (-R)"); d_compress_argument(arguments, "-ocn", output_cn, d_string_pure, "No CNs output file (-ocn)"); if (min_strip > d_trb_event_channels) min_strip = 0; if (max_strip > d_trb_event_channels) max_strip = d_trb_event_channels; if (max_strip < min_strip) { backup = min_strip; min_strip = max_strip; max_strip = backup; } if ((merge_A) && (merge_B) && (output)) { stream_merge_A = f_stream_new_file(NULL, merge_A, "r", 0777); stream_merge_B = f_stream_new_file(NULL, merge_B, "r", 0777); f_merge_compression(stream_merge_A, stream_merge_B, output); d_release(stream_merge_B); d_release(stream_merge_A); } else if ((calibration) && (data) && (output)) { stream = f_stream_new_file(NULL, calibration, "r", 0777); f_read_calibration(stream, pedestal, sigma_raw, sigma, flags, gain, NULL); f_compress_data(data, output, output_cn, high_treshold, low_treshold, 10.0, pedestal, sigma, flags); f_check_compression(output); d_release(stream); } else d_log(e_log_level_ever, "Missing arguments", NULL); if (output_cn) d_release(output_cn); d_release(calibration); d_release(data); d_release(output); } d_catch(exception) { d_exception_dump(stderr, exception); } d_endtry; return 0; }
void setup_thread(LIBEVENT_WORK_THREAD *me) { me->base = event_init(); if (! me->base) { d_log("error Can't allocate event base"); exit(1); } /* Listen for notifications from other threads */ event_set(&me->notify_event, me->notify_read_fd, EV_READ | EV_PERSIST, libevent_work_thread, me); event_base_set(me->base, &me->notify_event); if (event_add(&me->notify_event, 0) == -1) { d_log("Can't monitor libevent notify pipe"); exit(1); } }
void remove_pidfile(const char *pid_file) { if (pid_file == NULL) return; if (unlink(pid_file) != 0) { d_log("Could not remove the pid file "); } }
d_define_method(emitter, raise)(struct s_object *self, const char *id) { struct s_signal *signal; void *result = NULL; if ((signal = d_call(self, m_emitter_get, id))) { d_log(e_log_level_high, "signal '%s' has been raised from %s (%s::%d)", id, self->type, self->file, self->line); if (signal->action) result = signal->action(self, signal->parameters, signal->parameters_size); } return result; }
int read_headdata(struct VARS *raceI) { int fd = 0; HEADDATA hd; int type = 0; if ((fd = open(raceI->headpath, O_RDONLY)) == -1) { d_log("read_headdata: failed to open(%s): %s - returning '0' as data_type\n", raceI->headpath, strerror(errno)); return 0; } if ((read(fd, &hd, sizeof(HEADDATA))) != sizeof(HEADDATA)) { d_log("read_headdata: failed to read %s : %s - returning '0' as data_type\n", raceI->headpath, strerror(errno)); return 0; } type = hd.data_type; close(fd); return type; }
void audioSortDir(char *targetDir) { int cnt = 0; char link_source[PATH_MAX], link_target[PATH_MAX], *file_target = NULL; struct audio info; DIR *ourDir; if (*targetDir != '/') { d_log("audioSortDir: not an absolute path. (%s)\n", targetDir); return; } /* Look at something like that to verify that a release is complete? * (And tell user to run rescan if not) * char *sfv_data = NULL; * * sfv_data = ng_realloc2(sfv_data, n, 1, 1, 1); * sprintf(sfv_data, storage "/%s/sfvdata", targetDir); * * readsfv(sfv_data, &g.v, 0); */ cnt = extractDirname(link_target, targetDir); strlcpy(link_source, targetDir, PATH_MAX); if (chdir(targetDir) == -1) { d_log("audioSortDir: Failed to chdir() to %s: %s\n", targetDir, strerror(errno)); } if ((ourDir = opendir(targetDir)) == NULL) { printf("Error: Failed to open dir \"%s\" : %s\n", targetDir, strerror(errno)); return; } file_target = findfileextfromlist(ourDir, audio_types); get_audio_info(file_target, &info); audioSort(&info, link_source, link_target); closedir(ourDir); }
/* * for init cc */ RQ *rq_item_init(){ RQ *rq; rq = (RQ *)calloc(1, sizeof(RQ)); if(rq == NULL){ d_log("Can't calloc for RQ"); exit(1); } rq->frontend = (FC_ITEM *)calloc(1, sizeof(FC_ITEM)); if(rq->frontend == NULL){ d_log("Can't calloc for CC_ITEM"); exit(1); } rq->isjob = JOB_FREE; rq->next = NULL; return rq; }
void setup_token(void *(*func)(void *), void *arg) { pthread_t thread; pthread_attr_t attr; int ret; pthread_attr_init(&attr); if ((ret = pthread_create(&thread, &attr, func, arg)) != 0) { d_log("Can't create thread"); exit(1); } }
void f_chart_append_envelope(struct s_chart *chart, unsigned int code, float x, float max, float min) { chart->kind[code] = e_chart_kind_envelope; if (chart->head[code] < d_chart_bucket) { chart->values[code][chart->head[code]].x = x; chart->values[code][chart->head[code]].y = max; chart->values[code][chart->head[code]].w = min; chart->values[code][chart->head[code]].normalized.done = d_false; chart->head[code]++; } else d_log(d_log_level_default, "[WARNING] - d_chart_bucket too small"); }
/* * Modified : 02.19.2002 Author : Dark0n3 * Modified : 06.02.2005 by : js * Description : Creates directory where all race information will be * stored. */ void maketempdir(char *path) { char full_path[PATH_MAX], *p; snprintf(full_path, PATH_MAX, "%s/%s", storage, path); /* work recursively */ for (p = full_path; *p; p++) { if (*p == '/') { *p = '\0'; if (strlen(full_path) && mkdir(full_path, 0777) == -1 && errno != EEXIST) d_log("maketempdir: Failed to create tempdir (%s): %s\n", full_path, strerror(errno)); *p = '/'; } } /* the final entry */ if (mkdir(full_path, 0777) == -1 && errno != EEXIST) d_log("maketempdir: Failed to create tempdir (%s): %s\n", full_path, strerror(errno)); }
void f_chart_append_signal(struct s_chart *chart, unsigned int code, float x, float y) { if (chart->head[code] < d_chart_bucket) { chart->values[code][chart->head[code]].x = x; chart->values[code][chart->head[code]].y = y; chart->values[code][chart->head[code]].normalized.done = d_false; chart->head[code]++; } else d_log(d_log_level_default, "[WARNING] - d_chart_bucket too small"); chart->total[code] += y; chart->total_square[code] += y*y; chart->elements[code]++; }
int AudioSystem::PlaySound(DataStreamPtr stream, int category, int flag, int channel) { IAudioPtr ado = ADO_Load(stream); if (ado == NULL) { d_log("!: load audio failed"); return -1; } return PlaySound(ado, category, flag, channel); }
void create_worker(void *(*func)(void *), void *arg) { pthread_t thread; pthread_attr_t attr; int ret; pthread_attr_init(&attr); if ((ret = pthread_create(&thread, &attr, func, arg)) != 0) { d_log("Can't create thread"); printf("here---\n"); exit(1); } }
void p_chart_create_bins(struct s_chart *chart, unsigned int code) { float step = fabs(chart->axis_x.range[1]-chart->axis_x.range[0])/(float)chart->bins[code], current_value; for (current_value = chart->axis_x.range[0]; current_value <= chart->axis_x.range[1]; current_value += step) { if (chart->head[code] < d_chart_bucket) { chart->values[code][chart->head[code]].x = current_value; chart->values[code][chart->head[code]].y = 0; chart->values[code][chart->head[code]].normalized.done = d_false; chart->head[code]++; } else d_log(d_log_level_default, "[WARNING] - d_chart_bucket too small"); } chart->kind[code] = e_chart_kind_histogram; }
short int match_file(char *rname, char *f) { int n; FILE *file = NULL; RACEDATA rd; n = 0; if ((file = fopen(rname, "r+"))) { while (fread(&rd, sizeof(RACEDATA), 1, file)) { if (strncmp(rd.fname, f, NAME_MAX) == 0 && rd.status == F_CHECKED) { d_log("match_file: '%s' == '%s'\n", rd.fname, f); n = 1; break; } } fclose(file); } else { d_log("match_file: Error fopen(%s): %s\n", rname, strerror(errno)); } return n; }
void on_accept(int fd, short ev, void *arg){ int client_fd, err_len; char *err, *err_log; struct sockaddr_in client_addr; socklen_t client_len; client_len = sizeof(client_addr); uint64_t u; ssize_t s; extern NOTIFY_TOKEN_STATE notify_token_thread; extern int token_efd; int isDep; /* Accept the new connection. */ client_fd = accept(fd, (struct sockaddr *)&client_addr, &client_len); if (client_fd == -1) { err = strerror(errno); /*err_len = strlen(err); err_log = calloc(1, err_len+10); sprintf(err_log, "accept-- %s", err); d_log(err_log); free(err_log);*/ DEBUG("accept error %s --", err); return; } if(rq_push(client_fd) == 0){ if(notify_token_thread == NT_FREE){ /* thread = work_threads+1; write(thread->notify_write_fd, "", 1); token_sem_post();*/ u = 1; s = write(token_efd, &u , sizeof(uint64_t)); if(s != sizeof(uint64_t))DEBUG("write to token_efd"); } /*else{ DEBUG("notify_token error"); } */ }else{ close(client_fd); d_log("RQ is full"); } if(proc_status == NT_FREE){ proc_status = NT_HAS; anyThread(fproc, NULL); } }
int AudioSystem::PlaySound(const String & filename, int category, int flag, int channel) { if (filename == "") return -1; DataStreamPtr stream = ResourceManager::Instance()->OpenResource(filename); if (stream == NULL) { d_log("!: file open failed, '%s'", filename.c_str()); return -1; } return PlaySound(stream, category, flag, channel); }
/* * === FUNCTION ====================================================================== * Name: anyThread * Description: * ===================================================================================== */ void anyThread (void *(*func)(void *), void *arg ){ pthread_t thread; pthread_attr_t attr; int ret; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); if ((ret = pthread_create(&thread, &attr, func, arg)) != 0) { d_log("Can't create thread"); //exit(1); } pthread_attr_destroy(&attr); } /* ----- end of function anyThread ----- */