void prng_seed (const char *password_filename, int password_length) { unsigned char *a = calloc (64 + password_length, 1); assert (a != NULL); long long r = rdtsc (); struct timespec T; my_clock_gettime (CLOCK_REALTIME, &T); memcpy (a, &T.tv_sec, 4); memcpy (a+4, &T.tv_nsec, 4); memcpy (a+8, &r, 8); unsigned short p = getpid (); memcpy (a + 16, &p, 2); int s = get_random_bytes (a + 18, 32) + 18; if (password_filename) { int fd = open (password_filename, O_RDONLY); if (fd < 0) { logprintf ( "Warning: fail to open password file - \"%s\", %m.\n", password_filename); } else { int l = read (fd, a + s, password_length); if (l < 0) { logprintf ( "Warning: fail to read password file - \"%s\", %m.\n", password_filename); } else { if (verbosity > 0) { logprintf ( "read %d bytes from password file.\n", l); } s += l; } close (fd); } } RAND_seed (a, s); BN_ctx = BN_CTX_new (); memset (a, 0, s); free (a); }
static double _get_utime (int clock_id) { struct timespec T; my_clock_gettime (clock_id, &T); double res = T.tv_sec + (double) T.tv_nsec * 1e-9; /* if (clock_id == CLOCK_REALTIME) { */ /* precise_time = (long long) (res * (1LL << 32)); */ /* } */ return res; }
void dcethread__interrupt(dcethread* thread) { int count = 0; int old_state = thread->state; if (old_state == DCETHREAD_STATE_INTERRUPT || old_state == DCETHREAD_STATE_DEAD) { /* Don't bother */ return; } DCETHREAD_TRACE("Thread %p: interrupt posted", thread); dcethread__change_state(thread, DCETHREAD_STATE_INTERRUPT); /* We need to poke the thread and wait for an acknowledgement of the interrupt if: */ if (thread != dcethread__self() && /* The interrupted thread is not us, and */ thread->flag.interruptible && /* The thread can be interrupted, and */ old_state == DCETHREAD_STATE_BLOCKED) /* The thread was blocked */ { /* FIXME: potential livelock here if another thread re-interrupts when the lock is released */ while (thread->state == DCETHREAD_STATE_INTERRUPT) { struct timespec waittime; if (count > 2) DCETHREAD_WARNING("Thread %p: still not interrupted after %i ms", thread, count * 100); if (thread->interrupt(thread, thread->interrupt_data)) { /* Interrupt is guaranteed to have succeeded, so leave state change wait loop */ break; } count++; my_clock_gettime(&waittime); waittime.tv_nsec += 100000000; if (waittime.tv_nsec > 1000000000) { waittime.tv_nsec -= 1000000000; waittime.tv_sec += 1; } /* Wait for state change */ dcethread__timedwait(thread, &waittime); } } }
int my_gettimeofday(struct timeval *timeval, void *unused) { int ret = 0; if(NULL == timeval) { fprintf(std_err, "invalid argument.\n"); return -1; } struct timespec ts; memset(&ts, 0, sizeof(struct timespec)); ret = my_clock_gettime(CLOCK_MONOTONIC, &ts); if(ret < 0) { fprintf(std_err, "my_clock_gettime error[%s]", strerror(errno)); return -1; } timeval->tv_sec = ts.tv_sec; timeval->tv_usec = ts.tv_nsec / 1000; return (0); }
int main (int argc, char **argv) { int c; program_name = argv[0]; long long int msec = 0;// start timecode in ms from 00:00:00.00 long int date = 0;// bcd: 201012 = 20 Oct 2012 long int tzoff = 0;// time-zone in minuteswest int custom_user_bits = 0; while ((c = getopt_long (argc, argv, "h" /* help */ "f:" /* fps */ "d:" /* date */ "g:" /* gain^wvolume */ "l:" /* duration */ "r" /* reverse */ "s:" /* samplerate */ "t:" /* timecode */ "z:" /* timezone */ "m:" /* timezone */ "u:" /* free format user bits */ "V", /* version */ long_options, (int *) 0)) != EOF) { switch (c) { case 'V': printf ("%s %s\n\n",basename(argv[0]), VERSION); printf ( "Copyright (C) 2012 Robin Gareus <*****@*****.**>\n" "This is free software; see the source for copying conditions. There is NO\n" "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n" ); exit (0); case 'h': usage (0); case 'f': parse_fps(optarg); break; case 'd': { date=atoi(optarg); char *tmp = optarg; if (tmp && (tmp = strchr(tmp, '/'))) date=date*100+(atoi(++tmp)*10000); if (tmp) { if ((tmp = strchr(tmp, '/'))) date+=atoi(++tmp); else date+=12;// 2012 } } break; case 'g': volume_dbfs = atof(optarg); if (volume_dbfs > 0) volume_dbfs=0; if (volume_dbfs < -96.0) volume_dbfs=-96.0; printf("Output volume %.2f dBfs\n", volume_dbfs); break; case 'm': tzoff=atoi(optarg); //minuteswest break; case 'r': reverse = 1; break; case 's': samplerate=atoi(optarg); break; case 'z': { int hh = atoi(optarg)/100; tzoff= 60*hh + ((atoi(optarg)-(100*hh))%60); // HHMM } break; case 'l': { int bcd[SMPTE_LAST]; parse_string(rint(fps_num/(double)fps_den), bcd, optarg); duration = bcdarray_to_framecnt(bcd) * 1000.0 / (((double)fps_num)/(double)fps_den); } break; case 't': { sync_now=0; int bcd[SMPTE_LAST]; parse_string(rint(fps_num/(double)fps_den), bcd, optarg); msec = bcdarray_to_framecnt(bcd) * 1000.0 / (((double)fps_num)/(double)fps_den); } break; case 'u': { custom_user_bits = 1; parse_user_bits(user_bit_array, optarg); /* Free format user bits, so reset any date/timezone settings. */ date = 0; tzoff = 0; } break; default: usage (EXIT_FAILURE); } } if (optind >= argc) { usage (EXIT_FAILURE); } fps_sanity_checks(); { SF_INFO sfnfo; memset(&sfnfo, 0, sizeof(SF_INFO)); sfnfo.samplerate = samplerate; sfnfo.channels = 1; sfnfo.format = SF_FORMAT_WAV | sf_format; sf = sf_open(argv[optind], SFM_WRITE, &sfnfo); if (!sf) { fprintf(stderr, "cannot open output file '%s'\n", argv[optind]); return 1; } } printf("writing to '%s'\n", argv[optind]); printf("samplerate: %d, duration %.1f ms\n", samplerate, duration); encoder_setup(fps_num, fps_den, ltc_tv, samplerate, ((date != 0) ? LTC_USE_DATE : 0) | ((sync_now) ? (LTC_USE_DATE|LTC_TC_CLOCK) : 0) ); if (sync_now==0) { #if 0 // DEBUG printf("date: %06ld (DDMMYY)\n", date); printf("time: %lldms\n", msec); printf("zone: %c%02d%02d = %ld minutes west\n", tzoff<0?'-':'+', abs(tzoff/60),abs(tzoff%60), tzoff); #endif set_encoder_time(1000.0*msec, date, tzoff, fps_num, fps_den, 1); } else { struct timespec t; long int sync_msec; my_clock_gettime(&t); sync_msec = (t.tv_sec%86400)*1000 + (t.tv_nsec/1000000); time_t now = t.tv_sec; struct tm gm; long int sync_date = 0; if (gmtime_r(&now, &gm)) sync_date = gm.tm_mday*10000 + (gm.tm_mon + 1)*100 + (gm.tm_year % 100); sync_msec += 1000.0 * ltc_frame_alignment(samplerate * fps_den / (double) fps_num, ltc_tv) / samplerate; set_encoder_time(1000.0*sync_msec, custom_user_bits ? 0 : sync_date, 0, fps_num, fps_den, 1); } if (custom_user_bits) set_user_bits(user_bit_array); signal(SIGINT, endnow); if (reverse) main_loop_reverse(); else main_loop(); if (sf) sf_close(sf); if (enc_buf) free(enc_buf); if (encoder) ltc_encoder_free(encoder); return(0); }