Skip to content

Commit

Permalink
Merge branch 'master' into cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-domke authored Mar 27, 2019
2 parents 3cfcbd9 + 4050495 commit 0cd6018
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 125 deletions.
52 changes: 22 additions & 30 deletions software/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,35 @@

#include "infnoise.h"

// Write af PID in a file
// Write PID in a file
static bool writePid(int32_t pid, char *fileName) {
FILE *pidFile;
int ret;
pidFile = fopen(fileName,"w");
FILE *pidFile = fopen(fileName, "w");
if(pidFile == NULL) {
return errno;
}
ret = fprintf(pidFile, "%d\n", pid);
if (ret < 0) {
return false;
}
int ret = fprintf(pidFile, "%d\n", pid);
fclose(pidFile);
return true;
return (ret >= 0);
}

void startDaemon(struct opt_struct* opts) {
if(!opts->daemon) {
// No backgrounding, optionslly write current PID
if(opts->pidFileName != NULL) {
writePid(getpid(), opts->pidFileName);
}
return;
}
int32_t pid = fork();
if(pid < 0) {
fputs("fork() failed\n", stderr);
exit(1);
} else if(pid > 0) {
// Parent
if(opts->pidFileName != NULL) {
if(!writePid(pid, opts->pidFileName)) {
exit(1);
}
}
exit(0);
}
// Child
if(!opts->daemon) {
// No backgrounding, optionally write current PID
if(opts->pidFileName != NULL) {
writePid(getpid(), opts->pidFileName);
}
return;
}
int32_t pid = fork();
if(pid < 0) {
fputs("fork() failed\n", stderr);
exit(1);
}
if(pid > 0) {
// Parent
exit(opts->pidFileName != NULL
&& !writePid(pid, opts->pidFileName));
}
// Child
}

38 changes: 15 additions & 23 deletions software/healthcheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ bool inmHealthCheckStart(uint8_t N, double K, bool debug) {
inmK = K;
inmN = N;
inmPrevBits = 0u;
inmOnesEven = calloc(1u << N, sizeof(uint32_t));
inmZerosEven = calloc(1u << N, sizeof(uint32_t));
inmOnesOdd = calloc(1u << N, sizeof(uint32_t));
inmZerosOdd = calloc(1u << N, sizeof(uint32_t));
inmOnesEven = calloc(1u << N, sizeof(*inmOnesEven));
inmZerosEven = calloc(1u << N, sizeof(*inmZerosEven));
inmOnesOdd = calloc(1u << N, sizeof(*inmOnesOdd));
inmZerosOdd = calloc(1u << N, sizeof(*inmZerosOdd));
inmExpectedEntropyPerBit = log(K)/log(2.0);
inmTotalBits = 0u;
inmPrevBit = false;
Expand Down Expand Up @@ -156,16 +156,10 @@ bool inmHealthCheckAddBit(bool evenBit, bool oddBit, bool even) {
bool bit;
if(even) {
bit = evenBit;
if(evenBit != inmPrevEven) {
inmEvenMisfires++;
//printf("even misfire\n");
}
inmEvenMisfires += (evenBit != inmPrevEven);
} else {
bit = oddBit;
if(oddBit != inmPrevOdd) {
inmOddMisfires++;
//printf("odd misfire\n");
}
inmOddMisfires += (oddBit != inmPrevOdd);
}
inmPrevEven = evenBit;
inmPrevOdd = oddBit;
Expand All @@ -177,8 +171,8 @@ bool inmHealthCheckAddBit(bool evenBit, bool oddBit, bool even) {
fprintf(stderr, "num1s:%f%%, even misfires:%f%%, odd misfires:%f%%\n",
inmTotalOnes*100.0/(inmTotalZeros + inmTotalOnes),
inmEvenMisfires*100.0/inmNumBitsSampled, inmOddMisfires*100.0/inmNumBitsSampled);
fflush(stderr);
}
fflush(stderr);
}
inmPrevBits = (inmPrevBits << 1) & ((1 << inmN)-1);
if(inmPrevBit) {
inmPrevBits |= 1;
Expand Down Expand Up @@ -305,12 +299,10 @@ static void checkLSBStatsForNBits(uint8_t N) {
uint32_t totalGuesses = 0u;
uint32_t totalRight = 0.0;
for(i = 0u; i < (1u << N); i++) {
uint32_t total = 0u;
uint32_t zeros = 0u;
uint32_t ones = 0u;
for(j = 0u; j < (1u << (inmN - N)); j++) {
uint32_t pos = i + j*(1u << N);
total += inmZerosEven[pos] + inmOnesEven[pos];
zeros += inmZerosEven[pos];
ones += inmOnesEven[pos];
}
Expand All @@ -319,7 +311,7 @@ static void checkLSBStatsForNBits(uint8_t N) {
} else {
totalRight += ones;
}
totalGuesses += total;
totalGuesses += zeros + ones;
}
printf("Probability of guessing correctly with %u bits: %f\n", N, (double)totalRight/totalGuesses);
}
Expand Down Expand Up @@ -356,17 +348,17 @@ static inline bool computeRandBit(double *A, double K, double noiseAmplitude) {
}

static void initOpts(struct opt_struct *opts) {
opts->outputMultiplier = 0u;
opts->daemon =
opts->outputMultiplier = 0u;
opts->daemon =
opts->debug =
opts->devRandom =
opts->noOutput =
opts->listDevices =
opts->raw = false;
opts->version = false;
opts->help = false;
opts->none = false;
opts->pidFileName =
opts->version = false;
opts->help = false;
opts->none = false;
opts->pidFileName =
opts->serial = NULL;
}

Expand Down
55 changes: 25 additions & 30 deletions software/infnoise.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ int main(int argc, char **argv) {
break;
case 'p':
opts.pidFileName = optarg;
if (opts.pidFileName == NULL || !strcmp("", opts.pidFileName)) {
if (opts.pidFileName == NULL || opts.pidFileName[0] == '\0') {
fputs("--pidfile without file name\n", stderr);
return 1;
}
break;
case 's':
opts.serial = optarg;
if (opts.serial == NULL || !strcmp("", opts.serial)) {
if (opts.serial == NULL || opts.serial[0] == '\0') {
fputs("--serial without value\n", stderr);
return 1;
}
Expand Down Expand Up @@ -184,31 +184,31 @@ int main(int argc, char **argv) {
" -v, --version - show version information\n"
" -h, --help - this help output\n",
stdout);
if (opts.none) {
return 1;
} else {
return 0;
}
return opts.none;
}

if (opts.version) {
printf("GIT VERSION - %s\n", GIT_VERSION);
printf("GIT COMMIT - %s\n", GIT_COMMIT);
printf("GIT DATE - %s\n", GIT_DATE);
return 0;
}

// read environment variables, not overriding command line options
if (opts.serial == NULL) {
if (getenv("INFNOISE_SERIAL") != NULL) {
opts.serial = getenv("INFNOISE_SERIAL");
}
opts.serial = getenv("INFNOISE_SERIAL");
}

if (!opts.debug) {
if (getenv("INFNOISE_DEBUG") != NULL) {
if (!strcmp("true", getenv("INFNOISE_DEBUG"))) {
opts.debug = true;
}
}
char *envDbg = getenv("INFNOISE_DEBUG");
opts.debug = (envDbg != NULL
&& !strcmp("true", envDbg));
}

if (!multiplierAssigned) {
if (getenv("INFNOISE_MULTIPLIER") != NULL) {
int tmpOutputMult = atoi(getenv("INFNOISE_MULTIPLIER"));
char *envMultiplier = getenv("INFNOISE_MULTIPLIER");
if (envMultiplier != NULL) {
int tmpOutputMult = atoi(envMultiplier);
if (tmpOutputMult < 0) {
fputs("Multiplier must be >= 0\n", stderr);
return 1;
Expand All @@ -222,25 +222,22 @@ int main(int argc, char **argv) {
opts.outputMultiplier = 2u; // Don't throw away entropy when writing to /dev/random unless told to do so
}

if (opts.version) {
printf("GIT VERSION - %s\n", GIT_VERSION);
printf("GIT COMMIT - %s\n", GIT_COMMIT);
printf("GIT DATE - %s\n", GIT_DATE);
return 0;
}

if (opts.listDevices) {
devlist_node devlist = listUSBDevices(&context.message);
if (devlist == NULL) {
fprintf(stderr, "Error: %s\n", context.message);
return 1;
}
devlist_node curdev = NULL;
uint8_t i = 0;
for (curdev = devlist; curdev != NULL; i++) {
printf("ID: %i, Manufacturer: %s, Description: %s, Serial: %s\n", curdev->id, curdev->manufacturer,
devlist_node curdev;
for (curdev = devlist; curdev != NULL; ) {
printf("ID: %i, Manufacturer: %s, Description: %s, Serial: %s\n",
curdev->id, curdev->manufacturer,
curdev->description, curdev->serial);

// cleanup:
devlist_node olddev = curdev;
curdev = curdev->next;
free(olddev);
}
return 0;
}
Expand Down Expand Up @@ -315,9 +312,7 @@ int main(int argc, char **argv) {
fprintf(stderr, "Output %lu bytes\n", (unsigned long) totalBytesWritten);
}
}

deinitInfnoise(&context);
inmWriteEntropyEnd();

return 0;
}
Loading

0 comments on commit 0cd6018

Please sign in to comment.