Init commit of jobs output/error message peek project.

This commit is contained in:
Lei Chen 2026-05-09 16:57:54 +08:00
parent c772d7e601
commit d2b47e3985
38 changed files with 2267 additions and 21 deletions

View file

@ -1,3 +1,4 @@
#include "client.h"
#include "controller_connector.h"
#include "netio.h"
#include "uv_ipc.h"
@ -11,6 +12,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <windows.h>
@ -1097,6 +1099,89 @@ static int handle_runtasks(xce_request_t* request)
return 0;
}
/**
* @brief Handles the fetchfile request from the controller.
*
* Reads up to msg->max_bytes bytes from msg->filepath starting at msg->offset
* and sends the data back to the controller as an OPCode_reply.
*
* @param request Pointer to the xce_request_t containing the fetchfile msg.
* @return int 0 on success, -1 on failure.
*/
static int handle_fetchfile(xce_request_t* request)
{
xce_fetchfile_t* msg = request->body.fetchfilemsg;
xce_fetchfile_reply_t freply;
struct stat st;
uint8_t* buf = NULL;
int fd;
ssize_t nread;
uint32_t maxbytes;
XCECD_LOG(XCE_LOG_DEBUG,
"Fetchfile request: jobid=%lu path=%s offset=%lu max=%u",
msg->jobid, msg->filepath, msg->offset, msg->max_bytes);
memset(&freply, 0, sizeof(freply));
freply.jobid = msg->jobid;
if (msg->filepath == NULL || msg->filepath[0] == '\0') {
XCECD_LOG(XCE_LOG_ERROR, "Fetchfile: empty filepath");
freply.error = -EINVAL;
goto send;
}
/* Bound max_bytes to avoid huge allocations */
maxbytes = (msg->max_bytes == 0 || msg->max_bytes > 4 * 1024 * 1024)
? 4 * 1024 * 1024
: msg->max_bytes;
fd = open(msg->filepath, O_RDONLY);
if (fd < 0) {
XCECD_LOG(XCE_LOG_ERROR, "Fetchfile: open(%s) failed: %s",
msg->filepath, strerror(errno));
freply.error = -errno;
goto send;
}
if (fstat(fd, &st) < 0) {
freply.error = -errno;
close(fd);
goto send;
}
freply.file_size = (uint64_t)st.st_size;
buf = malloc(maxbytes);
if (buf == NULL) {
freply.error = -ENOMEM;
close(fd);
goto send;
}
nread = pread(fd, buf, maxbytes, (off_t)msg->offset);
close(fd);
if (nread < 0) {
XCECD_LOG(XCE_LOG_ERROR, "Fetchfile: pread failed: %s",
strerror(errno));
freply.error = -errno;
freply.bytes_read = 0;
free(buf);
buf = NULL;
} else {
freply.error = 0;
freply.bytes_read = (uint32_t)nread;
freply.content = buf;
buf = NULL; /* ownership passed to freply */
}
send:
xcecli_reply_fetchfile(xcecli_ctx, request, &freply);
free(freply.content);
free(buf);
return 0;
}
/**
* @brief Handles the runtasks request from the controller.
*
@ -1234,6 +1319,9 @@ static int myreq_handler(xce_request_t* request, void* args)
case OPCode_jobcontrol:
ret = handle_jobcontrol(request);
break;
case OPCode_fetchfile:
ret = handle_fetchfile(request);
break;
default:
break;
}