mirror of
https://github.com/rtlsdrblog/rtl-sdr-blog.git
synced 2025-01-13 11:47:21 +01:00
exit gracefully on SIGPIPE
this will properly free all the used memory when working with a pipe and closing the other end (think of GRC)
This commit is contained in:
parent
3738a6730c
commit
f5f92ae3fa
32
src/main.c
32
src/main.c
@ -57,8 +57,12 @@ static void sighandler(int signum)
|
|||||||
|
|
||||||
static void rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx)
|
static void rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx)
|
||||||
{
|
{
|
||||||
if (ctx)
|
if (ctx) {
|
||||||
fwrite(buf, len, 1, (FILE*)ctx);
|
if (fwrite(buf, 1, len, (FILE*)ctx) != len) {
|
||||||
|
fprintf(stderr, "Short write, samples lost, exiting!\n");
|
||||||
|
rtlsdr_cancel_async(dev);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
@ -148,6 +152,7 @@ int main(int argc, char **argv)
|
|||||||
sigaction(SIGINT, &sigact, NULL);
|
sigaction(SIGINT, &sigact, NULL);
|
||||||
sigaction(SIGTERM, &sigact, NULL);
|
sigaction(SIGTERM, &sigact, NULL);
|
||||||
sigaction(SIGQUIT, &sigact, NULL);
|
sigaction(SIGQUIT, &sigact, NULL);
|
||||||
|
sigaction(SIGPIPE, &sigact, NULL);
|
||||||
|
|
||||||
/* Set the sample rate */
|
/* Set the sample rate */
|
||||||
r = rtlsdr_set_sample_rate(dev, samp_rate);
|
r = rtlsdr_set_sample_rate(dev, samp_rate);
|
||||||
@ -168,14 +173,14 @@ int main(int argc, char **argv)
|
|||||||
else
|
else
|
||||||
fprintf(stderr, "Tuner gain set to %i dB.\n", gain);
|
fprintf(stderr, "Tuner gain set to %i dB.\n", gain);
|
||||||
|
|
||||||
if(strcmp(filename, "-") != 0) {
|
if(strcmp(filename, "-") == 0) { /* Write samples to stdout */
|
||||||
|
file = stdout;
|
||||||
|
} else {
|
||||||
file = fopen(filename, "wb");
|
file = fopen(filename, "wb");
|
||||||
if (!file) {
|
if (!file) {
|
||||||
fprintf(stderr, "Failed to open %s\n", filename);
|
fprintf(stderr, "Failed to open %s\n", filename);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
file = stdout;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Reset endpoint before we start reading from it (mandatory) */
|
/* Reset endpoint before we start reading from it (mandatory) */
|
||||||
@ -187,10 +192,15 @@ int main(int argc, char **argv)
|
|||||||
fprintf(stderr, "Reading samples in sync mode...\n");
|
fprintf(stderr, "Reading samples in sync mode...\n");
|
||||||
while (!do_exit) {
|
while (!do_exit) {
|
||||||
r = rtlsdr_read_sync(dev, buffer, out_block_size, &n_read);
|
r = rtlsdr_read_sync(dev, buffer, out_block_size, &n_read);
|
||||||
if (r < 0)
|
if (r < 0) {
|
||||||
fprintf(stderr, "WARNING: sync read failed.\n");
|
fprintf(stderr, "WARNING: sync read failed.\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
fwrite(buffer, n_read, 1, file);
|
if (fwrite(buffer, 1, n_read, file) != n_read) {
|
||||||
|
fprintf(stderr, "Short write, samples lost, exiting!\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (n_read < out_block_size) {
|
if (n_read < out_block_size) {
|
||||||
fprintf(stderr, "Short read, samples lost, exiting!\n");
|
fprintf(stderr, "Short read, samples lost, exiting!\n");
|
||||||
@ -198,15 +208,15 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "Reading samples...\n");
|
fprintf(stderr, "Reading samples in async mode...\n");
|
||||||
rtlsdr_read_async(dev, rtlsdr_callback, (void *)file,
|
r = rtlsdr_read_async(dev, rtlsdr_callback, (void *)file,
|
||||||
DEFAULT_ASYNC_BUF_NUMBER, out_block_size);
|
DEFAULT_ASYNC_BUF_NUMBER, out_block_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (do_exit)
|
if (do_exit)
|
||||||
fprintf(stderr, "\nUser cancel, exiting...\n");
|
fprintf(stderr, "\nUser cancel, exiting...\n");
|
||||||
else
|
else
|
||||||
fprintf(stderr, "\nSystem cancel, exiting...\n");
|
fprintf(stderr, "\nLibrary error %d, exiting...\n", r);
|
||||||
|
|
||||||
if (file != stdout)
|
if (file != stdout)
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
Loading…
Reference in New Issue
Block a user