aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/e/ebuild/utils/locked_pipe_command.cc
blob: a2619f3c44fe50fbe8483e31356c9a57c83d341a (plain)
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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007, 2009 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis 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., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <cstdlib>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <iostream>

int
main(int argc, char *argv[])
{
    if (argc != 3)
    {
        std::cerr << "Usage: " << argv[0] << " write_fd read_fd" << std::endl;
        return EXIT_FAILURE;
    }

    int write_fd(std::atoi(argv[1])), read_fd(std::atoi(argv[2]));
    if (0 != ::lockf(write_fd, F_LOCK, 0))
    {
        std::cerr << "Error: " << argv[0] << ": lockf failed with " << ::strerror(errno) << std::endl;
        return EXIT_FAILURE;
    }

    /* copy stdin to the pipe read buffer */
    char buf[1024];
    int c, w;
    while (((c = read(0, buf, 1024))) > 0)
    {
        char * buf_p(buf);
        while (((w = write(write_fd, buf_p, c))) > 0)
        {
            buf_p += w;
            c -= w;
            if (c == 0)
                break;
        }
        if (w == -1)
        {
            std::cerr << "Error: " << argv[0] << ": write failed with " << ::strerror(errno) << std::endl;
            return EXIT_FAILURE;
        }
    }

    if (c == -1)
    {
        std::cerr << "Error: " << argv[0] << ": read failed with " << ::strerror(errno) << std::endl;
        return EXIT_FAILURE;
    }

    /* append an 'end' marker to the pipe read buffer */
    buf[0] = '\0';
    while (((w = write(write_fd, buf, 1))) == 0)
        sleep(0);
    if (w == -1)
    {
        std::cerr << "Error: " << argv[0] << ": write failed with " << ::strerror(errno) << std::endl;
        return EXIT_FAILURE;
    }

    /* copy write buffer to stdout until we get a null, and discard that null */
    while (((c = read(read_fd, buf, 1024))) > 0)
    {
        bool done(false);
        if (buf[c - 1] == '\0')
        {
            done = true;
            --c;
        }

        char * buf_p(buf);
        while (((w = write(1, buf_p, c))) > 0)
        {
            c -= w;
            buf_p += w;
            if (c == 0)
                break;
        }

        if (w == -1)
        {
            std::cerr << "Error: " << argv[0] << ": write failed with " << ::strerror(errno) << std::endl;
            return EXIT_FAILURE;
        }

        if (done)
            break;
    }

    if (c == -1)
    {
        std::cerr << "Error: " << argv[0] << ": read failed with " << ::strerror(errno) << std::endl;
        return EXIT_FAILURE;
    }

    if (0 != ::lockf(write_fd, F_ULOCK, 0))
    {
        std::cerr << "Error: " << argv[0] << ": lockf unlock failed with " << ::strerror(errno) << std::endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}