77
77
78
78
#define USE_STDERR JNI_TRUE /* we usually print to stderr */
79
79
#define USE_STDOUT JNI_FALSE
80
- #if defined(J9VM_OPT_CRAC_SUPPORT )
81
- /* The return codes indicating errors, successful execution, or specific conditions. */
82
- #define EXECVP_ERROR -1
83
- #define EXECVP_SUCCESS 0
84
- #define WAIT_INDEFINITELY 0
85
- #define MEMORY_ALLOCATION_ERROR 1
86
- #define OPTION_NAME_NOT_FOUND_ERROR 2
87
- #define OPTION_VALUE_NOT_FOUND_ERROR 3
88
- #define OPTION_VALUE_NOT_VALID_ERROR 4
89
- #define OPTION_VALUE_NOT_EXPECTED_ERROR 5
90
- #define COMMAND_OPTION_LENGTH_CALCULATION_ERROR 6
91
-
92
- /* The error messages. */
93
- #define OPTION_VALUE_NOT_FOUND_ERROR_MESSAGE "The value of the command line option %s was not found."
94
- #define OPTION_VALUE_NOT_VALID_ERROR_MESSAGE "The value of the command line option is not valid, option=%s, value=%s."
95
- #define OPTION_VALUE_NOT_EXPECTED_ERROR_MESSAGE "The value of the command line option is not expected, option=%s, value=%s."
96
- #define CHECKPOINT_DIRECTORY_ERROR_MESSAGE "Failed to get the CRIU checkpoint directory, error=%d."
97
- #define LOG_LEVEL_ERROR_MESSAGE "Failed to get the CRIU log level, error=%d."
98
- #define UNPRIVILEGED_MODE_ERROR_MESSAGE "Failed to get the CRIU unprivileged mode, error=%d."
99
- #define LOG_FILE_ERROR_MESSAGE "Failed to get the CRIU log file, error=%d."
100
- #define COMMAND_OPTION_LENGTH_CALCULATION_ERROR_MESSAGE "Failed to calculate the length of the command option, value=%s, format=%s."
101
- #define COMMAND_OPTION_MEMORY_ALLOCATION_ERROR_MESSAGE "Failed to allocate memory for the command option, value=%s, format=%s."
102
- #define RESTORE_FROM_CHECKPOINT_ERROR_MESSAGE "Failed to restore from checkpoint, error=%d."
103
- #define RESTORE_CHILD_PROCESS_FAILED_ERROR_MESSAGE "The CRIU restore child process failed."
104
-
105
- /* The option names. */
106
- #define CRAC_RESTORE_FROM_OPTION_NAME "-XX:CRaCRestoreFrom"
107
- #define LOG_LEVEL_OPTION_NAME "-Dopenj9.internal.criu.logLevel"
108
- #define UNPRIVILEGED_MODE_OPTION_NAME "-Dopenj9.internal.criu.unprivilegedMode"
109
- #define LOG_FILE_OPTION_NAME "-Dopenj9.internal.criu.logFile"
110
-
111
- /* The option formats. */
112
- #define LOG_LEVEL_OPTION_FORMAT "-v%d"
113
- #define LOG_FILE_OPTION_FORMAT "--log-file=%s"
114
-
115
- /* The CRIU command options. */
116
- #define CRIU_COMMAND "criu"
117
- #define CRIU_RESTORE_OPTION "restore"
118
- #define CRIU_CHECKPOINT_DIRECTORY_OPTION "-D"
119
- #define CRIU_SHELL_JOB_OPTION "--shell-job"
120
- #define CRIU_UNPRIVILEGED_MODE_OPTION "--unprivileged"
121
- #endif /* defined(J9VM_OPT_CRAC_SUPPORT) */
122
80
123
81
static jboolean printVersion = JNI_FALSE ; /* print and exit */
124
82
static jboolean showVersion = JNI_FALSE ; /* print but continue */
@@ -625,7 +583,7 @@ getCommandLineOptionValue(const char *optionName, int argc, char **argv, int *er
625
583
}
626
584
}
627
585
if (!optionNameFound ) {
628
- * error = OPTION_NAME_NOT_FOUND_ERROR ;
586
+ * error = -1 ;
629
587
}
630
588
return value ;
631
589
}
@@ -641,29 +599,18 @@ static const char *
641
599
getCheckpointDirectory (int argc , char * * argv , int * error )
642
600
{
643
601
const char * checkpointDirectory = NULL ;
644
- const char * checkpointDirectoryPropertyValue = getCommandLineOptionValue (CRAC_RESTORE_FROM_OPTION_NAME , argc , argv , error );
602
+ const char * checkpointDirectoryPropertyValue = getCommandLineOptionValue ("-XX:CRaCRestoreFrom" , argc , argv , error );
645
603
if (0 == * error ) {
646
604
if (NULL != checkpointDirectoryPropertyValue ) {
647
605
checkpointDirectory = checkpointDirectoryPropertyValue ;
648
606
} else {
649
- JLI_ReportErrorMessage (OPTION_VALUE_NOT_FOUND_ERROR_MESSAGE , CRAC_RESTORE_FROM_OPTION_NAME );
650
- * error = OPTION_VALUE_NOT_FOUND_ERROR ;
607
+ JLI_ReportErrorMessage ("The value of the command line option -XX:CRaCRestoreFrom was not found." );
608
+ * error = -2 ;
651
609
}
652
610
}
653
611
return checkpointDirectory ;
654
612
}
655
613
656
- /**
657
- * Check if a command line option is found with an error condition.
658
- * @param[in] error The error code of the result of obtaining the command line option
659
- * @return true if the command line option is found and an error occurred, false otherwise
660
- */
661
- static jboolean
662
- isCommandLineOptionFoundWithError (int error )
663
- {
664
- return (OPTION_NAME_NOT_FOUND_ERROR != error ) && (0 != error );
665
- }
666
-
667
614
/**
668
615
* Get the log level specified in the command line arguments.
669
616
* @param[in] argc The number of command line arguments
@@ -676,8 +623,8 @@ getLogLevel(int argc, char **argv, int *error)
676
623
{
677
624
int logLevelValue = 2 ; /* default */
678
625
const char * logLevelPropertyValue = NULL ;
679
- logLevelPropertyValue = getCommandLineOptionValue (LOG_LEVEL_OPTION_NAME , argc , argv , error );
680
- if (! isCommandLineOptionFoundWithError ( * error ) ) {
626
+ logLevelPropertyValue = getCommandLineOptionValue ("-Dopenj9.internal.criu.logLevel" , argc , argv , error );
627
+ if (0 == * error ) {
681
628
const char * c = logLevelPropertyValue ;
682
629
if (NULL == c ) {
683
630
goto done ;
@@ -695,8 +642,8 @@ getLogLevel(int argc, char **argv, int *error)
695
642
}
696
643
}
697
644
setLogLevelOptionValueNotValidError :
698
- JLI_ReportErrorMessage (OPTION_VALUE_NOT_VALID_ERROR_MESSAGE , LOG_LEVEL_OPTION_NAME , logLevelPropertyValue );
699
- * error = OPTION_VALUE_NOT_VALID_ERROR ;
645
+ JLI_ReportErrorMessage ("The value of the command line option is not valid, option=-Dopenj9.internal.criu.logLevel, value=%s." , logLevelPropertyValue );
646
+ * error = -2 ;
700
647
done :
701
648
return logLevelValue ;
702
649
}
@@ -712,13 +659,13 @@ static jboolean
712
659
isUnprivilegedModeOn (int argc , char * * argv , int * error )
713
660
{
714
661
jboolean isUnprivilegedModeOn = JNI_FALSE ;
715
- const char * unprivilegedModePropertyValue = getCommandLineOptionValue (UNPRIVILEGED_MODE_OPTION_NAME , argc , argv , error );
662
+ const char * unprivilegedModePropertyValue = getCommandLineOptionValue ("-Dopenj9.internal.criu.unprivilegedMode" , argc , argv , error );
716
663
if (0 == * error ) {
717
664
if (NULL == unprivilegedModePropertyValue ) {
718
665
isUnprivilegedModeOn = JNI_TRUE ;
719
666
} else {
720
- JLI_ReportErrorMessage (OPTION_VALUE_NOT_EXPECTED_ERROR_MESSAGE , UNPRIVILEGED_MODE_OPTION_NAME , unprivilegedModePropertyValue );
721
- * error = OPTION_VALUE_NOT_EXPECTED_ERROR ;
667
+ JLI_ReportErrorMessage ("The value of the command line option is not expected, option=-Dopenj9.internal.criu.unprivilegedMode, value=%s." , unprivilegedModePropertyValue );
668
+ * error = -2 ;
722
669
}
723
670
}
724
671
return isUnprivilegedModeOn ;
@@ -735,13 +682,13 @@ static const char *
735
682
getLogFile (int argc , char * * argv , int * error )
736
683
{
737
684
const char * logFile = NULL ;
738
- const char * logFilePropertyValue = getCommandLineOptionValue (LOG_FILE_OPTION_NAME , argc , argv , error );
685
+ const char * logFilePropertyValue = getCommandLineOptionValue ("-Dopenj9.internal.criu.logFile" , argc , argv , error );
739
686
if (0 == * error ) {
740
687
if (NULL != logFilePropertyValue ) {
741
688
logFile = logFilePropertyValue ;
742
689
} else {
743
- JLI_ReportErrorMessage (OPTION_VALUE_NOT_FOUND_ERROR_MESSAGE , LOG_FILE_OPTION_NAME );
744
- * error = OPTION_VALUE_NOT_FOUND_ERROR ;
690
+ JLI_ReportErrorMessage ("The value of the command line option -Dopenj9.internal.criu.logFile was not found." );
691
+ * error = -2 ;
745
692
}
746
693
}
747
694
return logFile ;
@@ -753,73 +700,73 @@ getLogFile(int argc, char **argv, int *error)
753
700
* @param[in] logLevel The log level for CRIU logging
754
701
* @param[in] unprivilegedModeOn Indicates whether the unprivileged mode option is on
755
702
* @param[in] logFile The log file option for CRIU
756
- * @return EXECVP_SUCCESS if the execution of the 'criu restore' command succeeds, error code otherwise
703
+ * @return 0 if the execution of the 'criu restore' command succeeds, -1 otherwise
757
704
*/
758
705
static int
759
706
restoreFromCheckpoint (const char * checkpointDirectory , int logLevel , jboolean unprivilegedModeOn , const char * logFile )
760
707
{
761
- int restoreStatus = EXECVP_SUCCESS ;
708
+ int restoreStatus = 0 ;
762
709
int length = -1 ;
763
710
char * logLevelOption = NULL ;
764
711
char * logFileOption = NULL ;
765
712
int argc = 0 ;
766
713
const char * argv [9 ] = { NULL };
767
- argv [argc ++ ] = CRIU_COMMAND ;
768
- argv [argc ++ ] = CRIU_RESTORE_OPTION ;
769
- argv [argc ++ ] = CRIU_CHECKPOINT_DIRECTORY_OPTION ;
714
+ argv [argc ++ ] = "criu" ;
715
+ argv [argc ++ ] = "restore" ;
716
+ argv [argc ++ ] = "-D" ;
770
717
argv [argc ++ ] = checkpointDirectory ;
771
- length = snprintf (NULL , 0 , LOG_LEVEL_OPTION_FORMAT , logLevel );
718
+ length = snprintf (NULL , 0 , "-v%d" , logLevel );
772
719
if (length < 0 ) {
773
720
char logLevelString [2 ] = { 0 };
774
721
sprintf (logLevelString , "%d" , logLevel );
775
- JLI_ReportErrorMessage (COMMAND_OPTION_LENGTH_CALCULATION_ERROR_MESSAGE , logLevelString , LOG_LEVEL_OPTION_FORMAT );
776
- restoreStatus = MEMORY_ALLOCATION_ERROR ;
722
+ JLI_ReportErrorMessage ("Failed to calculate the length of the command option, value=%s, format=%s." , logLevelString , "-v%d" );
723
+ restoreStatus = -1 ;
777
724
goto done ;
778
725
}
779
726
logLevelOption = (char * )JLI_MemAlloc (length + 1 );
780
727
if (NULL == logLevelOption ) {
781
728
char logLevelString [2 ] = { 0 };
782
729
sprintf (logLevelString , "%d" , logLevel );
783
- JLI_ReportErrorMessage (COMMAND_OPTION_MEMORY_ALLOCATION_ERROR_MESSAGE , logLevelString , LOG_LEVEL_OPTION_FORMAT );
784
- restoreStatus = MEMORY_ALLOCATION_ERROR ;
730
+ JLI_ReportErrorMessage ("Failed to allocate memory for the command option, value=%s, format=%s." , logLevelString , "-v%d" );
731
+ restoreStatus = -1 ;
785
732
goto done ;
786
733
}
787
- if (snprintf (logLevelOption , length + 1 , LOG_LEVEL_OPTION_FORMAT , logLevel ) < 0 ) {
734
+ if (snprintf (logLevelOption , length + 1 , "-v%d" , logLevel ) < 0 ) {
788
735
char logLevelString [2 ] = { 0 };
789
736
sprintf (logLevelString , "%d" , logLevel );
790
- JLI_ReportErrorMessage (COMMAND_OPTION_MEMORY_ALLOCATION_ERROR_MESSAGE , logLevelString , LOG_LEVEL_OPTION_FORMAT );
791
- restoreStatus = MEMORY_ALLOCATION_ERROR ;
737
+ JLI_ReportErrorMessage ("Failed to allocate memory for the command option, value=%s, format=%s." , logLevelString , "-v%d" );
738
+ restoreStatus = -1 ;
792
739
goto freeLogLevelOption ;
793
740
}
794
741
argv [argc ++ ] = logLevelOption ;
795
- argv [argc ++ ] = CRIU_SHELL_JOB_OPTION ;
742
+ argv [argc ++ ] = "--shell-job" ;
796
743
if (unprivilegedModeOn ) {
797
- argv [argc ++ ] = CRIU_UNPRIVILEGED_MODE_OPTION ;
744
+ argv [argc ++ ] = "--unprivileged" ;
798
745
}
799
746
if (NULL != logFile ) {
800
- length = snprintf (NULL , 0 , LOG_FILE_OPTION_FORMAT , logFile );
747
+ length = snprintf (NULL , 0 , "--log-file=%s" , logFile );
801
748
if (length < 0 ) {
802
- JLI_ReportErrorMessage (COMMAND_OPTION_LENGTH_CALCULATION_ERROR_MESSAGE , logFile , LOG_FILE_OPTION_FORMAT );
803
- restoreStatus = MEMORY_ALLOCATION_ERROR ;
749
+ JLI_ReportErrorMessage ("Failed to calculate the length of the command option, value=%s, format=%s." , logFile , "--log-file=%s" );
750
+ restoreStatus = -1 ;
804
751
goto freeLogLevelOption ;
805
752
}
806
753
logFileOption = (char * )JLI_MemAlloc (length + 1 );
807
754
if (NULL == logFileOption ) {
808
- JLI_ReportErrorMessage (COMMAND_OPTION_MEMORY_ALLOCATION_ERROR_MESSAGE , logFile , LOG_FILE_OPTION_FORMAT );
809
- restoreStatus = MEMORY_ALLOCATION_ERROR ;
755
+ JLI_ReportErrorMessage ("Failed to allocate memory for the command option, value=%s, format=%s." , logFile , "--log-file=%s" );
756
+ restoreStatus = -1 ;
810
757
goto freeLogLevelOption ;
811
758
}
812
- if (snprintf (logFileOption , length + 1 , LOG_FILE_OPTION_FORMAT , logFile ) < 0 ) {
813
- JLI_ReportErrorMessage (COMMAND_OPTION_MEMORY_ALLOCATION_ERROR_MESSAGE , logFile , LOG_FILE_OPTION_FORMAT );
814
- restoreStatus = MEMORY_ALLOCATION_ERROR ;
759
+ if (snprintf (logFileOption , length + 1 , "--log-file=%s" , logFile ) < 0 ) {
760
+ JLI_ReportErrorMessage ("Failed to allocate memory for the command option, value=%s, format=%s." , logFile , "--log-file=%s" );
761
+ restoreStatus = -1 ;
815
762
goto freeLogFileOption ;
816
763
}
817
764
argv [argc ++ ] = logFileOption ;
818
765
}
819
766
argv [argc ] = NULL ;
820
767
execvp (argv [0 ], (char * const * )argv );
821
768
/* If execvp returns, there was an error. */
822
- restoreStatus = EXECVP_ERROR ;
769
+ restoreStatus = -1 ;
823
770
freeLogFileOption :
824
771
if (logFileOption != NULL ) {
825
772
JLI_MemFree ((void * )logFileOption );
@@ -853,11 +800,11 @@ handleCRaCRestore(int argc, char **argv)
853
800
int childProcessPidStatus = 0 ;
854
801
int childProcessPidExitStatus = 0 ;
855
802
checkpointDirectory = getCheckpointDirectory (argc , argv , & error );
856
- if (OPTION_NAME_NOT_FOUND_ERROR == error ) {
803
+ if (-1 == error ) {
857
804
return ;
858
805
}
859
- if (0 ! = error ) {
860
- JLI_ReportErrorMessage (CHECKPOINT_DIRECTORY_ERROR_MESSAGE , error );
806
+ if (-2 = = error ) {
807
+ JLI_ReportErrorMessage ("Failed to get the CRIU checkpoint directory, error=%d." , error );
861
808
parentProcessExitStatus = EXIT_FAILURE ;
862
809
goto doneParentProcess ;
863
810
}
@@ -868,36 +815,36 @@ handleCRaCRestore(int argc, char **argv)
868
815
childProcessPid = fork ();
869
816
if (0 == childProcessPid ) {
870
817
logLevel = getLogLevel (argc , argv , & error );
871
- if (isCommandLineOptionFoundWithError ( error ) ) {
872
- JLI_ReportErrorMessage (LOG_LEVEL_ERROR_MESSAGE , error );
818
+ if (-2 == error ) {
819
+ JLI_ReportErrorMessage ("Failed to get the CRIU log level, error=%d." , error );
873
820
childProcessExitStatus = EXIT_FAILURE ;
874
821
goto doneChildProcess ;
875
822
}
876
823
unprivilegedModeOn = isUnprivilegedModeOn (argc , argv , & error );
877
- if (isCommandLineOptionFoundWithError ( error ) ) {
878
- JLI_ReportErrorMessage (UNPRIVILEGED_MODE_ERROR_MESSAGE , error );
824
+ if (-2 == error ) {
825
+ JLI_ReportErrorMessage ("Failed to get the CRIU unprivileged mode, error=%d." , error );
879
826
childProcessExitStatus = EXIT_FAILURE ;
880
827
goto doneChildProcess ;
881
828
}
882
829
logFile = getLogFile (argc , argv , & error );
883
- if (isCommandLineOptionFoundWithError ( error ) ) {
884
- JLI_ReportErrorMessage (LOG_FILE_ERROR_MESSAGE , error );
830
+ if (-2 == error ) {
831
+ JLI_ReportErrorMessage ("Failed to get the CRIU log file, error=%d." , error );
885
832
childProcessExitStatus = EXIT_FAILURE ;
886
833
goto doneChildProcess ;
887
834
}
888
835
childProcessExitStatus = restoreFromCheckpoint (checkpointDirectory , logLevel , unprivilegedModeOn , logFile );
889
836
doneChildProcess :
890
837
exit (childProcessExitStatus );
891
838
} else {
892
- waitpid (childProcessPid , & childProcessPidStatus , WAIT_INDEFINITELY );
839
+ waitpid (childProcessPid , & childProcessPidStatus , 0 );
893
840
if (WIFEXITED (childProcessPidStatus )) {
894
841
childProcessPidExitStatus = WEXITSTATUS (childProcessPidStatus );
895
842
if (EXIT_SUCCESS != childProcessPidExitStatus ) {
896
- JLI_ReportErrorMessage (RESTORE_FROM_CHECKPOINT_ERROR_MESSAGE , childProcessPidExitStatus );
843
+ JLI_ReportErrorMessage ("Failed to restore from checkpoint, error=%d." , childProcessPidExitStatus );
897
844
parentProcessExitStatus = EXIT_FAILURE ;
898
845
}
899
846
} else {
900
- JLI_ReportErrorMessage (RESTORE_CHILD_PROCESS_FAILED_ERROR_MESSAGE );
847
+ JLI_ReportErrorMessage ("The CRIU restore child process failed." );
901
848
parentProcessExitStatus = EXIT_FAILURE ;
902
849
}
903
850
}
0 commit comments