summaryrefslogtreecommitdiff
path: root/src/include_server_if.c
blob: 22679e96618ae1d81d862eaa0cd45084c17e241b (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
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
/* -*- c-file-style: "java"; indent-tabs-mode: nil; tab-width: 4; fill-column: 78 -*-
 * Copyright 2007 Google Inc.
 *
 * 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.
*/

/* Authors: Manos Renieris, Fergus Henderson */

#include <config.h>

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#include <netinet/in.h>
#include <netinet/tcp.h>

#include "distcc.h"
#include "trace.h"
#include "rpc.h"
#include "clinet.h"
#include "exitcode.h"
#include "util.h"
#include "hosts.h"
#include "include_server_if.h"

static int dcc_count_slashes(const char *path);
static int dcc_count_leading_dotdots(const char *path);
static int dcc_categorize_file(const char *include_server_filename);

/* The include server puts all files in its own special directory,
 * which is n path components long, where n = INCLUDE_SERVER_DIR_DEPTH
 */
#define INCLUDE_SERVER_DIR_DEPTH 3

/** Talks to the include server, over the AF_UNIX socket specified
 * in env variable INCLUDE_SERVER_PORT. If all goes well,
 * it returns the array of files in @p files and returns 0;
 * if anything goes wrong, it returns a non-zero value.
 */

int dcc_talk_to_include_server(char **argv, char ***files)
{
    char *include_server_port;
    int fd;
    struct sockaddr_un sa;

    int ret;
    char *stub;

    /* for testing purposes, if INCLUDE_SERVER_STUB is set,
       use its value rather than the include server */
    stub = getenv("INCLUDE_SERVER_STUB");
    if (stub != NULL) {
        ret = dcc_tokenize_string(stub, files);
        rs_log_warning("INCLUDE_SERVER_STUB is set to '%s'; "
                       "ignoring include server",
                       dcc_argv_tostr(*files));
        return ret;
    }

    include_server_port = getenv("INCLUDE_SERVER_PORT");
    if (include_server_port == NULL) {
        rs_log_warning("INCLUDE_SERVER_PORT not set - "
                       "did you forget to run under 'pump'?");
        return 1;
    }

    if (strlen(include_server_port) >= ((int)sizeof(sa.sun_path) - 1)) {
        rs_log_warning("$INCLUDE_SERVER_PORT is longer than %ld characters",
                       ((long) sizeof(sa.sun_path) - 1));
        return 1;
    }

    strcpy(sa.sun_path, include_server_port);
    sa.sun_family = AF_UNIX;

    if (dcc_connect_by_addr((struct sockaddr *) &sa, sizeof(sa), &fd))
        return 1;

    /* TODO? switch include_server to use more appropriate token names */
    if (dcc_x_cwd(fd) ||
        dcc_x_argv(fd, "ARGC", "ARGV", argv) ||
        dcc_r_argv(fd, "ARGC", "ARGV", files)) {
        rs_log_warning("failed to talk to include server '%s'",
                       include_server_port);
        dcc_close(fd);
        /* We are failing anyway, so we can ignore
           the return value of dcc_close() */
        return 1;
    }

    if (dcc_close(fd)) {
        return 1;
    }

    if (dcc_argv_len(*files) == 0) {
        rs_log_warning("include server gave up analyzing");
        return 1;
    }
    return 0;
}

/* The include server puts all files in its own special directory,
 * which is n path components long, where n = INCLUDE_SERVER_DIR_DEPTH
 * The original file should drop those components.
 * Also, we need to strip the .lzo and .lzo.abs suffixes.
 */
int dcc_get_original_fname(const char *fname, char **original_fname)
{
    int i;
    char *work, *alloced_work, *extension;

    alloced_work = work = strdup(fname);
    if (work == NULL)
        return EXIT_OUT_OF_MEMORY;

    /* Since all names are supposed to be absolute, they start with
     * a slash. We are trying to drop INCLUDE_SERVER_DIR_DEPTH path
     * components, so we start right after the first slash, and we look
     * for a slash, and then we skip that slash and look for a slash, etc.
     */

    for (i = 0; i < INCLUDE_SERVER_DIR_DEPTH; ++i) {
        work = strchr(work + 1, '/');
        if (work == NULL) {
            return 1;
        }
    }

    /* This code removes an abs extension if it's there, and
       then a .lzo extension if it's there. As a result
       a .lzo.abs extension is removed, but not a .abs.lzo
       extension.
     */
    extension = dcc_find_extension(work);
    if (extension && (strcmp(extension, ".abs") == 0)) {
        *extension = '\0';
    }
    extension = dcc_find_extension(work);
    if (extension && (strcmp(extension, ".lzo") == 0)) {
        *extension = '\0';
    }

    *original_fname = strdup(work);
    if (*original_fname == NULL) {
        free(alloced_work);
        return EXIT_OUT_OF_MEMORY;
    }
    free(alloced_work);
    return 0;
}

/**
 * This implements the --scan_includes option.
 * Talks to the include server, and prints the results to stdout.
 */
int
dcc_approximate_includes(struct dcc_hostdef *host, char **argv)
{
    char **files;
    int i;
    int ret;

    if (host->cpp_where != DCC_CPP_ON_SERVER) {
        rs_log_error("'--scan_includes' specified, "
                     "but distcc wouldn't have used include server "
                     "(make sure hosts list includes ',cpp' option?)");
        return EXIT_DISTCC_FAILED;
        //return 0;
    }

    if ((ret = dcc_talk_to_include_server(argv, &files))) {
        rs_log_error("failed to get includes from include server");
        return ret;
    }

    for (i = 0; files[i]; i++) {
        if ((ret = dcc_categorize_file(files[i])))
            return ret;
    }

    return 0;
}

/*
 * A subroutine of dcc_approximate_includes().
 * Take a filename output from the include server,
 * convert the filename back so that it refers to the original source tree
 * (as opposed to the include server's mirror tree),
 * categorize it as SYSTEMDIR, DIRECTORY, SYMLINK, or FILE,
 * and print the category and original name to stdout.
 * For SYMLINKs, also print out what the symlink points to.
 */
static int
dcc_categorize_file(const char *include_server_filename) {
    char *filename;
    int is_symlink = 0;
    int is_forced_directory = 0;
    int is_system_include_directory = 0;
    char link_target[MAXPATHLEN + 1];
    int ret;

    if ((ret = dcc_is_link(include_server_filename, &is_symlink)))
        return ret;

    if (is_symlink)
        if ((ret = dcc_read_link(include_server_filename, link_target)))
            return ret;

    if ((ret = dcc_get_original_fname(include_server_filename, &filename))) {
        rs_log_error("dcc_get_original_fname failed");
        return ret;
    }

    if (str_endswith("/forcing_technique_271828", filename)) {
        /* Replace "<foo>/forcing_technique_271818" with "<foo>". */
        filename[strlen(filename) - strlen("/forcing_technique_271828")]
            = '\0';
        is_forced_directory = 1;
    }

    if (is_symlink) {
        int leading_dotdots = dcc_count_leading_dotdots(link_target);
        is_system_include_directory =
            leading_dotdots > 0 &&
            leading_dotdots > dcc_count_slashes(filename) &&
            strcmp(link_target + 3 * leading_dotdots - 1, filename) == 0;
    }

    printf("%-9s %s\n", is_system_include_directory ? "SYSTEMDIR" :
                        is_forced_directory         ? "DIRECTORY" :
                        is_symlink                  ? "SYMLINK" :
                                                      "FILE",
                        filename);

    return 0;
}

/* Count the number of slashes in a path. */
static int
dcc_count_slashes(const char *path)
{
    int i;
    int count = 0;
    for (i = 0; path[i]; i++) {
        if (path[i] == '/')
            count++;
    }
    return count;
}

/* Count the number of leading "../" references in a path. */
static int
dcc_count_leading_dotdots(const char *path)
{
    int count = 0;
    while (str_startswith("../", path)) {
        path += 3;
        count++;
    }
    return count;
}