|
| 1 | +/************************************************************\ |
| 2 | + * Copyright 2024 Lawrence Livermore National Security, LLC |
| 3 | + * (c.f. AUTHORS, NOTICE.LLNS, COPYING) |
| 4 | + * |
| 5 | + * This file is part of the Flux resource manager framework. |
| 6 | + * For details, see https://github.com/flux-framework. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: LGPL-3.0 |
| 9 | +\************************************************************/ |
| 10 | + |
| 11 | +/* hwloc options handler |
| 12 | + */ |
| 13 | +#define FLUX_SHELL_PLUGIN_NAME "hwloc" |
| 14 | + |
| 15 | +#if HAVE_CONFIG_H |
| 16 | +#include "config.h" |
| 17 | +#endif |
| 18 | + |
| 19 | +#include <sys/types.h> |
| 20 | +#include <sys/stat.h> |
| 21 | +#include <fcntl.h> |
| 22 | +#include <unistd.h> |
| 23 | + |
| 24 | +#include <flux/core.h> |
| 25 | +#include <flux/shell.h> |
| 26 | + |
| 27 | +#include "src/common/libutil/read_all.h" |
| 28 | +#include "ccan/str/str.h" |
| 29 | + |
| 30 | +#include "builtins.h" |
| 31 | + |
| 32 | +static int create_xmlfile (flux_shell_t *shell) |
| 33 | +{ |
| 34 | + int fd = -1; |
| 35 | + char *xmlfile = NULL; |
| 36 | + const char *hwloc_xml; |
| 37 | + const char *tmpdir; |
| 38 | + |
| 39 | + if (!(tmpdir = flux_shell_getenv (shell, "FLUX_JOB_TMPDIR"))) |
| 40 | + tmpdir = "/tmp"; |
| 41 | + |
| 42 | + if (flux_shell_get_hwloc_xml (shell, &hwloc_xml) < 0) { |
| 43 | + shell_log_error ("failed to get shell hwloc xml"); |
| 44 | + goto error; |
| 45 | + } |
| 46 | + if (asprintf (&xmlfile, "%s/hwloc.xml", tmpdir) < 0) { |
| 47 | + shell_log_error ("asprintf HWLOC_XMLFILE failed"); |
| 48 | + goto error; |
| 49 | + } |
| 50 | + if ((fd = open (xmlfile, O_CREAT|O_EXCL|O_WRONLY, 0640)) < 0) { |
| 51 | + shell_log_errno ("%s", xmlfile); |
| 52 | + goto error; |
| 53 | + } |
| 54 | + shell_debug ("Writing %ld bytes to HWLOC_XMLFILE=%s\n", |
| 55 | + (long int) strlen (hwloc_xml), |
| 56 | + xmlfile); |
| 57 | + if (write_all (fd, hwloc_xml, strlen (hwloc_xml)) < 0 || close (fd) < 0) { |
| 58 | + shell_log_errno ("failed to write HWLOC_XMLFILE"); |
| 59 | + goto error; |
| 60 | + } |
| 61 | + if (flux_shell_setenvf (shell, 0, "HWLOC_XMLFILE", "%s", xmlfile) < 0) { |
| 62 | + shell_log_errno ("failed to set HWLOC_XMLFILE in job environment"); |
| 63 | + goto error; |
| 64 | + } |
| 65 | + return 0; |
| 66 | +error: |
| 67 | + if (fd >= 0) |
| 68 | + close (fd); |
| 69 | + free (xmlfile); |
| 70 | + return -1; |
| 71 | +} |
| 72 | + |
| 73 | +static int hwloc_post_init (flux_plugin_t *p, |
| 74 | + const char *topic, |
| 75 | + flux_plugin_arg_t *args, |
| 76 | + void *data) |
| 77 | +{ |
| 78 | + flux_shell_t *shell = flux_plugin_get_shell (p); |
| 79 | + int xmlfile = 0; |
| 80 | + |
| 81 | + if (flux_shell_getopt_unpack (shell, |
| 82 | + "hwloc", |
| 83 | + "{s?i}", |
| 84 | + "xmlfile", &xmlfile) < 0) |
| 85 | + return shell_log_errno ("failed to unpack hwloc options"); |
| 86 | + |
| 87 | + if (xmlfile && create_xmlfile (shell) < 0) |
| 88 | + return shell_log_errno ("failed to write HWLOC_XMLFILE"); |
| 89 | + return 0; |
| 90 | +} |
| 91 | + |
| 92 | +struct shell_builtin builtin_hwloc = { |
| 93 | + .name = FLUX_SHELL_PLUGIN_NAME, |
| 94 | + .post_init = hwloc_post_init, |
| 95 | +}; |
| 96 | + |
| 97 | +/* |
| 98 | + * vi:tabstop=4 shiftwidth=4 expandtab |
| 99 | + */ |
0 commit comments