File: cmd-background.c

package info (click to toggle)
remctl 3.18-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,612 kB
  • sloc: ansic: 19,504; sh: 5,386; perl: 1,778; java: 740; makefile: 715; xml: 502; python: 430
file content (50 lines) | stat: -rw-r--r-- 1,249 bytes parent folder | download | duplicates (4)
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
/*
 * Small C program that outputs a string, forks off a process that sleeps for
 * ten seconds and outputs another string, and meanwhile immediately exits.
 * Used to test that remctld stops listening as soon as its child has exited
 * and doesn't wait forever for output to be closed.
 *
 * Written by Russ Allbery <eagle@eyrie.org>
 * Copyright 2007, 2012
 *     The Board of Trustees of the Leland Stanford Junior University
 *
 * SPDX-License-Identifier: MIT
 */

#include <config.h>
#include <portable/system.h>

#include <tests/tap/basic.h>
#include <util/xmalloc.h>


int
main(void)
{
    pid_t pid;
    char *tmpdir, *path;
    FILE *pidfile;

    printf("Parent\n");
    fflush(stdout);
    pid = fork();
    if (pid < 0) {
        fprintf(stderr, "Cannot fork child\n");
        exit(1);
    } else if (pid == 0) {
        tmpdir = test_tmpdir();
        xasprintf(&path, "%s/cmd-background.pid", tmpdir);
        pidfile = fopen(path, "w");
        if (pidfile != NULL) {
            fprintf(pidfile, "%lu\n", (unsigned long) getpid());
            fclose(pidfile);
        }
        sleep(10);
        printf("Child\n");
        unlink(path);
        free(path);
        test_tmpdir_free(tmpdir);
        exit(0);
    }
    return 0;
}