1 /*****************************************************************************\
2  * src/slurmd/slurmstepd/io.h - slurmstepd standard IO routines
3  *****************************************************************************
4  *  Copyright (C) 2002 The Regents of the University of California.
5  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
6  *  Written by Mark Grondona <mgrondona@llnl.gov>.
7  *  CODE-OCEC-09-009. All rights reserved.
8  *
9  *  This file is part of Slurm, a resource management program.
10  *  For details, see <https://slurm.schedmd.com/>.
11  *  Please also read the included file: DISCLAIMER.
12  *
13  *  Slurm is free software; you can redistribute it and/or modify it under
14  *  the terms of the GNU General Public License as published by the Free
15  *  Software Foundation; either version 2 of the License, or (at your option)
16  *  any later version.
17  *
18  *  In addition, as a special exception, the copyright holders give permission
19  *  to link the code of portions of this program with the OpenSSL library under
20  *  certain conditions as described in each individual source file, and
21  *  distribute linked combinations including the two. You must obey the GNU
22  *  General Public License in all respects for all of the code used other than
23  *  OpenSSL. If you modify file(s) with this exception, you may extend this
24  *  exception to your version of the file(s), but you are not obligated to do
25  *  so. If you do not wish to do so, delete this exception statement from your
26  *  version.  If you delete this exception statement from all source files in
27  *  the program, then also delete it here.
28  *
29  *  Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
30  *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
31  *  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
32  *  details.
33  *
34  *  You should have received a copy of the GNU General Public License along
35  *  with Slurm; if not, write to the Free Software Foundation, Inc.,
36  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
37 \*****************************************************************************/
38 
39 #ifndef _IO_H
40 #define _IO_H
41 
42 #include "src/common/eio.h"
43 
44 #include "src/slurmd/slurmstepd/slurmstepd_job.h"
45 
46 /*
47  * The message cache uses up free message buffers, so STDIO_MAX_MSG_CACHE
48  * must be a number smaller than STDIO_MAX_FREE_BUF.
49  */
50 #define STDIO_MAX_FREE_BUF 1024
51 #define STDIO_MAX_MSG_CACHE 128
52 
53 struct io_buf {
54 	int ref_count;
55 	uint32_t length;
56 	void *data;
57 };
58 
59 /* For each task's ofname and efname, are all the names NULL,
60    one null and the others "/dev/null", all non-null and unique,
61    or all non-null and identical. */
62 typedef enum {
63 	SLURMD_ALL_NULL,   /* output from all tasks goes to the client (srun) */
64 	SLURMD_ONE_NULL,   /* output from one task goes to the client, output
65 			      from other tasks is discarded */
66 	SLURMD_ALL_UNIQUE, /* separate output files per task.  written from
67 			      tasks unless stepd_step_rec_t->labelio == true, in
68 			      which case the slurmstepd does the write */
69 	SLURMD_ALL_SAME,   /* all tasks write to the same file.  written from
70 			      tasks unless stepd_step_rec_t->labelio == true, in
71 			      which case the slurmstepd does the write */
72 	SLURMD_UNKNOWN
73 } slurmd_filename_pattern_t;
74 
75 
76 struct io_buf *alloc_io_buf(void);
77 void free_io_buf(struct io_buf *buf);
78 
79 /*
80  * Create a TCP connection back the initial client (e.g. srun).
81  *
82  * Since this is the first client connection and the IO engine has not
83  * yet started, we initialize the msg_queue as an empty list and
84  * directly add the eio_obj_t to the eio handle with eio_new_initial_handle.
85  */
86 int io_initial_client_connect(srun_info_t *srun, stepd_step_rec_t *job,
87 			      int stdout_tasks, int stderr_tasks);
88 
89 /*
90  * Initiate a TCP connection back to a waiting client (e.g. srun).
91  *
92  * Create a new eio client object and wake up the eio engine so that
93  * it can see the new object.
94  */
95 int io_client_connect(srun_info_t *srun, stepd_step_rec_t *job);
96 
97 
98 /*
99  * Open a local file and create and eio object for files written
100  * from the slurmstepd, probably with labelled output.
101  */
102 int
103 io_create_local_client(const char *filename, int file_flags,
104 		       stepd_step_rec_t *job, bool labelio,
105 		       int stdout_tasks, int stderr_tasks);
106 
107 /*
108  * Initialize each task's standard I/O file descriptors.  The file descriptors
109  * may be files, or may be the end of a pipe which is handled by an eio_obj_t.
110  */
111 int io_init_tasks_stdio(stepd_step_rec_t *job);
112 
113 /*
114  * Start IO handling thread.
115  * Initializes IO pipes, creates IO objects and appends them to job->objs,
116  * and opens 2*ntask initial connections for stdout/err, also appending these
117  * to job->objs list.
118  */
119 extern void io_thread_start(stepd_step_rec_t *job);
120 
121 int io_dup_stdio(stepd_step_task_info_t *t);
122 
123 /*
124  *  Close the tasks' ends of the stdio pipes.
125  *  Presumably the tasks have already been started, and
126  *  have their copies of these file descriptors.
127  */
128 void io_close_task_fds(stepd_step_rec_t *job);
129 
130 void io_close_all(stepd_step_rec_t *job);
131 
132 void io_close_local_fds(stepd_step_rec_t *job);
133 
134 
135 /*
136  *  Look for a pattern in the stdout and stderr file names, and see
137  *  if stdout and stderr point to the same file(s).
138  *  See comments above for slurmd_filename_pattern_t.
139  */
140 void io_find_filename_pattern(  stepd_step_rec_t *job,
141 				slurmd_filename_pattern_t *outpattern,
142 				slurmd_filename_pattern_t *errpattern,
143 				bool *same_out_err_files );
144 
145 /*
146  *  Get the flags to be used with the open call to create output files.
147  */
148 int io_get_file_flags(stepd_step_rec_t *job);
149 
150 /*
151  *  Initialize "user managed" IO, where each task has a single TCP
152  *  socket end point shared on stdin, stdout, and stderr.
153  */
154 int user_managed_io_client_connect(int ntasks, srun_info_t *srun,
155 				   stepd_step_task_info_t **tasks);
156 
157 #endif /* !_IO_H */
158