summaryrefslogtreecommitdiff
path: root/src/state.c
blob: ec24de51f2fd4ec469f17962f32101b89bff22ff (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
282
283
284
285
286
287
288
289
290
291
292
/* -*- c-file-style: "java"; indent-tabs-mode: nil; tab-width: 4; fill-column: 78 -*-
 *
 * distcc -- A simple distributed compiler system
 *
 * Copyright (C) 2003 by Martin Pool <mbp@samba.org>
 *
 * 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.
 */

#include <config.h>

#include <sys/time.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>

#include "types.h"
#include "distcc.h"
#include "rpc.h"
#include "trace.h"
#include "exitcode.h"
#include "snprintf.h"
#include "util.h"

const char *dcc_state_prefix = "binstate_";


static struct dcc_task_state *my_state = NULL;
static struct dcc_task_state local_state, remote_state;

static struct dcc_task_state *direct_my_state(const enum dcc_host target);

/**
 * @file
 *
 * This file provides a way for distcc processes to make little notes
 * about what they're up to that can be read by a monitor process.
 *
 * State is stored as follows.
 *
 * Within our temporary directory, we create a subdirectory called "state".
 *
 * Each process creates a file named "binstate%d", for its pid.  We
 * always rewrite this file from the beginning.
 *
 * Inside each of these, we store a binary struct in the native host
 * encoding.  Ugly, but quick and easy both in code and CPU time.
 *
 * Any process reading these files needs to handle the fact that they may be
 * truncated or otherwise incorrect.
 *
 * When the process exits, it removes its state file.  If you didn't
 * notice it already, it's too late now.
 *
 * In addition, if the process identified by the file no longer
 * exists, then the file must be orphaned by a process that suddenly
 * terminated.  The file is ignored and can be deleted by the first
 * process that notices it.
 *
 * The reader interface for these files is in mon.c
 *
 * These files are considered a private format, and they may change
 * between distcc releases.  The only supported way to read them is
 * through mon.c.
 **/


/**
 * Return newly allocated buffer holding the name of this process's state file.
 *
 * (This can't reliably be static because we might fork...)
 **/
static int dcc_get_state_filename(char **fname)
{
    int ret;
    char *dir;

    if ((ret = dcc_get_state_dir(&dir)))
        return ret;

    if (asprintf(fname, "%s/%s%ld",
                 dir, dcc_state_prefix, (long) getpid()) == -1) {
        return EXIT_OUT_OF_MEMORY;
    }

    return 0;
}


const char *dcc_get_phase_name(enum dcc_phase phase)
{
    switch (phase) {
    case DCC_PHASE_STARTUP:
        return "Startup";
    case DCC_PHASE_BLOCKED:
        return "Blocked";
    case DCC_PHASE_COMPILE:
        return "Compile";
    case DCC_PHASE_CPP:
        return "Preprocess";
    case DCC_PHASE_CONNECT:
        return "Connect";
    case DCC_PHASE_SEND:
        return "Send";
    case DCC_PHASE_RECEIVE:
        return "Receive";
    case DCC_PHASE_DONE:
        return "Done";
    default:
        return "Unknown";
    }
}


/**
 * Get a file descriptor for writing to this process's state file.
 * file.
 **/
static int dcc_open_state(int *p_fd,
                          const char *fname)
{
    int fd;

    fd = open(fname, O_CREAT|O_WRONLY|O_TRUNC|O_BINARY, 0666);
    if (fd == -1) {
        rs_log_error("failed to open %s: %s", fname, strerror(errno));
        return EXIT_IO_ERROR;
    }

    *p_fd = fd;
    return 0;
}


/**
 * Remove the state file for this process.
 *
 * This can be called from atexit().
 **/
void dcc_remove_state_file (void)
{
    char *fname;
    int ret;

    if ((ret = dcc_get_state_filename(&fname)))
        return;

    if (unlink(fname) == -1) {
        /* It's OK if we never created it */
        if (errno != ENOENT) {
            rs_log_warning("failed to unlink %s: %s", fname, strerror(errno));
            ret = EXIT_IO_ERROR;
        }
    }

    free(fname);

    (void) ret;
}


static int dcc_write_state(int fd)
{
    int ret;

    /* Write out as one big blob.  fd is positioned at the start of
     * the file. */

    if ((ret = dcc_writex(fd, my_state, sizeof *my_state)))
        return ret;

    return 0;
}


/**
 * Record the state of this process.
 *
 * The filename is trimmed down to its basename.
 *
 * If the source_file or host are NULL, then are left unchanged from
 * their previous value.
 **/
int dcc_note_state(enum dcc_phase state,
                   const char *source_file,
                   const char *host, enum dcc_host target)
{
    int fd;
    int ret;
    char *fname;
    struct timeval tv;


	if (!direct_my_state(target))
		return -1;

    my_state->struct_size = sizeof *my_state;
    my_state->magic = DCC_STATE_MAGIC;
    my_state->cpid = (unsigned long) getpid();

    if ((ret = dcc_get_state_filename(&fname)))
        return ret;

    source_file = dcc_find_basename(source_file);
    if (source_file) {
        strlcpy(my_state->file, source_file, sizeof my_state->file);
    }

    if (host) {
        strlcpy(my_state->host, host, sizeof my_state->host);
    }

    if (gettimeofday(&tv, NULL) == -1) {
        rs_log_error("gettimeofday failed: %s", strerror(errno));
        return EXIT_DISTCC_FAILED;
    }
    my_state->curr_phase = state;

    rs_trace("note state %d, file \"%s\", host \"%s\"",
             state,
             source_file ? source_file : "(NULL)",
             host ? host : "(NULL)");

    if ((ret = dcc_open_state(&fd, fname))) {
        free(fname);
        return ret;
    }

    if ((ret = dcc_write_state(fd))) {
        dcc_close(fd);
        free(fname);
        return ret;
    }

    dcc_close(fd);
    free(fname);

    return 0;
}


void dcc_note_state_slot(int slot, enum dcc_host target)
{
	if (direct_my_state(target))
		my_state->slot = slot;
}


/**
	Point 'my_state' to the local or remote host state information, depending on target.

	Return 'my_state' pointer.
**/
static struct dcc_task_state *direct_my_state(const enum dcc_host target)
{
	switch (target)
	{
		case DCC_LOCAL:
			my_state = &local_state;
			break;

		case DCC_REMOTE:
			my_state = &remote_state;
			break;

		case DCC_UNKNOWN:
			break;
	}

	if (!my_state)
		rs_log_error("my_state == NULL");

	return my_state;
}