Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
- Reduce the number of system headers loaded, overload some more system
[simgrid.git] / src / gras / Transport / transport_plugin_file.c
1 /* $Id$ */
2
3 /* File transport - send/receive a bunch of bytes from a file               */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2004 Martin Quinson.                                       */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include <errno.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16
17 #include "gras_private.h"
18 #include "transport_private.h"
19
20 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(trp_file,transport,
21         "Pseudo-transport to write to/read from a file");
22
23 /***
24  *** Prototypes 
25  ***/
26 void         gras_trp_file_close(gras_socket_t *sd);
27   
28 gras_error_t gras_trp_file_chunk_send(gras_socket_t *sd,
29                                       const char *data,
30                                       long int size);
31
32 gras_error_t gras_trp_file_chunk_recv(gras_socket_t *sd,
33                                       char *data,
34                                       long int size);
35
36
37 /***
38  *** Specific plugin part
39  ***/
40
41 typedef struct {
42   fd_set incoming_socks;
43 } gras_trp_file_plug_data_t;
44
45 /***
46  *** Specific socket part
47  ***/
48
49
50
51 /***
52  *** Code
53  ***/
54 gras_error_t
55 gras_trp_file_setup(gras_trp_plugin_t *plug) {
56
57   gras_trp_file_plug_data_t *file = gras_new(gras_trp_file_plug_data_t,1);
58   if (!file)
59     RAISE_MALLOC;
60
61   FD_ZERO(&(file->incoming_socks));
62
63   plug->socket_close = gras_trp_file_close;
64
65   plug->chunk_send   = gras_trp_file_chunk_send;
66   plug->chunk_recv   = gras_trp_file_chunk_recv;
67
68   plug->data         = (void*)file;
69
70   return no_error;
71 }
72
73 /**
74  * gras_socket_client_from_file:
75  *
76  * Create a client socket from a file path.
77  *
78  * This only possible in RL, and is mainly for debugging.
79  */
80 gras_error_t
81 gras_socket_client_from_file(const char*path,
82                              /* OUT */ gras_socket_t **dst) {
83   gras_error_t errcode;
84   gras_trp_plugin_t *trp;
85
86   gras_assert0(gras_if_RL(),
87                "Cannot use file as socket in the simulator");
88
89   TRY(gras_trp_socket_new(0,dst));
90
91   TRY(gras_trp_plugin_get_by_name("file",&trp));
92   (*dst)->plugin=trp;
93
94   if (strcmp("-", path)) {
95     (*dst)->sd = open(path, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP );
96     
97     if ( (*dst)->sd < 0) {
98       RAISE2(system_error,
99              "Cannot create a client socket from file %s: %s",
100              path, strerror(errno));
101     }
102   } else {
103     (*dst)->sd = 1; /* stdout */
104   }
105
106   DEBUG5("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
107          path,
108          (*dst)->sd,
109          (*dst)->incoming?'y':'n', 
110          (*dst)->outgoing?'y':'n',
111          (*dst)->accepting?'y':'n');
112    
113   return no_error;
114 }
115
116 /**
117  * gras_socket_server_from_file:
118  *
119  * Create a server socket from a file path.
120  *
121  * This only possible in RL, and is mainly for debugging.
122  */
123 gras_error_t
124 gras_socket_server_from_file(const char*path,
125                              /* OUT */ gras_socket_t **dst) {
126   gras_error_t errcode;
127   gras_trp_plugin_t *trp;
128
129   gras_assert0(gras_if_RL(),
130                "Cannot use file as socket in the simulator");
131
132   TRY(gras_trp_socket_new(1,dst));
133
134   TRY(gras_trp_plugin_get_by_name("file",&trp));
135   (*dst)->plugin=trp;
136
137
138   if (strcmp("-", path)) {
139     (*dst)->sd = open(path, O_RDONLY );
140
141     if ( (*dst)->sd < 0) {
142       RAISE2(system_error,
143              "Cannot create a server socket from file %s: %s",
144              path, strerror(errno));
145     }
146   } else {
147     (*dst)->sd = 0; /* stdin */
148   }
149
150   DEBUG4("sd=%d in=%c out=%c accept=%c",
151          (*dst)->sd,
152          (*dst)->incoming?'y':'n', 
153          (*dst)->outgoing?'y':'n',
154          (*dst)->accepting?'y':'n');
155
156   return no_error;
157 }
158
159 void gras_trp_file_close(gras_socket_t *sock){
160   gras_trp_file_plug_data_t *data;
161   
162   if (!sock) return; /* close only once */
163   data=sock->plugin->data;
164
165   if (sock->sd == 0) {
166     DEBUG0("Do not close stdin");
167   } else if (sock->sd == 1) {
168     DEBUG0("Do not close stdout");
169   } else {
170     DEBUG1("close file connection %d", sock->sd);
171
172     /* forget about the socket */
173     FD_CLR(sock->sd, &(data->incoming_socks));
174
175     /* close the socket */
176     if(close(sock->sd) < 0) {
177       WARN2("error while closing file %d: %s", 
178                sock->sd, strerror(errno));
179     }
180   }
181 }
182
183 /**
184  * gras_trp_file_chunk_send:
185  *
186  * Send data on a file pseudo-socket
187  */
188 gras_error_t 
189 gras_trp_file_chunk_send(gras_socket_t *sock,
190                          const char *data,
191                          long int size) {
192   
193   gras_assert0(sock->outgoing, "Cannot write on client file socket");
194   gras_assert0(size >= 0, "Cannot send a negative amount of data");
195
196   while (size) {
197     int status = 0;
198     
199     DEBUG3("write(%d, %p, %ld);", sock->sd, data, (long int)size);
200     status = write(sock->sd, data, (long int)size);
201     
202     if (status == -1) {
203       RAISE4(system_error,"write(%d,%p,%d) failed: %s",
204              sock->sd, data, (int)size,
205              strerror(errno));
206     }
207     
208     if (status) {
209       size  -= status;
210       data  += status;
211     } else {
212       RAISE0(system_error,"file descriptor closed");
213     }
214   }
215
216   return no_error;
217 }
218 /**
219  * gras_trp_file_chunk_recv:
220  *
221  * Receive data on a file pseudo-socket.
222  */
223 gras_error_t 
224 gras_trp_file_chunk_recv(gras_socket_t *sock,
225                         char *data,
226                         long int size) {
227
228   gras_assert0(sock, "Cannot recv on an NULL socket");
229   gras_assert0(sock->incoming, "Cannot recv on client file socket");
230   gras_assert0(size >= 0, "Cannot receive a negative amount of data");
231   
232   while (size) {
233     int status = 0;
234     
235     status = read(sock->sd, data, (long int)size);
236     DEBUG3("read(%d, %p, %ld);", sock->sd, data, size);
237     
238     if (status == -1) {
239       RAISE4(system_error,"read(%d,%p,%d) failed: %s",
240              sock->sd, data, (int)size,
241              strerror(errno));
242     }
243     
244     if (status) {
245       size  -= status;
246       data  += status;
247     } else {
248       RAISE0(system_error,"file descriptor closed");
249     }
250   }
251   
252   return no_error;
253 }
254