5858#include <unistd.h>
5959#include <stdlib.h>
6060#include <assert.h>
61+ #include <libgen.h>
6162#ifdef HAVE_ARGZ_ADD
6263#include <argz.h>
6364#else
@@ -649,6 +650,25 @@ static int shell_pmi_init (flux_plugin_t *p,
649650 return 0 ;
650651}
651652
653+ /* Prepend 'path' to the environment variable 'name' which is assumed to
654+ * be a colon-separated list. If 'name' isn't already set, set it to 'path'.
655+ * Return 0 on success, -1 on failure.
656+ */
657+ static int prepend_path_to_cmd_env (flux_cmd_t * cmd ,
658+ const char * name ,
659+ const char * path )
660+ {
661+ const char * searchpath = flux_cmd_getenv (cmd , name );
662+
663+ return flux_cmd_setenvf (cmd ,
664+ 1 ,
665+ name ,
666+ "%s%s%s" ,
667+ path ,
668+ searchpath ? ":" : "" ,
669+ searchpath ? searchpath : "" );
670+ }
671+
652672static int shell_pmi_task_init (flux_plugin_t * p ,
653673 const char * topic ,
654674 flux_plugin_arg_t * args ,
@@ -674,12 +694,31 @@ static int shell_pmi_task_init (flux_plugin_t *p,
674694 return -1 ;
675695 if (flux_shell_task_channel_subscribe (task , "PMI_FD" , pmi_fd_cb , pmi ) < 0 )
676696 return -1 ;
697+ const char * pmipath ;
698+ if (!(pmipath = flux_conf_builtin_get ("pmi_library_path" , FLUX_CONF_AUTO )))
699+ return -1 ;
700+ /* Flux libpmi.so and libpmi2.so are installed to a directory outside
701+ * of the default ld.so search path. Add this directory to LD_LIBRARY_PATH
702+ * so Flux jobs find Flux PMI libs before Slurm's PMI libs which are in
703+ * the system path (a tripping hazard).
704+ * N.B. The cray-pals plugin in flux-coral2 will need to undo this
705+ * so Cray MPICH finds the Cray libpmi2.so first which uses libpals.
706+ * See also flux-framework/flux-core#5714.
707+ */
708+ char * cpy ;
709+ char * pmidir ;
710+ if (!(cpy = strdup (pmipath ))
711+ || !(pmidir = dirname (cpy ))
712+ || prepend_path_to_cmd_env (cmd , "LD_LIBRARY_PATH" , pmidir ) < 0 ) {
713+ free (cpy );
714+ return -1 ;
715+ }
716+ free (cpy );
677717 /* N.B. The pre-v5 OpenMPI flux MCA plugin dlopens the library pointed to
678718 * by FLUX_PMI_LIBRARY_PATH. Since the library only works when this shell
679719 * plugin is active, set it here.
680720 */
681- const char * s = flux_conf_builtin_get ("pmi_library_path" , FLUX_CONF_AUTO );
682- if (!s || flux_cmd_setenvf (cmd , 1 , "FLUX_PMI_LIBRARY_PATH" , "%s" , s ) < 0 )
721+ if (flux_cmd_setenvf (cmd , 1 , "FLUX_PMI_LIBRARY_PATH" , "%s" , pmipath ) < 0 )
683722 return -1 ;
684723 return 0 ;
685724}
0 commit comments