File: beep.c

package info (click to toggle)
beep 1.4.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 524 kB
  • sloc: ansic: 863; sh: 228; python: 26; makefile: 10
file content (523 lines) | stat: -rw-r--r-- 16,723 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/* beep - beep the pc speaker any number of ways
 * Copyright (C) 2000-2010 Johnathan Nightingale
 * Copyright (C) 2010-2013 Gerfried Fuchs
 * Copyright (C) 2013-2018 Hans Ulrich Niedermann
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

/*
 * For more documentation on beep, see the beep-usage.txt and
 * beep.1.in files.
 */


#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <linux/kd.h>
#include <linux/input.h>

#include "beep-drivers.h"
#include "beep-drivers.h"
#include "beep-driver-console.h"
#include "beep-driver-evdev.h"
#include "beep-driver-noop.h"
#include "beep-library.h"
#include "beep-log.h"
#include "beep-usage.h"


static
const char version_message[] =
    PACKAGE_TARNAME " " PACKAGE_VERSION "\n"
    "Copyright (C) 2002-2016 Johnathan Nightingale\n"
    "Copyright (C) 2013-2018 Hans Ulrich Niedermann\n"
    "Use and Distribution subject to GPL.\n"
    "For information: http://www.gnu.org/copyleft/.\n";


typedef enum
    {
     END_DELAY_NO = 0,
     END_DELAY_YES = 1,
    } end_delay_E;

typedef enum
    {
     STDIN_BEEP_NONE = 0,
     STDIN_BEEP_LINE = 1,
     STDIN_BEEP_CHAR = 2,
    } stdin_beep_E;

typedef struct _beep_parms_T beep_parms_T;

/* Meaningful Defaults */
#define DEFAULT_FREQ       440   /* Middle A */
#define DEFAULT_LENGTH     200   /* milliseconds */
#define DEFAULT_REPS       1
#define DEFAULT_DELAY      100   /* milliseconds */
#define DEFAULT_END_DELAY  END_DELAY_NO
#define DEFAULT_STDIN_BEEP STDIN_BEEP_NONE

struct _beep_parms_T
{
    unsigned int freq; /* tone frequency (Hz)      */
    unsigned int length;     /* tone length    (ms)      */
    unsigned int reps;       /* # of repetitions         */
    unsigned int delay;      /* delay between reps  (ms) */
    end_delay_E  end_delay;  /* do we delay after last rep? */
    stdin_beep_E stdin_beep; /* are we using stdin triggers?  We have three options:
		     - just beep and terminate (default)
		     - beep after a line of input
		     - beep after a character of input
		     In the latter two cases, pass the text back out again,
		     so that beep can be tucked appropriately into a text-
		     processing pipe.
		  */
    beep_parms_T *next;  /* in case -n/--new is used. */
};


/* Global. Set to true by the signal handlers, read only by the main
 * thread. */
static volatile sig_atomic_t global_abort = false;


/* If we get interrupted, it would be nice to not leave the speaker
 * beeping in perpetuity.
 *
 * Everything called from this signal handler must be thread-safe,
 * signal-safe, reentrant including all API functions.  Otherwise, we
 * get another CVE-2018-0492.
 *
 * So we make certain we keep to using the following API calls:
 *
 *   * close(2):      safe
 *   * _exit(2):      safe (which exit(3) is NOT)
 *   * bzero(3):      MT-safe
 *   * memset(3):     MT-safe
 *   * write(2):      safe
 *   * strerror_r(3): MT-safe
 *   * strlen(3):     MT-safe
 */
void handle_signal(int unused_signum);

void handle_signal(int unused_signum __attribute__(( unused )))
{
    global_abort = true;
}


/* print usage and leave exit code up to the caller */
static
void print_usage(void)
{
    fputs(beep_usage, stdout);
}


/* print usage and exit */
static
void usage_bail(void)
    __attribute__(( noreturn ));

static
void usage_bail(void)
{
    print_usage();
    exit(EXIT_FAILURE);
}


/* Global. Written by parse_command_line(), read by main() initialization. */
static char *param_device_name = NULL;


/* Parse the command line.  argv should be untampered, as passed to main.
 * Beep parameters returned in result, subsequent parameters in argv will over-
 * ride previous ones.
 *
 * Currently valid parameters:
 *  "-f <frequency in Hz>"
 *  "-l <tone length in ms>"
 *  "-r <repetitions>"
 *  "-d <delay in ms>"
 *  "-D <delay in ms>" (similar to -d, but delay after last repetition as well)
 *  "-s" (beep after each line of input from stdin, echo line to stdout)
 *  "-c" (beep after each char of input from stdin, echo char to stdout)
 *  "--verbose/--debug"
 *  "-h/--help"
 *  "-v/-V/--version"
 *  "-n/--new"
 *
 * March 29, 2002 - Daniel Eisenbud points out that ch should be int, not char,
 * for correctness on platforms with unsigned chars.
 */
static
void parse_command_line(const int argc, char *const argv[], beep_parms_T *result)
{
    int ch;

    static const
        struct option opt_list[] =
        { {"help",    no_argument,       NULL, 'h'},
          {"version", no_argument,       NULL, 'V'},
          {"new",     no_argument,       NULL, 'n'},
          {"verbose", no_argument,       NULL, 'X'},
          {"debug",   no_argument,       NULL, 'X'},
          {"device",  required_argument, NULL, 'e'},
          {NULL,      0,                 NULL,  0 }
        };

    while ((ch = getopt_long(argc, argv, "f:l:r:d:D:schvVne:", opt_list, NULL))
           != EOF) {
        /* handle parsed numbers for various arguments */
        int          argval_i = -1;
        unsigned int argval_u = ~0U;
        float        argval_f = -1.0f;

        switch (ch) {
        case 'f':  /* freq */
            if (sscanf(optarg, "%f", &argval_f) != 1) {
                usage_bail();
            }
            if ((0.0f > argval_f) || (argval_f > 20000.0f)) {
                usage_bail();
            }
            argval_i = (int) (argval_f + 0.5f);
            argval_u = (unsigned int) argval_i;
            if (result->freq != 0) {
                log_warning("multiple -f values given, only last one is used.");
            }
            result->freq = argval_u;
            break;
        case 'l' : /* length */
            if (sscanf(optarg, "%u", &argval_u) != 1) {
                usage_bail();
            }
            if (argval_u > 300000U) {
                usage_bail();
            }
            result->length = argval_u;
            break;
        case 'r' : /* repetitions */
            if (sscanf(optarg, "%u", &argval_u) != 1) {
                usage_bail();
            }
            if (argval_u > 300000U) {
                usage_bail();
            }
            result->reps = argval_u;
            break;
        case 'd' : /* delay between reps - WITHOUT delay after last beep*/
            if (sscanf(optarg, "%u", &argval_u) != 1) {
                usage_bail();
            }
            if (argval_u > 300000U) {
                usage_bail();
            }
            result->delay = argval_u;
            result->end_delay = END_DELAY_NO;
            break;
        case 'D' : /* delay between reps - WITH delay after last beep */
            if (sscanf(optarg, "%u", &argval_u) != 1) {
                usage_bail();
            }
            if (argval_u > 300000U) {
                usage_bail();
            }
            result->delay = argval_u;
            result->end_delay = END_DELAY_YES;
            break;
        case 's' :
            result->stdin_beep = STDIN_BEEP_LINE;
            break;
        case 'c' :
            result->stdin_beep = STDIN_BEEP_CHAR;
            break;
        case 'v' :
        case 'V' : /* also --version */
            fputs(version_message, stdout);
            exit(EXIT_SUCCESS);
            /* break; unreachable */
        case 'n' : /* also --new - create another beep */
            if (result->freq == 0) {
                result->freq = DEFAULT_FREQ;
            }
            result->next = (beep_parms_T *)malloc(sizeof(beep_parms_T));
            result->next->freq       = 0;
            result->next->length     = DEFAULT_LENGTH;
            result->next->reps       = DEFAULT_REPS;
            result->next->delay      = DEFAULT_DELAY;
            result->next->end_delay  = DEFAULT_END_DELAY;
            result->next->stdin_beep = DEFAULT_STDIN_BEEP;
            result->next->next       = NULL;
            result = result->next; /* yes, I meant to do that. */
            break;
        case 'X' : /* --debug / --verbose */
            if (log_level < 999) {
                /* just limit to some finite value */
                log_level++;
            }
            break;
        case 'e' : /* also --device */
            if (param_device_name) {
                log_error("You cannot give the --device parameter more than once.");
                exit(EXIT_FAILURE);
            }
            param_device_name = optarg;
            break;
        case 'h': /* also --help */
            print_usage();
            exit(EXIT_SUCCESS);
            /* break; unreachable */
        default:
            usage_bail();
            /* break; unreachable */
        }
    }
    if (optind < argc) {
        log_error("non-option arguments left on command line");
        usage_bail();
    }
    if (result->freq == 0) {
        result->freq = DEFAULT_FREQ;
    }
}


static
int sleep_ms(beep_driver *driver, unsigned int milliseconds)
{
    const time_t seconds = milliseconds / 1000U;
    const long   nanoseconds = (milliseconds % 1000UL) * 1000UL * 1000UL;
    const struct timespec request =
        { seconds,
          nanoseconds };
    const int retcode = nanosleep(&request, NULL);
    if (global_abort) {
        beep_drivers_end_tone(driver);
        beep_drivers_fini(driver);
        exit(EXIT_FAILURE);
    }
    return retcode;
}


static
void play_beep(beep_driver *driver, beep_parms_T parms)
{
    log_verbose("%d times %d ms beeps (%d ms delay between, "
                "%d ms delay after) @ %d Hz",
                parms.reps, parms.length, parms.delay, parms.end_delay,
                parms.freq);

    /* repeat the beep */
    for (unsigned int i = 0; (!global_abort) && (i < parms.reps); i++) {
        beep_drivers_begin_tone(driver, parms.freq & 0xffff);
        sleep_ms(driver, parms.length);
        beep_drivers_end_tone(driver);
        if ((parms.end_delay == END_DELAY_YES) || ((i+1) < parms.reps)) {
            sleep_ms(driver, parms.delay);
        }
    }
}


/* If stdout is a TTY, print a bell character to stdout as a fallback. */
static
void fallback_beep(void)
{
    /* Printing '\a' can only beep if we print it to a tty */
    if (isatty(STDOUT_FILENO)) {
        fputc('\a', stdout);
    }
}


int main(const int argc, char *const argv[])
{
    log_init(argc, argv);

    /* Bail out if running setuid or setgid.
     *
     * It is near impossible to make beep setuid-safe:
     *
     *   * We open files for writing, and may even write to them.
     *
     *   * Checking the device file with realpath leaks information.
     *
     * So we refuse running setuid or setgid.
     */
    if ((getuid() != geteuid()) || (getgid() != getegid())) {
        log_error("Running setuid or setgid, "
                  "which is not supported for security reasons.");
        log_error("Set up permissions for the pcspkr evdev device file instead.");
        exit(EXIT_FAILURE);
    }

    /* Bail out if running under sudo.
     *
     * For the reasoning, see the setuid comment above.
     */
    if (getenv("SUDO_COMMAND") || getenv("SUDO_USER") || getenv("SUDO_UID") || getenv("SUDO_GID")) {
        log_error("Running under sudo, "
                  "which is not supported for security reasons.");
        log_error("Set up permissions for the pcspkr evdev device file instead.");
        exit(EXIT_FAILURE);
    }

    /* Parse command line */
    beep_parms_T *parms = (beep_parms_T *)malloc(sizeof(beep_parms_T));
    if (NULL == parms) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }
    parms->freq       = 0;
    parms->length     = DEFAULT_LENGTH;
    parms->reps       = DEFAULT_REPS;
    parms->delay      = DEFAULT_DELAY;
    parms->end_delay  = DEFAULT_END_DELAY;
    parms->stdin_beep = DEFAULT_STDIN_BEEP;
    parms->next       = NULL;

    parse_command_line(argc, argv, parms);

    /* Register drivers.  If we do that after parse_command_line, we may
     * have set the logging verbosity.  If we do that before
     * parse_command_line, parse_command_line might use some driver
     * functions.  Not sure yet which option we prefer.
     */
    /* beep_drivers_register(&noop_driver); */
    beep_drivers_register(&console_driver);
    beep_drivers_register(&evdev_driver);

    beep_driver *driver = NULL;

    if (param_device_name) {
        driver = beep_drivers_detect(param_device_name);
        if (!driver) {
            const int saved_errno = errno;
            log_error("Could not open %s for writing: %s",
                      param_device_name, strerror(saved_errno));
            exit(EXIT_FAILURE);
        }
    } else {
        driver = beep_drivers_detect(NULL);
        if (!driver) {
            log_error("Could not open any device");
            /* Output the only beep we can, in an effort to fall back on usefulness */
            fallback_beep();
            exit(EXIT_FAILURE);
        }
    }

    log_verbose("beep: using driver %p (name=%s, fd=%d, dev=%s)",
                (void *)driver, driver->name,
                driver->device_fd, driver->device_name);

    /* At this time, we know what API to use on which device, and we do
     * not have to fall back onto printing '\a' any more.
     */

    /* Memory barrier. All globals have been set up, so we make sure the
     * values are actually written to memory.  Only then do we install
     * the signal handlers.
     *
     * TBD: Use C11 atomic_signal_fence() instead of "__asm__ volatile"?
     */
    __asm__ volatile ("" : : : "memory");

    /* After all the initialization has happened and the global
     * variables used to communicate with the signal handlers have
     * actually been set up properly, we can finally install the signal
     * handlers. As we do not start making any noises, there is no need
     * to install the signal handlers any earlier.
     */
    signal(SIGINT,  handle_signal);
    signal(SIGTERM, handle_signal);

    /* this outermost while loop handles the possibility that -n/--new
       has been used, i.e. that we have multiple beeps specified. Each
       iteration will play, then free() one parms instance. */
    while ((!global_abort) && parms) {
        beep_parms_T *next = parms->next;

        if (parms->stdin_beep != STDIN_BEEP_NONE) {
            /* In this case, beep is probably part of a pipe, in which
               case POSIX says stdin and out should be fuly buffered.
               This however means very laggy performance with beep
               just twiddling it's thumbs until a buffer fills. Thus,
               kill the buffering.  In some situations, this too won't
               be enough, namely if we're in the middle of a long
               pipe, and the processes feeding us stdin are buffered,
               we'll have to wait for them, not much to be done about
               that. */
            setvbuf(stdin, NULL, _IONBF, 0);
            setvbuf(stdout, NULL, _IONBF, 0);

            char sin[4096];
            while ((!global_abort) && (fgets(sin, 4096, stdin))) {
                if (parms->stdin_beep == STDIN_BEEP_CHAR) {
                    for (char *ptr=sin; (!global_abort) && (*ptr); ptr++) {
                        putchar(*ptr);
                        fflush(stdout);
                        play_beep(driver, *parms);
                    }
                } else { /* STDIN_BEEP_LINE */
                    fputs(sin, stdout);
                    play_beep(driver, *parms);
                }
            }
        } else {
            play_beep(driver, *parms);
        }

        /* Junk each parms struct after playing it */
        free(parms);
        parms = next;
    }

    beep_drivers_end_tone(driver);
    beep_drivers_fini(driver);

    if (global_abort) {
        return EXIT_FAILURE;
    } else {
        return EXIT_SUCCESS;
    }
}


/*
 * Local Variables:
 * c-basic-offset: 4
 * indent-tabs-mode: nil
 * End:
 */