Passed
Push — master ( 86fd7b...3fe522 )
by Vitalii
01:34 queued 30s
created

log_write()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 7
nop 1
dl 0
loc 20
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
// This file is part of BOINC.
4
// http://boinc.berkeley.edu
5
// Copyright (C) 2011 University of California
6
//
7
// BOINC is free software; you can redistribute it and/or modify it
8
// under the terms of the GNU Lesser General Public License
9
// as published by the Free Software Foundation,
10
// either version 3 of the License, or (at your option) any later version.
11
//
12
// BOINC is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
// See the GNU Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public License
18
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
19
20
// server-side utility functions for remote job submissions and control
21
22
require_once("../inc/submit_db.inc");
23
24
// write status and error messages to log
25
//
26
function log_write($x) {
27
    static $enabled, $log_file;
28
29
    if (!isset($enabled)) {
30
        $enabled = false;
31
        $filename = parse_config(get_config(), "<remote_submit_log>");
32
        if (!$filename) {
33
            return;
34
        }
35
        $log_dir = parse_config(get_config(), "<log_dir>");
36
        if (!$log_dir) {
37
            return;
38
        }
39
        $log_file = fopen("$log_dir/$filename", "a");
40
        if (!$log_file) return;
41
        $enabled = true;
42
    }
43
    if (!$enabled) return;
44
    fwrite($log_file, sprintf("%s: %s\n", strftime("%c"), $x));
45
    fflush($log_file);
46
}
47
48
// in remote job submission,
49
// for input files of type local, semilocal, and inline,
50
// we need to give a unique physical name based on its content.
51
// Prepend the jf_ to make the origin of the file clear
52
//
53
function job_file_name($md5) {
54
    return "jf_$md5";
55
}
56
57
// does user have submit permissions?
58
//
59
function submit_permissions($user) {
60
    return BoincUserSubmit::lookup_userid($user->id);
61
}
62
63
// does user have submit permissions for given app?
64
//
65
function submit_permissions_app($user, $app) {
66
    return BoincUserSubmitApp::lookup("user_id=$user->id and app_id=$app->id");
67
}
68
69
// check whether user has permissions for a remote job submission
70
// or job file request.
71
// $r is a request message that includes an 'authenticator' field
72
// $app is the app being submitted to (or null if file op)
73
// returns [user, UserSubmit], or give XML error
74
//
75
function check_remote_submit_permissions($r, $app) {
76
    $auth = (string)$r->authenticator;
77
    if (!$auth) {
78
        log_write("no authenticator");
79
        xml_error(-1, "no authenticator");
80
    }
81
    $auth = BoincDb::escape_string($auth);
82
    $user = BoincUser::lookup("authenticator='$auth'");
83
    if (!$user) {
84
        log_write("bad authenticator");
85
        xml_error(-1, "bad authenticator");
86
    }
87
    $user_submit = submit_permissions($user);
88
    if (!$user_submit) {
89
        log_write("no submit access");
90
        xml_error(-1, "no submit access");
91
    }
92
    if ($app && !$user_submit->submit_all) {
93
        $usa = submit_permissions_app($user, $app);
94
        if (!$usa) {
95
            log_write("no app submit access");
96
            xml_error(-1, "no app submit access");
97
        }
98
    }
99
    return array($user, $user_submit);
100
}
101
102
function delete_remote_submit_user($user) {
103
    BoincUserSubmit::delete_user($user->id);
104
    BoincUserSubmitApp::delete_user($user->id);
105
}
106
107
108
// given its WUs, compute progress of a batch
109
// (fraction done, est completion time etc.)
110
// NOTE: this is inefficient because we need all the WUs.
111
// it could be done by server components
112
// (transitioner, validator etc.) as jobs complete or time out
113
//
114
// TODO: update est_completion_time
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
115
//
116
function get_batch_params($batch, $wus) {
117
    if ($batch->state == BATCH_STATE_INIT) {
118
        // a batch in INIT state has no jobs
119
        //
120
        return $batch;
121
    }
122
    $fp_total = 0;
123
    $fp_done = 0;
124
    $completed = true;
125
    $batch->nerror_jobs = 0;
126
    $batch->credit_canonical = 0;
127
    foreach ($wus as $wu) {
128
        $fp_total += $wu->rsc_fpops_est;
129
        if ($wu->canonical_resultid) {
130
            $fp_done += $wu->rsc_fpops_est;
131
            $batch->credit_canonical += $wu->canonical_credit;
132
        } else if ($wu->error_mask) {
133
            $batch->nerror_jobs++;
134
        } else {
135
            $completed = false;
136
        }
137
    }
138
    if ($fp_total) {
139
        $batch->fraction_done = $fp_done / $fp_total;
140
    }
141
    if ($completed && $batch->state == BATCH_STATE_IN_PROGRESS) {
142
        $batch->state = BATCH_STATE_COMPLETE;
143
        $batch->completion_time = time();
144
    }
145
    $batch->update("fraction_done = $batch->fraction_done, nerror_jobs = $batch->nerror_jobs, state=$batch->state, completion_time = $batch->completion_time, credit_canonical = $batch->credit_canonical");
146
147
    $batch->credit_estimate = flops_to_credit($fp_total);
148
    return $batch;
149
}
150
151
// get the number of WUs for which we've sent at least 1 instance
152
// TODO: do this more efficiently (single query)
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
153
//
154
function wus_nsent($wus) {
155
    $n = 0;
156
    foreach ($wus as $wu) {
157
        $res = BoincResult::enum(
158
            sprintf('workunitid=%d and server_state<>%d',
159
                $wu->id, RESULT_SERVER_STATE_UNSENT
160
            )
161
        );
162
        if (count($res) > 0) $n++;
163
    }
164
    return $n;
165
}
166
167
// get the physical names of a result's output files.
168
/
0 ignored issues
show
Coding Style introduced by
PHP syntax error: syntax error, unexpected '/', expecting end of file
Loading history...
Coding Style introduced by
Expected 1 space after "/"; newline found
Loading history...
Bug introduced by
A parse error occurred: Syntax error, unexpected '/' on line 168 at column 0
Loading history...
169
function get_outfile_phys_names($result) {
170
    $names = [];
171
    $xml = "<a>".$result->xml_doc_out."</a>";
172
    $r = simplexml_load_string($xml);
173
    if (!$r) return $names;
174
    foreach ($r->file_info as $fi) {
175
        $names[] = (string)($fi->name);
176
    }
177
    return $names;
178
}
179
180
function get_outfile_log_names($result) {
181
    $names = [];
182
    $xml = "<a>".$result->xml_doc_in."</a>";
183
    $r = simplexml_load_string($xml);
184
    if (!$r) return $names;
185
    foreach ($r->result->file_ref as $fr) {
186
        $names[] = (string)($fr->open_name);
187
    }
188
    return $names;
189
}
190
191
function get_outfile_paths($result) {
192
    $fanout = parse_config(get_config(), "<uldl_dir_fanout>");
193
    $upload_dir = parse_config(get_config(), "<upload_dir>");
194
195
    $paths = array();
196
    $xml = "<a>".$result->xml_doc_out."</a>";
197
    $r = simplexml_load_string($xml);
198
    if (!$r) return $paths;
199
    foreach ($r->file_info as $fi) {
200
        $path = dir_hier_path((string)($fi->name), $upload_dir, $fanout);
201
        $paths[] = $path;
202
    }
203
    return $paths;
204
}
205
206
function abort_workunit($wu) {
207
    BoincResult::update_aux(
208
        "server_state=5, outcome=5 where server_state=2 and workunitid=$wu->id"
209
    );
210
    $wu->update("error_mask=error_mask|16");
211
}
212
213
function abort_batch($batch) {
214
    $wus = BoincWorkunit::enum("batch=$batch->id");
215
    foreach ($wus as $wu) {
216
        abort_workunit($wu);
217
    }
218
    $batch->update("state=".BATCH_STATE_ABORTED);
219
    return 0;
220
}
221
222
// mark WUs as assimilated; this lets them be purged
223
//
224
function retire_batch($batch) {
225
    $wus = BoincWorkunit::enum("batch=$batch->id");
226
    $now = time();
227
    foreach ($wus as $wu) {
228
        $wu->update(
229
            "assimilate_state=".ASSIMILATE_DONE.", transition_time=$now"
230
        );
231
        // remove output template if it's a temporary
232
        //
233
        if (strstr($wu->result_template_file, "templates/tmp/")) {
234
            @unlink($wu->result_template_file);
235
        }
236
    }
237
    $batch->update("state=".BATCH_STATE_RETIRED);
238
}
239
240
function expire_batch($batch) {
241
    abort_batch($batch);
242
    retire_batch($batch);
243
    $batch->update("state=".BATCH_STATE_EXPIRED);
244
}
245
246
function batch_state_string($state) {
247
    switch ($state) {
248
    case BATCH_STATE_INIT: return "new";
249
    case BATCH_STATE_IN_PROGRESS: return "in progress";
250
    case BATCH_STATE_COMPLETE: return "completed";
251
    case BATCH_STATE_ABORTED: return "aborted";
252
    case BATCH_STATE_RETIRED: return "retired";
253
    }
254
    return "unknown state $state";
255
}
256
// get the total size of output files of a batch
257
//
258
function batch_output_file_size($batchid) {
259
    $batch_td_size=0;
260
    $wus = BoincWorkunit::enum("batch=$batchid");
261
    $fanout = parse_config(get_config(), "<uldl_dir_fanout>");
262
    $upload_dir = parse_config(get_config(), "<upload_dir>");
263
    foreach ($wus as $wu) {
264
        if (!$wu->canonical_resultid) continue;
265
        $result = BoincResult::lookup_id($wu->canonical_resultid);
266
        $names = get_outfile_phys_names($result);
267
        foreach ($names as $name) {
268
            $path = dir_hier_path($name, $upload_dir, $fanout);
269
            if (is_file($path)) {
270
                $batch_td_size += filesize($path);
271
            }
272
        }
273
    }
274
    return $batch_td_size;
275
}
276
277
function boinc_get_output_file_url($user, $result, $i) {
278
    $name = $result->name;
279
    $auth_str = md5($user->authenticator.$name);
280
    return "get_output.php?cmd=result_file&result_name=$name&file_num=$i&auth_str=$auth_str";
281
}
282
283
function boinc_get_output_files_url($user, $batch_id) {
284
    $auth_str = md5($user->authenticator.$batch_id);
285
    return "get_output.php?cmd=batch_files&batch_id=$batch_id&auth_str=$auth_str";
286
}
287
288
function boinc_get_wu_output_files_url($user, $wu_id) {
289
    $auth_str =  md5($user->authenticator.$wu_id);
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "="; 2 found
Loading history...
290
    return "get_output.php?cmd=workunit_files&wu_id=$wu_id&auth_str=$auth_str";
291
}
292
293
////////////////// FILE INFO FILES //////////////
294
295
// these are used:
296
// 1) in user file sandbox
297
// 2) in BUDA app variant dirs
298
// in each case a file dir/foo has an info file dir/.md5/foo
299
// containing its md5 and size
300
// (same format as .md5 files in download hierarchy)
301
302
// get the MD5 and size of a file
303
//
304
function get_file_info($path) {
305
    $md5 = md5_file($path);
306
    $s = stat($path);
307
    $size = $s['size'];
308
    return [$md5, $size];
309
}
310
311
// write a "info file" containing MD5 and size
312
//
313
function write_info_file($path, $md5, $size) {
314
    file_put_contents($path, "$md5 $size");
315
}
316
317
// parse info file and return [md5, size]
318
//
319
function parse_info_file($path) {
320
    if (!file_exists($path)) return null;
321
    $x = file_get_contents($path);
322
    $n = sscanf($x, "%s %d", $md5, $size);
323
    if ($n != 2 || strlen($md5)!=32) {
324
        return null;
325
    }
326
    return [$md5, $size];
327
}
328
329
?>
330