Skip to content

Commit c203a91

Browse files
singh264tajilaJasonFengJ9keithc-ca
committed
Address comments
Co-authored-by: Tobi Ajila <[email protected]> Co-authored-by: Jason Feng <[email protected]> Co-authored-by: Keith W. Campbell <[email protected]> Signed-off-by: Amarpreet Singh <[email protected]>
1 parent aa9cb9d commit c203a91

File tree

1 file changed

+55
-104
lines changed
  • src/java.base/share/native/libjli

1 file changed

+55
-104
lines changed

src/java.base/share/native/libjli/java.c

+55-104
Original file line numberDiff line numberDiff line change
@@ -77,48 +77,6 @@
7777

7878
#define USE_STDERR JNI_TRUE /* we usually print to stderr */
7979
#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) */
12280

12381
static jboolean printVersion = JNI_FALSE; /* print and exit */
12482
static jboolean showVersion = JNI_FALSE; /* print but continue */
@@ -625,7 +583,7 @@ getCommandLineOptionValue(const char *optionName, int argc, char **argv, int *er
625583
}
626584
}
627585
if (!optionNameFound) {
628-
*error = OPTION_NAME_NOT_FOUND_ERROR;
586+
*error = -1;
629587
}
630588
return value;
631589
}
@@ -641,29 +599,18 @@ static const char *
641599
getCheckpointDirectory(int argc, char **argv, int *error)
642600
{
643601
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);
645603
if (0 == *error) {
646604
if (NULL != checkpointDirectoryPropertyValue) {
647605
checkpointDirectory = checkpointDirectoryPropertyValue;
648606
} 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;
651609
}
652610
}
653611
return checkpointDirectory;
654612
}
655613

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-
667614
/**
668615
* Get the log level specified in the command line arguments.
669616
* @param[in] argc The number of command line arguments
@@ -676,8 +623,8 @@ getLogLevel(int argc, char **argv, int *error)
676623
{
677624
int logLevelValue = 2; /* default */
678625
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) {
681628
const char *c = logLevelPropertyValue;
682629
if (NULL == c) {
683630
goto done;
@@ -693,10 +640,12 @@ getLogLevel(int argc, char **argv, int *error)
693640
} else {
694641
goto setLogLevelOptionValueNotValidError;
695642
}
643+
} else if (-1 == *error) {
644+
goto done;
696645
}
697646
setLogLevelOptionValueNotValidError:
698-
JLI_ReportErrorMessage(OPTION_VALUE_NOT_VALID_ERROR_MESSAGE, LOG_LEVEL_OPTION_NAME, logLevelPropertyValue);
699-
*error = OPTION_VALUE_NOT_VALID_ERROR;
647+
JLI_ReportErrorMessage("The value of the command line option is not valid, option=-Dopenj9.internal.criu.logLevel, value=%s.", logLevelPropertyValue);
648+
*error = -2;
700649
done:
701650
return logLevelValue;
702651
}
@@ -712,13 +661,13 @@ static jboolean
712661
isUnprivilegedModeOn(int argc, char **argv, int *error)
713662
{
714663
jboolean isUnprivilegedModeOn = JNI_FALSE;
715-
const char *unprivilegedModePropertyValue = getCommandLineOptionValue(UNPRIVILEGED_MODE_OPTION_NAME, argc, argv, error);
664+
const char *unprivilegedModePropertyValue = getCommandLineOptionValue("-Dopenj9.internal.criu.unprivilegedMode", argc, argv, error);
716665
if (0 == *error) {
717666
if (NULL == unprivilegedModePropertyValue) {
718667
isUnprivilegedModeOn = JNI_TRUE;
719668
} else {
720-
JLI_ReportErrorMessage(OPTION_VALUE_NOT_EXPECTED_ERROR_MESSAGE, UNPRIVILEGED_MODE_OPTION_NAME, unprivilegedModePropertyValue);
721-
*error = OPTION_VALUE_NOT_EXPECTED_ERROR;
669+
JLI_ReportErrorMessage("The value of the command line option is not expected, option=-Dopenj9.internal.criu.unprivilegedMode, value=%s.", unprivilegedModePropertyValue);
670+
*error = -2;
722671
}
723672
}
724673
return isUnprivilegedModeOn;
@@ -735,13 +684,13 @@ static const char *
735684
getLogFile(int argc, char **argv, int *error)
736685
{
737686
const char *logFile = NULL;
738-
const char *logFilePropertyValue = getCommandLineOptionValue(LOG_FILE_OPTION_NAME, argc, argv, error);
687+
const char *logFilePropertyValue = getCommandLineOptionValue("-Dopenj9.internal.criu.logFile", argc, argv, error);
739688
if (0 == *error) {
740689
if (NULL != logFilePropertyValue) {
741690
logFile = logFilePropertyValue;
742691
} else {
743-
JLI_ReportErrorMessage(OPTION_VALUE_NOT_FOUND_ERROR_MESSAGE, LOG_FILE_OPTION_NAME);
744-
*error = OPTION_VALUE_NOT_FOUND_ERROR;
692+
JLI_ReportErrorMessage("The value of the command line option -Dopenj9.internal.criu.logFile was not found.");
693+
*error = -2;
745694
}
746695
}
747696
return logFile;
@@ -753,73 +702,73 @@ getLogFile(int argc, char **argv, int *error)
753702
* @param[in] logLevel The log level for CRIU logging
754703
* @param[in] unprivilegedModeOn Indicates whether the unprivileged mode option is on
755704
* @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
705+
* @return 0 if the execution of the 'criu restore' command succeeds, -1 otherwise
757706
*/
758707
static int
759708
restoreFromCheckpoint(const char *checkpointDirectory, int logLevel, jboolean unprivilegedModeOn, const char *logFile)
760709
{
761-
int restoreStatus = EXECVP_SUCCESS;
710+
int restoreStatus = 0;
762711
int length = -1;
763712
char *logLevelOption = NULL;
764713
char *logFileOption = NULL;
765714
int argc = 0;
766715
const char *argv[9] = { NULL };
767-
argv[argc++] = CRIU_COMMAND;
768-
argv[argc++] = CRIU_RESTORE_OPTION;
769-
argv[argc++] = CRIU_CHECKPOINT_DIRECTORY_OPTION;
716+
argv[argc++] = "criu";
717+
argv[argc++] = "restore";
718+
argv[argc++] = "-D";
770719
argv[argc++] = checkpointDirectory;
771-
length = snprintf(NULL, 0, LOG_LEVEL_OPTION_FORMAT, logLevel);
720+
length = snprintf(NULL, 0, "-v%d", logLevel);
772721
if (length < 0) {
773722
char logLevelString[2] = { 0 };
774723
sprintf(logLevelString, "%d", logLevel);
775-
JLI_ReportErrorMessage(COMMAND_OPTION_LENGTH_CALCULATION_ERROR_MESSAGE, logLevelString, LOG_LEVEL_OPTION_FORMAT);
776-
restoreStatus = MEMORY_ALLOCATION_ERROR;
724+
JLI_ReportErrorMessage("Failed to calculate the length of the command option, value=%s, format=%s.", logLevelString, "-v%d");
725+
restoreStatus = -1;
777726
goto done;
778727
}
779728
logLevelOption = (char *)JLI_MemAlloc(length + 1);
780729
if (NULL == logLevelOption) {
781730
char logLevelString[2] = { 0 };
782731
sprintf(logLevelString, "%d", logLevel);
783-
JLI_ReportErrorMessage(COMMAND_OPTION_MEMORY_ALLOCATION_ERROR_MESSAGE, logLevelString, LOG_LEVEL_OPTION_FORMAT);
784-
restoreStatus = MEMORY_ALLOCATION_ERROR;
732+
JLI_ReportErrorMessage("Failed to allocate memory for the command option, value=%s, format=%s.", logLevelString, "-v%d");
733+
restoreStatus = -1;
785734
goto done;
786735
}
787-
if (snprintf(logLevelOption, length + 1, LOG_LEVEL_OPTION_FORMAT, logLevel) < 0) {
736+
if (snprintf(logLevelOption, length + 1, "-v%d", logLevel) < 0) {
788737
char logLevelString[2] = { 0 };
789738
sprintf(logLevelString, "%d", logLevel);
790-
JLI_ReportErrorMessage(COMMAND_OPTION_MEMORY_ALLOCATION_ERROR_MESSAGE, logLevelString, LOG_LEVEL_OPTION_FORMAT);
791-
restoreStatus = MEMORY_ALLOCATION_ERROR;
739+
JLI_ReportErrorMessage("Failed to allocate memory for the command option, value=%s, format=%s.", logLevelString, "-v%d");
740+
restoreStatus = -1;
792741
goto freeLogLevelOption;
793742
}
794743
argv[argc++] = logLevelOption;
795-
argv[argc++] = CRIU_SHELL_JOB_OPTION;
744+
argv[argc++] = "--shell-job";
796745
if (unprivilegedModeOn) {
797-
argv[argc++] = CRIU_UNPRIVILEGED_MODE_OPTION;
746+
argv[argc++] = "--unprivileged";
798747
}
799748
if (NULL != logFile) {
800-
length = snprintf(NULL, 0, LOG_FILE_OPTION_FORMAT, logFile);
749+
length = snprintf(NULL, 0, "--log-file=%s", logFile);
801750
if (length < 0) {
802-
JLI_ReportErrorMessage(COMMAND_OPTION_LENGTH_CALCULATION_ERROR_MESSAGE, logFile, LOG_FILE_OPTION_FORMAT);
803-
restoreStatus = MEMORY_ALLOCATION_ERROR;
751+
JLI_ReportErrorMessage("Failed to calculate the length of the command option, value=%s, format=%s.", logFile, "--log-file=%s");
752+
restoreStatus = -1;
804753
goto freeLogLevelOption;
805754
}
806755
logFileOption = (char *)JLI_MemAlloc(length + 1);
807756
if (NULL == logFileOption) {
808-
JLI_ReportErrorMessage(COMMAND_OPTION_MEMORY_ALLOCATION_ERROR_MESSAGE, logFile, LOG_FILE_OPTION_FORMAT);
809-
restoreStatus = MEMORY_ALLOCATION_ERROR;
757+
JLI_ReportErrorMessage("Failed to allocate memory for the command option, value=%s, format=%s.", logFile, "--log-file=%s");
758+
restoreStatus = -1;
810759
goto freeLogLevelOption;
811760
}
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;
761+
if (snprintf(logFileOption, length + 1, "--log-file=%s", logFile) < 0) {
762+
JLI_ReportErrorMessage("Failed to allocate memory for the command option, value=%s, format=%s.", logFile, "--log-file=%s");
763+
restoreStatus = -1;
815764
goto freeLogFileOption;
816765
}
817766
argv[argc++] = logFileOption;
818767
}
819768
argv[argc] = NULL;
820769
execvp(argv[0], (char * const *)argv);
821770
/* If execvp returns, there was an error. */
822-
restoreStatus = EXECVP_ERROR;
771+
restoreStatus = -1;
823772
freeLogFileOption:
824773
if (logFileOption != NULL) {
825774
JLI_MemFree((void *)logFileOption);
@@ -853,11 +802,11 @@ handleCRaCRestore(int argc, char **argv)
853802
int childProcessPidStatus = 0;
854803
int childProcessPidExitStatus = 0;
855804
checkpointDirectory = getCheckpointDirectory(argc, argv, &error);
856-
if (OPTION_NAME_NOT_FOUND_ERROR == error) {
805+
if (-1 == error) {
857806
return;
858807
}
859-
if (0 != error) {
860-
JLI_ReportErrorMessage(CHECKPOINT_DIRECTORY_ERROR_MESSAGE, error);
808+
if (-2 == error) {
809+
JLI_ReportErrorMessage("Failed to get the CRIU checkpoint directory, error=%d.", error);
861810
parentProcessExitStatus = EXIT_FAILURE;
862811
goto doneParentProcess;
863812
}
@@ -868,36 +817,38 @@ handleCRaCRestore(int argc, char **argv)
868817
childProcessPid = fork();
869818
if (0 == childProcessPid) {
870819
logLevel = getLogLevel(argc, argv, &error);
871-
if (isCommandLineOptionFoundWithError(error)) {
872-
JLI_ReportErrorMessage(LOG_LEVEL_ERROR_MESSAGE, error);
820+
if (-2 == error) {
821+
JLI_ReportErrorMessage("Failed to get the CRIU log level, error=%d.", error);
873822
childProcessExitStatus = EXIT_FAILURE;
874823
goto doneChildProcess;
875824
}
876825
unprivilegedModeOn = isUnprivilegedModeOn(argc, argv, &error);
877-
if (isCommandLineOptionFoundWithError(error)) {
878-
JLI_ReportErrorMessage(UNPRIVILEGED_MODE_ERROR_MESSAGE, error);
826+
if (-2 == error) {
827+
JLI_ReportErrorMessage("Failed to get the CRIU unprivileged mode, error=%d.", error);
879828
childProcessExitStatus = EXIT_FAILURE;
880829
goto doneChildProcess;
881830
}
882831
logFile = getLogFile(argc, argv, &error);
883-
if (isCommandLineOptionFoundWithError(error)) {
884-
JLI_ReportErrorMessage(LOG_FILE_ERROR_MESSAGE, error);
832+
if (-2 == error) {
833+
JLI_ReportErrorMessage("Failed to get the CRIU log file, error=%d.", error);
885834
childProcessExitStatus = EXIT_FAILURE;
886835
goto doneChildProcess;
887836
}
888837
childProcessExitStatus = restoreFromCheckpoint(checkpointDirectory, logLevel, unprivilegedModeOn, logFile);
889838
doneChildProcess:
890839
exit(childProcessExitStatus);
891840
} else {
892-
waitpid(childProcessPid, &childProcessPidStatus, WAIT_INDEFINITELY);
841+
waitpid(childProcessPid, &childProcessPidStatus, 0);
893842
if (WIFEXITED(childProcessPidStatus)) {
894843
childProcessPidExitStatus = WEXITSTATUS(childProcessPidStatus);
895-
if (EXIT_SUCCESS != childProcessPidExitStatus) {
896-
JLI_ReportErrorMessage(RESTORE_FROM_CHECKPOINT_ERROR_MESSAGE, childProcessPidExitStatus);
844+
if (EXIT_SUCCESS == childProcessPidExitStatus) {
845+
JLI_ReportMessage("Completed restore with -XX:CRaCRestoreFrom=PATH.");
846+
} else {
847+
JLI_ReportErrorMessage("Failed to restore from checkpoint, error=%d.", childProcessPidExitStatus);
897848
parentProcessExitStatus = EXIT_FAILURE;
898849
}
899850
} else {
900-
JLI_ReportErrorMessage(RESTORE_CHILD_PROCESS_FAILED_ERROR_MESSAGE);
851+
JLI_ReportErrorMessage("The CRIU restore child process failed.");
901852
parentProcessExitStatus = EXIT_FAILURE;
902853
}
903854
}

0 commit comments

Comments
 (0)