Skip to content

Commit af19c0b

Browse files
committed
flux-shell: truncate long log messages
Problem: potentially useful shell log messages are lost if the static log buffer size is exceeded. If the message is too long, truncate it, add a '+', and continue on. Don't treat this as an error. Fixes flux-framework#4860
1 parent 3e5ceca commit af19c0b

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

src/shell/log.c

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -168,29 +168,23 @@ static int errorcat (int errnum, int start, char *buf, size_t len)
168168
return n;
169169
}
170170

171-
/* Format message, printing an error to stderr on failure
171+
/* Format message, appending a '+' if the buffer isn't large enough.
172172
* If errnum > 0, then append result of strerror (errnum).
173173
*/
174-
static int msgfmt (char *buf,
175-
size_t len,
176-
int errnum,
177-
const char *fmt,
178-
va_list ap)
174+
static void msgfmt (char *buf,
175+
size_t len,
176+
int errnum,
177+
const char *fmt,
178+
va_list ap)
179179
{
180-
int rc = vsnprintf (buf, len, fmt, ap);
181-
if ((rc < 0 || rc >= len) || errorcat (errnum, rc, buf, len) < 0) {
182-
fprintf (stderr,
183-
"%s: unable to format log msg (%s): %s\n",
184-
logger.prog,
185-
fmt,
186-
strerror (errno));
187-
return -1;
188-
}
180+
int rc;
181+
if ((rc = vsnprintf (buf, len, fmt, ap)) >= len
182+
|| errorcat (errnum, rc, buf, len) < 0)
183+
buf[len - 2] = '+';
189184
/* Clean up trailing newline, pointless here
190185
*/
191-
if (buf[rc-1] == '\n')
192-
buf[rc-1] = '\0';
193-
return 0;
186+
else if (rc > 0 && buf[rc - 1] == '\n')
187+
buf[rc - 1] = '\0';
194188
}
195189

196190
void flux_shell_log (const char *component,
@@ -202,8 +196,8 @@ void flux_shell_log (const char *component,
202196
char buf [4096];
203197
va_list ap;
204198
va_start (ap, fmt);
205-
if (msgfmt (buf, sizeof (buf), 0, fmt, ap) == 0)
206-
send_logmsg (buf, level, component, file, line);
199+
msgfmt (buf, sizeof (buf), 0, fmt, ap);
200+
send_logmsg (buf, level, component, file, line);
207201
va_end (ap);
208202
}
209203

@@ -237,8 +231,8 @@ int flux_shell_err (const char *component,
237231
char buf [4096];
238232
va_list ap;
239233
va_start (ap, fmt);
240-
if (msgfmt (buf, sizeof (buf), errnum, fmt, ap) == 0)
241-
send_logmsg (buf, FLUX_SHELL_ERROR, component, file, line);
234+
msgfmt (buf, sizeof (buf), errnum, fmt, ap);
235+
send_logmsg (buf, FLUX_SHELL_ERROR, component, file, line);
242236
va_end (ap);
243237
errno = errnum;
244238
return -1;
@@ -257,8 +251,7 @@ void flux_shell_raise (const char *type,
257251
return;
258252

259253
va_start (ap, fmt);
260-
if (msgfmt (buf, sizeof (buf), 0, fmt, ap) < 0)
261-
sprintf (buf, "flux-shell: fatal error");
254+
msgfmt (buf, sizeof (buf), 0, fmt, ap);
262255
va_end (ap);
263256

264257
if (!(f = flux_job_raise (shell->h,
@@ -288,10 +281,8 @@ void flux_shell_fatal (const char *component,
288281
va_list ap;
289282

290283
va_start (ap, fmt);
291-
if (msgfmt (buf, sizeof (buf), errnum, fmt, ap) < 0)
292-
sprintf (buf, "flux-shell: fatal error");
293-
else
294-
send_logmsg (buf, FLUX_SHELL_FATAL, component, file, line);
284+
msgfmt (buf, sizeof (buf), errnum, fmt, ap);
285+
send_logmsg (buf, FLUX_SHELL_FATAL, component, file, line);
295286
va_end (ap);
296287

297288
/* Attempt to kill any running tasks

0 commit comments

Comments
 (0)