Lolly 1.4.28
Loading...
Searching...
No Matches
Functions
unix_sys_utils.hpp File Reference
#include "array.hpp"
#include "string.hpp"
Include dependency graph for unix_sys_utils.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

SN unix_get_process_id ()
 
int unix_system (array< string > arg, array< int > fd_in, array< string > str_in, array< int > fd_out, array< string * > str_out)
 
string unix_get_login ()
 
string unix_get_username ()
 

Function Documentation

◆ unix_get_process_id()

SN unix_get_process_id ( )

Definition at line 25 of file unix_sys_utils.cpp.

25 {
26 return getpid ();
27}

◆ unix_system()

int unix_system ( array< string > arg,
array< int > fd_in,
array< string > str_in,
array< int > fd_out,
array< string * > str_out )

Definition at line 202 of file unix_sys_utils.cpp.

203 {
204 // Run command arg[0] with arguments arg[i], i >= 1.
205 // str_in[i] is sent to the file descriptor fd_in[i].
206 // str_out[i] is filled from the file descriptor fd_out[i].
207 // If str_in[i] is -1 then $$i automatically replaced by a valid
208 // file descriptor in arg.
209 if (N (arg) == 0) return 0;
210 string which= recompose (arg, " ");
211 int n_in= N (fd_in), n_out= N (fd_out);
212 ASSERT (N (str_in) == n_in, "size mismatch");
213 ASSERT (N (str_out) == n_out, "size mismatch");
216 for (int i= 0; i < n_in; i++) {
218 pp_in[i].out ()) != 0)
219 return -1;
220 if (fd_in[i] >= 0) {
222 fd_in[i]) != 0)
223 return -1;
225 pp_in[i].in ()) != 0)
226 return -1;
227 }
228 }
229 for (int i= 0; i < n_out; i++) {
231 pp_out[i].in ()) != 0)
232 return -1;
234 fd_out[i]) != 0)
235 return -1;
237 pp_out[i].out ()) != 0)
238 return -1;
239 }
241 for (int j= 0; j < N (arg); j++)
242 for (int i= 0; i < n_in; i++)
243 if (fd_in[i] < 0)
244 arg_[j]=
245 replace (arg_[j], "$$" * as_string (i), as_string (pp_in[i].in ()));
247 for (int j= 0; j < N (arg_); j++)
248 _arg << as_charp (arg_[j]);
249 _arg << (char*) NULL;
250 pid_t pid;
251 int status=
252 posix_spawnp (&pid, _arg[0], &file_actions.rep, NULL, A (_arg), environ);
253 for (int j= 0; j < N (arg_); j++)
255 if (status != 0) {
256 return -1;
257 }
258 // close useless ports
259 for (int i= 0; i < n_in; i++)
260 close (pp_in[i].in ());
261 for (int i= 0; i < n_out; i++)
262 close (pp_out[i].out ());
263
264 // write to spawn process
267 for (int i= 0; i < n_in; i++) {
268 channels_in[i]._init_in (pp_in[i].out (), str_in[i], 1 << 12);
269 if (pthread_create (&threads_write[i], NULL /* &attr */,
270 _background_write_task, (void*) &(channels_in[i])))
271 return -1;
272 }
273
274 // read from spawn process
277 for (int i= 0; i < n_out; i++) {
278 channels_out[i]._init_out (pp_out[i].in (), 1 << 12);
279 if (pthread_create (&threads_read[i], NULL /* &attr */,
280 _background_read_task, (void*) &(channels_out[i])))
281 return -1;
282 }
283
284 int wret;
286 while ((wret= waitpid (pid, &status, WNOHANG)) == 0) {
287 usleep (100);
288 if (texmacs_time () - last_wait_time > 5000) {
290 _unix_system_warn (pid, which, "waiting spawn process");
291 }
292 }
293
294 // wait for terminating threads
295 void* exit_status;
296 int thread_status= 0;
297 for (int i= 0; i < n_in; i++) {
299 if (channels_in[i].status < 0) thread_status= -1;
300 }
301 for (int i= 0; i < n_out; i++) {
303 *(str_out[i])= string (channels_out[i].data.a, channels_out[i].data.n);
304 if (channels_out[i].status < 0) thread_status= -1;
305 }
306
307 if (thread_status < 0) return thread_status;
308 if (wret < 0 || WIFEXITED (status) == 0) return -1;
309 return WEXITSTATUS (status);
310}
string recompose(array< string > a, string sep)
Definition analyze.cpp:963
string replace(string s, string what, string by)
Definition analyze.cpp:899
T * A(array< T > a)
Get a pointer to the first element of the array.
Definition array.hpp:174
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170
#define ASSERT(cond, msg)
Macro used to assert that a condition is true, and throw an exception with an error message if the co...
Definition basic.hpp:85
The list class represents a linked list.
Definition list.hpp:48
void tm_delete_array(C *Ptr)
char * as_charp(string s)
Definition string.cpp:256
string as_string(int16_t i)
Definition string.cpp:272
time_t texmacs_time()
Definition tm_timer.cpp:47
static void * _background_write_task(void *channel_as_void_ptr)
static void * _background_read_task(void *channel_as_void_ptr)
char ** environ
static void _unix_system_warn(pid_t pid, string which, string msg)

◆ unix_get_login()

string unix_get_login ( )

Definition at line 313 of file unix_sys_utils.cpp.

313 {
314 uid_t uid= getuid ();
315 struct passwd* pwd= getpwuid (uid);
316 return string (pwd->pw_name);
317}
list(T item)
Construct a new list object with a single item.
Definition list.hpp:137

◆ unix_get_username()

string unix_get_username ( )

Definition at line 320 of file unix_sys_utils.cpp.

320 {
321 uid_t uid= getuid ();
322 struct passwd* pwd= getpwuid (uid);
323 return tokenize (string (pwd->pw_gecos), string (","))[0];
324}
array< string > tokenize(string s, string sep)
Definition analyze.cpp:948