-
Notifications
You must be signed in to change notification settings - Fork 171
/
os_proc.cpp
1309 lines (1122 loc) · 37.6 KB
/
os_proc.cpp
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************
*
* Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
* University of Wisconsin-Madison, WI.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************/
#include "condor_common.h"
#include "condor_classad.h"
#include "condor_config.h"
#include "condor_debug.h"
#include "env.h"
#include "user_proc.h"
#include "os_proc.h"
#include "starter.h"
#include "condor_daemon_core.h"
#ifdef HAVE_SCM_RIGHTS_PASSFD
#include "shared_port_scm_rights.h"
#include "fdpass.h"
#endif
#include "condor_attributes.h"
#include "classad_helpers.h"
#include "sig_name.h"
#include "exit.h"
#include "condor_uid.h"
#include "filename_tools.h"
#include "my_popen.h"
#ifdef WIN32
#include "perm.h"
#include "profile.WINDOWS.h"
#include "access_desktop.WINDOWS.h"
#endif
#include "classad_oldnew.h"
#include "singularity.h"
#include "find_child_proc.h"
#include "ToE.h"
#include <sstream>
#include <charconv>
extern Starter *Starter;
extern const char* JOB_WRAPPER_FAILURE_FILE;
extern const char* JOB_AD_FILENAME;
extern const char* MACHINE_AD_FILENAME;
int singExecPid = -1;
ReliSock *sns = 0;
/* OsProc class implementation */
OsProc::OsProc( ClassAd* ad ) : howCode(-1), cgroupActive(false)
{
dprintf ( D_FULLDEBUG, "In OsProc::OsProc()\n" );
JobAd = ad;
is_suspended = false;
is_checkpointed = false;
num_pids = 0;
dumped_core = false;
job_not_started = false;
m_using_priv_sep = false;
singReaperId = -1;
UserProc::initialize();
}
OsProc::~OsProc()
{
if ( daemonCore && daemonCore->SocketIsRegistered(&sshListener)) {
daemonCore->Cancel_Socket(&sshListener);
}
}
bool
OsProc::canonicalizeJobPath(/* not const */ std::string &JobName, const char *job_iwd) {
// prepend the full path to this name so that we
// don't have to rely on the PATH inside the
// USER_JOB_WRAPPER or for exec().
bool transfer_exe = true;
JobAd->LookupBool(ATTR_TRANSFER_EXECUTABLE, transfer_exe);
bool preserve_rel = false;
if (!JobAd->LookupBool(ATTR_PRESERVE_RELATIVE_EXECUTABLE, preserve_rel)) {
preserve_rel = false;
}
bool relative_exe = !fullpath(JobName.c_str()) && (JobName.length() > 0);
if (this->ShouldConvertCmdToAbsolutePath()) {
if (relative_exe && preserve_rel && !transfer_exe) {
dprintf(D_ALWAYS, "Preserving relative executable path: %s\n", JobName.c_str());
}
else if ( Starter->jic->usingFileTransfer() && transfer_exe ) {
formatstr( JobName, "%s%c%s",
Starter->GetWorkingDir(0),
DIR_DELIM_CHAR,
condor_basename(JobName.c_str()) );
}
else if (relative_exe && job_iwd && *job_iwd) {
std::string full_name;
formatstr(full_name, "%s%c%s",
job_iwd,
DIR_DELIM_CHAR,
JobName.c_str());
JobName = full_name;
}
}
return true;
}
int
OsProc::StartJob(FamilyInfo* family_info, FilesystemRemap* fs_remap=NULL)
{
int nice_inc = 0;
bool has_wrapper = false;
dprintf(D_FULLDEBUG,"in OsProc::StartJob()\n");
if ( !JobAd ) {
dprintf ( D_ALWAYS, "No JobAd in OsProc::StartJob()!\n" );
job_not_started = true;
return 0;
}
std::string JobName;
if ( JobAd->LookupString( ATTR_JOB_CMD, JobName ) != 1 ) {
dprintf( D_ALWAYS, "%s not found in JobAd. Aborting StartJob.\n",
ATTR_JOB_CMD );
job_not_started = true;
return 0;
}
const char* job_iwd = Starter->jic->jobRemoteIWD();
dprintf( D_ALWAYS, "IWD: %s\n", job_iwd );
// // // // // //
// Arguments
// // // // // //
this->canonicalizeJobPath(JobName, job_iwd);
if( Starter->isGridshell() ) {
// if we're a gridshell, just try to chmod our job, since
// globus probably transfered it for us and left it with
// bad permissions...
priv_state old_priv = set_user_priv();
int retval = chmod( JobName.c_str(), S_IRWXU | S_IRWXO | S_IRWXG );
set_priv( old_priv );
if( retval < 0 ) {
dprintf ( D_ALWAYS, "Failed to chmod %s!\n", JobName.c_str() );
return 0;
}
}
ArgList args;
// Since we may be adding to the argument list, we may need to deal
// with platform-specific arg syntax in the user's args in order
// to successfully merge them with the additional wrapper args.
args.SetArgV1SyntaxToCurrentPlatform();
std::string wrapper;
has_wrapper = param(wrapper, "USER_JOB_WRAPPER");
if (JobName.length() > 0) {
if( !getArgv0() || has_wrapper ) {
args.AppendArg(JobName.c_str());
} else {
args.AppendArg(getArgv0());
}
}
// Support USE_PARROT
bool use_parrot = false;
if( JobAd->LookupBool( ATTR_USE_PARROT, use_parrot) ) {
// Check for parrot executable
char *parrot = NULL;
if( (parrot=param("PARROT")) ) {
if( access(parrot,X_OK) < 0 ) {
dprintf( D_ALWAYS, "Unable to use parrot(Cannot find/execute "
"at %s(%s)).\n", parrot, strerror(errno) );
free( parrot );
job_not_started = true;
return 0;
} else {
args.AppendArg(JobName.c_str());
JobName = parrot;
free( parrot );
}
} else {
dprintf( D_ALWAYS, "Unable to use parrot(Undefined path in config"
" file)" );
job_not_started = true;
return 0;
}
}
// Either way, we now have to add the user-specified args as
// the rest of the Args string.
std::string args_error;
if(!args.AppendArgsFromClassAd(JobAd,args_error)) {
dprintf(D_ALWAYS, "Failed to read job arguments from JobAd. "
"Aborting OsProc::StartJob: %s\n",args_error.c_str());
job_not_started = true;
return 0;
}
// // // // // //
// Environment
// // // // // //
// Now, instantiate an Env object so we can manipulate the
// environment as needed.
Env job_env;
std::string env_errors;
if( !Starter->GetJobEnv(JobAd,&job_env, env_errors) ) {
dprintf( D_ALWAYS, "Aborting OSProc::StartJob: %s\n",
env_errors.c_str());
job_not_started = true;
return 0;
}
// // // // // //
// Standard Files
// // // // // //
// handle stdin, stdout, and stderr redirection
int fds[3];
// initialize these to -2 to mean they're not specified.
// -1 will be treated as an error.
fds[0] = -2; fds[1] = -2; fds[2] = -2;
// in order to open these files we must have the user's privs:
priv_state priv;
priv = set_user_priv();
bool stdin_ok;
bool stdout_ok;
bool stderr_ok;
fds[0] = openStdFile( SFT_IN,
NULL,
true,
"Input file");
stdin_ok = (fds[0] != -1);
fds[1] = openStdFile( SFT_OUT,
NULL,
true,
"Output file");
stdout_ok = (fds[1] != -1);
fds[2] = openStdFile( SFT_ERR,
NULL,
true,
"Error file");
stderr_ok = (fds[2] != -1);
/* Bail out if we couldn't open the std files correctly */
if( !stdin_ok || !stdout_ok || !stderr_ok ) {
/* only close ones that had been opened correctly */
for ( int i = 0; i <= 2; i++ ) {
if ( fds[i] >= 0 ) {
daemonCore->Close_FD ( fds[i] );
}
}
dprintf(D_ALWAYS, "Failed to open some/all of the std files...\n");
dprintf(D_ALWAYS, "Aborting OsProc::StartJob.\n");
set_priv(priv); /* go back to original priv state before leaving */
job_not_started = true;
return 0;
}
// // // // // //
// Misc + Exec
// // // // // //
if( !ThisProcRunsAlongsideMainProc() ) {
Starter->jic->notifyJobPreSpawn();
}
// compute job's renice value by evaluating the machine's
// JOB_RENICE_INCREMENT in the context of the job ad...
char* ptmp = param( "JOB_RENICE_INCREMENT" );
if( ptmp ) {
// insert renice expr into our copy of the job ad
if( !JobAd->AssignExpr( "Renice", ptmp ) ) {
dprintf( D_ALWAYS, "ERROR: failed to insert JOB_RENICE_INCREMENT "
"into job ad, Aborting OsProc::StartJob...\n" );
free( ptmp );
job_not_started = true;
return 0;
}
// evaluate
if( JobAd->LookupInteger( "Renice", nice_inc ) ) {
dprintf( D_ALWAYS, "Renice expr \"%s\" evaluated to %d\n",
ptmp, nice_inc );
} else {
dprintf( D_ALWAYS, "WARNING: job renice expr (\"%s\") doesn't "
"eval to int! Using default of 10...\n", ptmp );
nice_inc = 10;
}
// enforce valid ranges for nice_inc
if( nice_inc < 0 ) {
dprintf( D_FULLDEBUG, "WARNING: job renice value (%d) is too "
"low: adjusted to 0\n", nice_inc );
nice_inc = 0;
}
else if( nice_inc > 19 ) {
dprintf( D_FULLDEBUG, "WARNING: job renice value (%d) is too "
"high: adjusted to 19\n", nice_inc );
nice_inc = 19;
}
ASSERT( ptmp );
free( ptmp );
ptmp = NULL;
} else {
// if JOB_RENICE_INCREMENT is undefined, default to 0
nice_inc = 0;
}
// Grab the full environment back out of the Env object
if(IsFulldebug(D_FULLDEBUG)) {
std::string env_string;
job_env.getDelimitedStringForDisplay( env_string);
dprintf(D_FULLDEBUG, "Env = %s\n", env_string.c_str());
}
// Stash the environment in the manifest directory, if desired.
std::string manifest_dir = "_condor_manifest";
bool want_manifest = false;
if( JobAd->LookupString( ATTR_JOB_MANIFEST_DIR, manifest_dir ) ||
(JobAd->LookupBool( ATTR_JOB_MANIFEST_DESIRED, want_manifest ) && want_manifest) ) {
const std::string f = "environment";
FILE * file = Starter->OpenManifestFile( f.c_str() );
if( file != NULL ) {
std::string env_string;
job_env.getDelimitedStringForDisplay( env_string);
fprintf( file, "%s\n", env_string.c_str());
fclose(file);
} else {
dprintf( D_ALWAYS, "Failed to open environment log %s: %d (%s)\n", f.c_str(), errno, strerror(errno) );
}
}
// Check to see if we need to start this process paused, and if
// so, pass the right flag to DC::Create_Process().
int job_opt_mask = DCJOBOPT_NO_CONDOR_ENV_INHERIT;
bool inherit_starter_env = false;
auto_free_ptr envlist(param("JOB_INHERITS_STARTER_ENVIRONMENT"));
if (envlist && ! string_is_boolean_param(envlist, inherit_starter_env)) {
WhiteBlackEnvFilter filter(envlist);
job_env.Import(filter);
inherit_starter_env = false; // make sure that CreateProcess doesn't inherit again
}
if ( ! inherit_starter_env) {
job_opt_mask |= DCJOBOPT_NO_ENV_INHERIT;
}
bool suspend_job_at_exec = false;
JobAd->LookupBool( ATTR_SUSPEND_JOB_AT_EXEC, suspend_job_at_exec);
if( suspend_job_at_exec ) {
dprintf( D_FULLDEBUG, "OsProc::StartJob(): "
"Job wants to be suspended at exec\n" );
job_opt_mask |= DCJOBOPT_SUSPEND_ON_EXEC;
}
// If there is a requested coresize for this job, enforce it.
// Convert negative and very large values to RLIM_INFINITY, meaning
// no size limit.
// RLIM_INFINITY is unsigned, but its value and type size vary.
long long core_size_ad;
size_t core_size;
size_t *core_size_ptr = NULL;
#if !defined(WIN32)
if ( JobAd->LookupInteger( ATTR_CORE_SIZE, core_size_ad ) ) {
if ( core_size_ad < 0 ) {
core_size = RLIM_INFINITY;
} else {
core_size = (size_t)core_size_ad;
}
} else {
// if ATTR_CORE_SIZE is unset, assume 0
core_size = 0;
}
core_size_ptr = &core_size;
#endif // !defined(WIN32)
long rlimit_as_hard_limit = 0;
char *rlimit_expr = param("STARTER_RLIMIT_AS");
if (rlimit_expr) {
classad::ClassAdParser parser;
classad::ExprTree *tree = parser.ParseExpression(rlimit_expr);
if (tree) {
classad::Value val;
long long result = 0L;
if (EvalExprToNumber(tree, Starter->jic->machClassAd(), JobAd, val) &&
val.IsIntegerValue(result)) {
result *= 1024 * 1024; // convert to megabytes
rlimit_as_hard_limit = (long)result; // truncate for Create_Process
if (result > rlimit_as_hard_limit) {
// if truncation to long results in a change in the value, then
// the requested limit must be > 2 GB and we are on a 32 bit platform
// in that case, the requested limit is > than what the process can get anyway
// so just don't set a limit.
rlimit_as_hard_limit = 0;
}
if (rlimit_as_hard_limit > 0) {
dprintf(D_ALWAYS, "Setting job's virtual memory rlimit to %ld megabytes\n", rlimit_as_hard_limit);
}
} else {
dprintf(D_ALWAYS, "Can't evaluate STARTER_RLIMIT_AS expression %s\n", rlimit_expr);
}
delete tree;
} else {
dprintf(D_ALWAYS, "Can't parse STARTER_RLIMIT_AS expression: %s\n", rlimit_expr);
}
free( rlimit_expr );
}
int *affinity_mask = makeCpuAffinityMask(Starter->getMySlotNumber());
#ifdef WIN32
// if we loaded a slot user profile, import environment variables from it
bool load_profile = false;
bool run_as_owner = false;
JobAd->LookupBool ( ATTR_JOB_LOAD_PROFILE, load_profile );
JobAd->LookupBool ( ATTR_JOB_RUNAS_OWNER, run_as_owner );
if (load_profile && !run_as_owner) {
const char * username = get_user_loginname();
/* publish the users environment into that of the main job's environment */
if (!OwnerProfile::environment(job_env, priv_state_get_handle(), username)) {
dprintf(D_ALWAYS, "Failed to export environment for %s into the job.\n",
username ? username : "<null>");
}
}
#endif
// While we are still in user priv, print out the username
#if defined(LINUX)
std::stringstream ss2;
ss2 << Starter->GetExecuteDir() << DIR_DELIM_CHAR << "dir_" << getpid();
std::string execute_dir = ss2.str();
htcondor::Singularity::result sing_result;
int orig_args_len = args.Count();
if (SupportsPIDNamespace()) {
sing_result = htcondor::Singularity::setup(*Starter->jic->machClassAd(), *JobAd, JobName, args, job_iwd ? job_iwd : "", execute_dir, job_env);
} else {
sing_result = htcondor::Singularity::DISABLE;
}
if (sing_result == htcondor::Singularity::SUCCESS) {
dprintf(D_ALWAYS, "Running job via singularity.\n");
bool ssh_enabled = param_boolean("ENABLE_SSH_TO_JOB",true,true,Starter->jic->machClassAd(),JobAd);
if( ssh_enabled ) {
SetupSingularitySsh();
}
if (fs_remap) {
dprintf(D_ALWAYS, "Disabling filesystem remapping; singularity will perform these features.\n");
fs_remap = NULL;
}
if (family_info && family_info->want_pid_namespace) {
dprintf(D_FULLDEBUG, "PID namespaces cannot be enabled for singularity jobs.\n");
job_not_started = true;
free(affinity_mask);
return 0;
}
if (param_boolean("SINGULARITY_RUN_TEST_BEFORE_JOB", true)) {
std::string singErrorMessage;
bool result = htcondor::Singularity::runTest(JobName, args, orig_args_len, job_env, singErrorMessage);
if (!result) {
free(affinity_mask);
job_not_started = true;
std::string starterErrorMessage = "Singularity test failed:";
starterErrorMessage += singErrorMessage;
EXCEPT("%s", starterErrorMessage.c_str());
return 0;
}
}
} else if (sing_result == htcondor::Singularity::FAILURE) {
dprintf(D_ALWAYS, "Singularity enabled but setup failed; failing job.\n");
job_not_started = true;
free(affinity_mask);
return 0;
}
#else
if( false ) {
}
#endif
else {
char const *username = NULL;
char const *how = "";
username = get_user_loginname();
if( !username ) {
username = "same uid as parent: personal condor";
}
dprintf(D_ALWAYS,"Running job %sas user %s\n",how,username);
}
// Support USER_JOB_WRAPPER parameter...
if( has_wrapper ) {
// make certain this wrapper program exists and is executable
if( access(wrapper.c_str(),X_OK) < 0 ) {
dprintf( D_ALWAYS,
"Cannot find/execute USER_JOB_WRAPPER file %s\n",
wrapper.c_str() );
job_not_started = true;
free(affinity_mask);
return 0;
}
// Now, we've got a valid wrapper. We want that to become
// "JobName" so we exec it directly. We also insert the
// wrapper filename at the front of args. As a result,
// the executable being wrapped is now argv[1] and so forth.
args.InsertArg(wrapper.c_str(),0);
JobName = wrapper;
}
// in the below dprintfs, we want to skip past argv[0], which
// is sometimes condor_exec, in the Args string.
std::string args_string;
args.GetArgsStringForDisplay(args_string, 1);
if( has_wrapper ) {
// print out exactly what we're doing so folks can debug
// it, if they need to.
dprintf( D_ALWAYS, "Using wrapper %s to exec %s\n", JobName.c_str(),
args_string.c_str() );
} else {
dprintf( D_ALWAYS, "About to exec %s %s\n", JobName.c_str(),
args_string.c_str() );
}
set_priv ( priv );
// use this to return more detailed and reliable error message info
// from create-process operation.
std::string create_process_err_msg;
//
// The following two commented-out Create_Process() calls were
// left in as examples of (respectively) that bad old way,
// the bad new way (in case you actually need to set all of
// the default arguments), and the good new way (in case you
// don't, in which case it's both shorter and clearer).
//
/*
JobPid = daemonCore->Create_Process( JobName.c_str(),
args,
PRIV_USER_FINAL,
1,
FALSE,
FALSE,
&job_env,
job_iwd,
family_info,
NULL,
fds,
NULL,
nice_inc,
NULL,
job_opt_mask,
core_size_ptr,
affinity_mask,
NULL,
create_process_err_msg,
fs_remap,
rlimit_as_hard_limit);
*/
/*
JobPid = daemonCore->CreateProcessNew( JobName.c_str(),
args,
{
PRIV_USER_FINAL,
1,
FALSE,
FALSE,
&job_env,
job_iwd,
family_info,
NULL,
fds,
NULL,
nice_inc,
NULL,
job_opt_mask,
core_size_ptr,
affinity_mask,
NULL,
create_process_err_msg,
fs_remap,
rlimit_as_hard_limit
}
);
*/
OptionalCreateProcessArgs cpArgs(create_process_err_msg);
JobPid = daemonCore->CreateProcessNew( JobName.c_str(), args,
cpArgs.priv(PRIV_USER_FINAL)
.wantCommandPort(FALSE).wantUDPCommandPort(FALSE)
.env(&job_env).cwd(job_iwd).familyInfo(family_info)
.std(fds).niceInc(nice_inc).jobOptMask(job_opt_mask)
.coreHardLimit(core_size_ptr).affinityMask(affinity_mask)
.remap(fs_remap)
.asHardLimit(rlimit_as_hard_limit)
);
// Create_Process() saves the errno for us if it is an "interesting" error.
int create_process_errno = errno;
// This executes for the daemon core create-process logic
if ((FALSE == JobPid) && (0 != create_process_errno)) {
if (create_process_err_msg != "") create_process_err_msg += " ";
std::string errbuf;
formatstr(errbuf, "(errno=%d: '%s')", create_process_errno, strerror(create_process_errno));
create_process_err_msg += errbuf;
}
free( affinity_mask );
// now close the descriptors in fds array. our child has inherited
// them already, so we should close them so we do not leak descriptors.
// NOTE, we want to use a special method to close the starter's
// versions, if that's what we're using, so we don't think we've
// still got those available in other parts of the code for any
// reason.
for ( int i = 0; i <= 2; i++ ) {
if ( fds[i] >= 0 ) {
daemonCore->Close_FD ( fds[i] );
}
}
if ( JobPid == FALSE ) {
JobPid = -1;
if(!create_process_err_msg.empty()) {
// if the reason Create_Process failed was that registering
// a family with the ProcD failed, it is indicative of a
// problem regarding this execute machine, not the job. in
// this case, we'll want to EXCEPT instead of telling the
// Shadow to put the job on hold. there are probably other
// error conditions where EXCEPTing would be more appropriate
// as well...
//
if (create_process_errno == DaemonCore::ERRNO_REGISTRATION_FAILED) {
EXCEPT("Create_Process failed to register the job with the ProcD");
}
std::string err_msg = "Failed to execute '";
err_msg += JobName;
err_msg += "'";
if(!args_string.empty()) {
err_msg += " with arguments ";
err_msg += args_string.c_str();
}
err_msg += ": ";
err_msg += create_process_err_msg;
if( !ThisProcRunsAlongsideMainProc() ) {
Starter->jic->notifyStarterError( err_msg.c_str(),
true,
CONDOR_HOLD_CODE::FailedToCreateProcess,
create_process_errno );
}
}
dprintf(D_ALWAYS,"Create_Process(%s,%s, ...) failed: %s\n",
JobName.c_str(), args_string.c_str(), create_process_err_msg.c_str());
job_not_started = true;
return 0;
}
num_pids++;
dprintf(D_ALWAYS,"Create_Process succeeded, pid=%d\n",JobPid);
if (family_info->cgroup_active) {
this->cgroupActive = true;
}
condor_gettimestamp( job_start_time );
return 1;
}
bool
OsProc::JobReaper( int pid, int status )
{
dprintf( D_FULLDEBUG, "Inside OsProc::JobReaper()\n" );
if (JobPid == pid) {
// Only write ToE tags for the actual job process.
if(! ThisProcRunsAlongsideMainProc()) {
// Write the appropriate ToE tag if the process exited
// of its own accord.
if(! requested_exit) {
// Store for the post-script's environment.
this->howCode = ToE::OfItsOwnAccord;
// This ClassAd gets delete()d by toe when toe goes out of
// scope, because Insert() transfers ownership.
classad::ClassAd * tag = new classad::ClassAd();
tag->InsertAttr( "Who", ToE::itself );
tag->InsertAttr( "How", ToE::strings[ToE::OfItsOwnAccord] );
tag->InsertAttr( "HowCode", ToE::OfItsOwnAccord );
struct timeval exitTime;
condor_gettimestamp( exitTime );
tag->InsertAttr( "When", (long long)exitTime.tv_sec );
if(WIFSIGNALED(status)) {
tag->InsertAttr( ATTR_ON_EXIT_BY_SIGNAL, true );
tag->InsertAttr( ATTR_ON_EXIT_SIGNAL, WTERMSIG(status));
} else {
tag->InsertAttr( ATTR_ON_EXIT_BY_SIGNAL, false );
tag->InsertAttr( ATTR_ON_EXIT_CODE, WEXITSTATUS(status));
}
classad::ClassAd toe;
toe.Insert( ATTR_JOB_TOE, tag );
std::string jobAdFileName;
formatstr( jobAdFileName, "%s/.job.ad",
Starter->GetWorkingDir(0) );
ToE::writeTag( & toe, jobAdFileName );
// Update the schedd's copy of the job ad.
ClassAd updateAd( toe );
Starter->publishUpdateAd( & updateAd );
Starter->jic->periodicJobUpdate( & updateAd );
} else {
// If we didn't write a ToE, check to see if the startd did.
std::string jobAdFileName;
formatstr( jobAdFileName, "%s/.job.ad",
Starter->GetWorkingDir(0) );
FILE * f = safe_fopen_wrapper_follow( jobAdFileName.c_str(), "r" );
if(! f) {
dprintf( D_ALWAYS, "Failed to open .job.ad, can't forward ToE tag.\n" );
} else {
int error;
bool isEof;
classad::ClassAd jobAd;
if( InsertFromFile( f, jobAd, isEof, error ) ) {
classad::ClassAd * tag =
dynamic_cast<classad::ClassAd *>(jobAd.Lookup(ATTR_JOB_TOE));
if( tag ) {
// Store for the post-script's environment.
tag->EvaluateAttrInt( "HowCode", this->howCode );
// Don't let jobAd delete tag; toe will delete
// when it goes out of scope.
jobAd.Remove(ATTR_JOB_TOE);
classad::ClassAd toe;
toe.Insert(ATTR_JOB_TOE, tag );
// Update the schedd's copy of the job ad.
ClassAd updateAd( toe );
Starter->publishUpdateAd( & updateAd );
Starter->jic->periodicJobUpdate( & updateAd );
}
}
}
}
}
// clear out num_pids... everything under this process
// should now be gone
num_pids = 0;
// check to see if the job dropped a core file. if so, we
// want to rename that file so that it won't clobber other
// core files back at the submit site.
checkCoreFile();
}
return UserProc::JobReaper(pid, status);
}
bool
OsProc::JobExit( void )
{
int reason;
dprintf( D_FULLDEBUG, "Inside OsProc::JobExit()\n" );
if( requested_exit == true ) {
if( Starter->jic->hadHold() || Starter->jic->hadRemove() ) {
reason = JOB_KILLED;
} else {
reason = JOB_SHOULD_REQUEUE;
}
} else if( dumped_core ) {
reason = JOB_COREDUMPED;
} else if( job_not_started ) {
reason = JOB_NOT_STARTED;
} else {
reason = JOB_EXITED;
}
#if defined ( WIN32 )
priv_state old = set_user_priv ();
HANDLE user_token = priv_state_get_handle ();
ASSERT ( user_token );
// Check USE_VISIBLE_DESKTOP in condor_config. If set to TRUE,
// then removed our users priveleges from the visible desktop.
if (param_boolean_crufty("USE_VISIBLE_DESKTOP", false)) {
/* at this point we can revoke the user's access to the visible desktop */
RevokeDesktopAccess ( user_token );
}
set_priv ( old );
#endif
return Starter->jic->notifyJobExit( exit_status, reason, this );
}
void
OsProc::checkCoreFile( void )
{
// We rename any core file we find to core.<cluster>.<proc>.
// We add the core file if present to the list of output files
// (that happens in renameCoreFile)
// Since Linux now writes out "core.pid" by default, we should
// search for that. Try the JobPid of our first child:
std::string name_with_pid;
formatstr( name_with_pid, "core.%d", JobPid );
std::string new_name;
formatstr( new_name, "core.%d.%d",
Starter->jic->jobCluster(),
Starter->jic->jobProc() );
if( renameCoreFile(name_with_pid.c_str(), new_name.c_str()) ) {
// great, we found it, renameCoreFile() took care of
// everything we need to do... we're done.
return;
}
// Now, just see if there's a file called "core"
if( renameCoreFile("core", new_name.c_str()) ) {
return;
}
/*
maybe we should check for other possible pids (either by
using a Directory object to scan all the files in the iwd,
or by using the procfamily to test all the known pids under
the job). also, you can configure linux to drop core files
with other possible values, not just pid. however, it gets
complicated and it becomes harder and harder to tell if the
core files belong to this job or not in the case of a shared
file system where we might be running in a directory shared
by many jobs... for now, the above is good enough and will
catch most of the problems we're having.
*/
}
bool
OsProc::renameCoreFile( const char* old_name, const char* new_name )
{
bool rval = false;
int t_errno = 0;
std::string old_full;
std::string new_full;
const char* job_iwd = Starter->jic->jobIWD();
formatstr( old_full, "%s%c%s", job_iwd, DIR_DELIM_CHAR, old_name );
formatstr( new_full, "%s%c%s", job_iwd, DIR_DELIM_CHAR, new_name );
priv_state old_priv;
// we need to do this rename as the user...
errno = 0;
old_priv = set_user_priv();
int ret = rename(old_full.c_str(), new_full.c_str());
if( ret != 0 ) {
// rename failed
t_errno = errno; // grab errno right away
rval = false;
} else {
// rename succeeded
rval = true;
}
set_priv( old_priv );
if( rval ) {
dprintf( D_FULLDEBUG, "Found core file '%s', renamed to '%s'\n",
old_name, new_name );
if( dumped_core ) {
EXCEPT( "IMPOSSIBLE: inside OsProc::renameCoreFile and "
"dumped_core is already TRUE" );
}
dumped_core = true;
// make sure it'll get transfered back, too.
Starter->jic->addToOutputFiles( new_name );
} else if( t_errno != ENOENT ) {
dprintf( D_ALWAYS, "Failed to rename(%s,%s): errno %d (%s)\n",
old_full.c_str(), new_full.c_str(), t_errno,
strerror(t_errno) );
}
return rval;
}
void
OsProc::Suspend()
{
daemonCore->Send_Signal(JobPid, SIGSTOP);
is_suspended = true;
}
void
OsProc::Continue()
{
if (is_suspended)
{
daemonCore->Send_Signal(JobPid, SIGCONT);
is_suspended = false;
}
}
bool
OsProc::ShutdownGraceful()
{
if ( is_suspended ) {
Continue();
}
requested_exit = true;
if ( findRmKillSig(JobAd) != -1 ) {
daemonCore->Send_Signal(JobPid, rm_kill_sig);
} else {
bool sent = daemonCore->Send_Signal(JobPid, soft_kill_sig);
if (!sent) {
dprintf(D_ALWAYS, "Send (softkill) signal failed, retrying...\n");
sleep(1);
sent = daemonCore->Send_Signal(JobPid, soft_kill_sig);
if (!sent) {
dprintf(D_ALWAYS, "Send (softkill) signal failed twice, hardkill will fire after timeout\n");
} else {
dprintf(D_ALWAYS, "Send (softkill) signal worked the second time\n");
}
}
}
return false; // return false says shutdown is pending
}
bool
OsProc::ShutdownFast()
{
// We purposely do not do a SIGCONT here, since there is no sense
// in potentially swapping the job back into memory if our next
// step is to hard kill it.
requested_exit = true;
daemonCore->Send_Signal(JobPid, SIGKILL);
return false; // return false says shutdown is pending
}
bool
OsProc::Remove()
{
if ( is_suspended ) {
Continue();
}
requested_exit = true;
daemonCore->Send_Signal(JobPid, rm_kill_sig);