Lolly 1.4.27
Loading...
Searching...
No Matches
Classes | Functions | Variables
unix_sys_utils.cpp File Reference
#include "unix_sys_utils.hpp"
#include "analyze.hpp"
#include "tm_timer.hpp"
#include <fcntl.h>
#include <pthread.h>
#include <pwd.h>
#include <spawn.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
Include dependency graph for unix_sys_utils.cpp:

Go to the source code of this file.

Classes

struct  _mutex
 
struct  _mutex_lock
 
struct  _ts_string
 
struct  _pipe_t
 
struct  _channel
 
struct  _file_actions_t
 

Functions

SN unix_get_process_id ()
 
void_ts_malloc (int n)
 
void _ts_free (void *a)
 
static void_background_read_task (void *channel_as_void_ptr)
 
static void_background_write_task (void *channel_as_void_ptr)
 
static void _unix_system_warn (pid_t pid, string which, string msg)
 
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 ()
 

Variables

char ** environ
 
static _mutex _ts_memory_lock
 

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}

◆ _ts_malloc()

void * _ts_malloc ( int n)

Definition at line 51 of file unix_sys_utils.cpp.

51 {
53 return malloc (n);
54}
The list class represents a linked list.
Definition list.hpp:48
static _mutex _ts_memory_lock

◆ _ts_free()

void _ts_free ( void * a)

Definition at line 57 of file unix_sys_utils.cpp.

57 {
59 free (a);
60}

◆ _background_read_task()

static void * _background_read_task ( void * channel_as_void_ptr)
static

Definition at line 139 of file unix_sys_utils.cpp.

139 {
141 int fd= c->fd;
142 int n = c->buffer_size;
143 char* b = A (c->buffer);
144 int m;
145 do {
146 m= read (fd, b, n);
147 // cout << "read " << m << " bytes from " << fd << "\n";
148 if (m > 0) c->data.append (b, m);
149 if (m == 0) {
150 if (close (fd) != 0) c->status= -1;
151 }
152 } while (m > 0);
153 return (void*) NULL;
154}
bool read(string s, int &i, const char *test)
Definition analyze.cpp:589
T * A(array< T > a)
Get a pointer to the first element of the array.
Definition array.hpp:174
blackbox b[13]

◆ _background_write_task()

static void * _background_write_task ( void * channel_as_void_ptr)
static

Definition at line 158 of file unix_sys_utils.cpp.

158 {
160 int fd= c->fd;
161 const char* d = c->data.a;
162 int n = c->buffer_size;
163 int t= (c->data).n, k= 0, o= 0;
164 if (t == 0) return (void*) NULL;
165 if (n == 0) {
166 c->status= -1;
167 return (void*) NULL;
168 }
169 do {
170 int m= min (n, t - k);
171 // cout << "writting " << m << " bytes / " << t-k << "\n";
172 o= write (fd, (void*) (d + k), m);
173 // cout << "written " << o << " bytes to " << fd << "\n";
174 if (o > 0) k+= o;
175 if (o < 0) {
176 close (fd);
177 c->status= -1;
178 }
179 if (k == t) {
180 if (close (fd) != 0) c->status= -1;
181 }
182 } while (o > 0 && k < t);
183 return (void*) NULL;
184}
blackbox t[13]
SI min(SI i, SI j)
Returns the minimum of two signed integers.
Definition minmax.hpp:27

◆ _unix_system_warn()

static void _unix_system_warn ( pid_t pid,
string which,
string msg )
static

Definition at line 197 of file unix_sys_utils.cpp.

197 {
198 (void) which;
199}

◆ 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
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
void tm_delete_array(C *Ptr)
char * as_charp(string s)
Definition string.cpp:294
string as_string(int16_t i)
Definition string.cpp:310
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

Variable Documentation

◆ environ

char** environ
extern

◆ _ts_memory_lock

_mutex _ts_memory_lock
static

Definition at line 48 of file unix_sys_utils.cpp.