Changeset 9fd39d6 in mainline
- Timestamp:
- 2010-11-07T19:15:39Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 16ac756
- Parents:
- 68f0c3a (diff), 3da12d74 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- uspace
- Files:
-
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/exec.c
r68f0c3a r9fd39d6 41 41 #include <fcntl.h> 42 42 #include <str_error.h> 43 #include <errno.h> 43 44 44 45 #include "config.h" … … 116 117 task_exit_t texit; 117 118 char *tmp; 118 int r etval;119 int rc, retval; 119 120 120 121 tmp = str_dup(find_command(cmd)); 121 122 free(found); 122 123 123 tid = task_spawn(tmp, (const char **) argv, &retval);124 rc = task_spawnv(&tid, tmp, (const char **) argv); 124 125 free(tmp); 125 126 126 if ( tid == 0) {127 if (rc != 0) { 127 128 cli_error(CL_EEXEC, "%s: Cannot spawn `%s' (%s)", progname, cmd, 128 str_error(r etval));129 str_error(rc)); 129 130 return 1; 130 131 } 131 132 132 task_wait(tid, &texit, &retval); 133 if (texit != TASK_EXIT_NORMAL) { 133 rc = task_wait(tid, &texit, &retval); 134 if (rc != EOK) { 135 printf("%s: Failed waiting for command (%s)\n", str_error(rc)); 136 } else if (texit != TASK_EXIT_NORMAL) { 134 137 printf("%s: Command failed (unexpectedly terminated)\n", progname); 135 138 } else if (retval != 0) { 136 printf("%s: Command failed ( %s)\n",137 progname, str_error(retval));139 printf("%s: Command failed (exit code %d)\n", 140 progname, retval); 138 141 } 139 142 -
uspace/app/getterm/getterm.c
r68f0c3a r9fd39d6 41 41 #include <task.h> 42 42 #include <str_error.h> 43 #include <errno.h> 43 44 #include "version.h" 44 45 … … 74 75 static task_id_t spawn(const char *fname) 75 76 { 76 const char *args[2]; 77 task_id_t id; 78 int rc; 77 79 78 args[0] = fname; 79 args[1] = NULL; 80 81 int err; 82 task_id_t id = task_spawn(fname, args, &err); 83 84 if (id == 0) 80 rc = task_spawnl(&id, fname, fname, NULL); 81 if (rc != EOK) { 85 82 printf("%s: Error spawning %s (%s)\n", APP_NAME, fname, 86 str_error(err)); 83 str_error(rc)); 84 return 0; 85 } 87 86 88 87 return id; -
uspace/app/init/init.c
r68f0c3a r9fd39d6 124 124 static void spawn(const char *fname) 125 125 { 126 const char *argv[2];126 int rc; 127 127 struct stat s; 128 128 … … 131 131 132 132 printf("%s: Spawning %s\n", NAME, fname); 133 134 argv[0] = fname; 135 argv[1] = NULL; 136 137 int err; 138 if (!task_spawn(fname, argv, &err)) 133 rc = task_spawnl(NULL, fname, fname, NULL); 134 if (rc != EOK) { 139 135 printf("%s: Error spawning %s (%s)\n", NAME, fname, 140 str_error(err)); 136 str_error(rc)); 137 } 141 138 } 142 139 143 140 static void srv_start(const char *fname) 144 141 { 145 const char *argv[2];146 142 task_id_t id; 147 143 task_exit_t texit; … … 153 149 154 150 printf("%s: Starting %s\n", NAME, fname); 155 156 argv[0] = fname; 157 argv[1] = NULL; 158 159 id = task_spawn(fname, argv, &retval); 151 rc = task_spawnl(&id, fname, fname, NULL); 160 152 if (!id) { 161 153 printf("%s: Error spawning %s (%s)\n", NAME, fname, 162 str_error(r etval));154 str_error(rc)); 163 155 return; 164 156 } … … 167 159 if (rc != EOK) { 168 160 printf("%s: Error waiting for %s (%s(\n", NAME, fname, 169 str_error(retval)); 170 return; 171 } 172 173 if ((texit != TASK_EXIT_NORMAL) || (retval != 0)) { 174 printf("%s: Server %s failed to start (%s)\n", NAME, 175 fname, str_error(retval)); 161 str_error(rc)); 162 return; 163 } 164 165 if (texit != TASK_EXIT_NORMAL) { 166 printf("%s: Server %s failed to start (unexpectedly " 167 "terminated)\n", NAME, fname); 168 return; 169 } 170 171 if (retval != 0) { 172 printf("%s: Server %s failed to start (exit code %d)\n", NAME, 173 fname, retval); 176 174 } 177 175 } … … 179 177 static void console(const char *dev) 180 178 { 181 const char *argv[3];182 179 char hid_in[DEVMAP_NAME_MAXLEN]; 183 180 int rc; … … 190 187 dev_handle_t handle; 191 188 rc = devmap_device_get_handle(dev, &handle, IPC_FLAG_BLOCKING); 192 193 if (rc == EOK) { 194 argv[0] = SRV_CONSOLE; 195 argv[1] = hid_in; 196 argv[2] = NULL; 197 198 if (!task_spawn(SRV_CONSOLE, argv, &rc)) 199 printf("%s: Error spawning %s %s (%s)\n", NAME, SRV_CONSOLE, 200 hid_in, str_error(rc)); 201 } else 189 if (rc != EOK) { 202 190 printf("%s: Error waiting on %s (%s)\n", NAME, hid_in, 203 191 str_error(rc)); 192 return; 193 } 194 195 rc = task_spawnl(NULL, SRV_CONSOLE, SRV_CONSOLE, hid_in, NULL); 196 if (rc != EOK) { 197 printf("%s: Error spawning %s %s (%s)\n", NAME, SRV_CONSOLE, 198 hid_in, str_error(rc)); 199 } 204 200 } 205 201 206 202 static void getterm(const char *dev, const char *app) 207 203 { 208 const char *argv[4];209 204 char term[DEVMAP_NAME_MAXLEN]; 210 205 int rc; … … 217 212 dev_handle_t handle; 218 213 rc = devmap_device_get_handle(dev, &handle, IPC_FLAG_BLOCKING); 219 220 if (rc == EOK) { 221 argv[0] = APP_GETTERM; 222 argv[1] = term; 223 argv[2] = app; 224 argv[3] = NULL; 225 226 if (!task_spawn(APP_GETTERM, argv, &rc)) 227 printf("%s: Error spawning %s %s %s (%s)\n", NAME, APP_GETTERM, 228 term, app, str_error(rc)); 229 } else 214 if (rc != EOK) { 230 215 printf("%s: Error waiting on %s (%s)\n", NAME, term, 231 216 str_error(rc)); 217 return; 218 } 219 220 rc = task_spawnl(NULL, APP_GETTERM, APP_GETTERM, term, app, NULL); 221 if (rc != EOK) { 222 printf("%s: Error spawning %s %s %s (%s)\n", NAME, 223 APP_GETTERM, term, app, str_error(rc)); 224 } 232 225 } 233 226 -
uspace/app/netecho/netecho.c
r68f0c3a r9fd39d6 41 41 #include <task.h> 42 42 #include <arg_parse.h> 43 #include <err.h>44 43 45 44 #include <net/in.h> … … 91 90 int main(int argc, char *argv[]) 92 91 { 93 ERROR_DECLARE;94 95 92 size_t size = 1024; 96 93 int verbose = 0; … … 117 114 size_t reply_length; 118 115 int value; 116 int rc; 119 117 120 118 // parse the command line arguments … … 123 121 switch (argv[index][1]) { 124 122 case 'b': 125 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &backlog, 0)); 123 rc = arg_parse_int(argc, argv, &index, &backlog, 0); 124 if (rc != EOK) 125 return rc; 126 126 break; 127 127 case 'c': 128 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &count, 0)); 128 rc = arg_parse_int(argc, argv, &index, &count, 0); 129 if (rc != EOK) 130 return rc; 129 131 break; 130 132 case 'f': 131 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family)); 133 rc = arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family); 134 if (rc != EOK) 135 return rc; 132 136 break; 133 137 case 'h': … … 136 140 break; 137 141 case 'p': 138 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 142 rc = arg_parse_int(argc, argv, &index, &value, 0); 143 if (rc != EOK) 144 return rc; 139 145 port = (uint16_t) value; 140 146 break; 141 147 case 'r': 142 ERROR_PROPAGATE(arg_parse_string(argc, argv, &index, &reply, 0)); 148 rc = arg_parse_string(argc, argv, &index, &reply, 0); 149 if (rc != EOK) 150 return rc; 143 151 break; 144 152 case 's': 145 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 153 rc = arg_parse_int(argc, argv, &index, &value, 0); 154 if (rc != EOK) 155 return rc; 146 156 size = (value >= 0) ? (size_t) value : 0; 147 157 break; 148 158 case 't': 149 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type)); 159 rc = arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type); 160 if (rc != EOK) 161 return rc; 150 162 type = (sock_type_t) value; 151 163 break; … … 156 168 case '-': 157 169 if (str_lcmp(argv[index] + 2, "backlog=", 6) == 0) { 158 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &backlog, 8)); 170 rc = arg_parse_int(argc, argv, &index, &backlog, 8); 171 if (rc != EOK) 172 return rc; 159 173 } else if (str_lcmp(argv[index] + 2, "count=", 6) == 0) { 160 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &count, 8));174 rc = arg_parse_int(argc, argv, &index, &count, 8); 161 175 } else if (str_lcmp(argv[index] + 2, "family=", 7) == 0) { 162 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family)); 176 rc = arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family); 177 if (rc != EOK) 178 return rc; 163 179 } else if (str_lcmp(argv[index] + 2, "help", 5) == 0) { 164 180 echo_print_help(); 165 181 return EOK; 166 182 } else if (str_lcmp(argv[index] + 2, "port=", 5) == 0) { 167 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7)); 183 rc = arg_parse_int(argc, argv, &index, &value, 7); 184 if (rc != EOK) 185 return rc; 168 186 port = (uint16_t) value; 169 187 } else if (str_lcmp(argv[index] + 2, "reply=", 6) == 0) { 170 ERROR_PROPAGATE(arg_parse_string(argc, argv, &index, &reply, 8)); 188 rc = arg_parse_string(argc, argv, &index, &reply, 8); 189 if (rc != EOK) 190 return rc; 171 191 } else if (str_lcmp(argv[index] + 2, "size=", 5) == 0) { 172 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7)); 192 rc = arg_parse_int(argc, argv, &index, &value, 7); 193 if (rc != EOK) 194 return rc; 173 195 size = (value >= 0) ? (size_t) value : 0; 174 196 } else if (str_lcmp(argv[index] + 2, "type=", 5) == 0) { 175 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type)); 197 rc = arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type); 198 if (rc != EOK) 199 return rc; 176 200 type = (sock_type_t) value; 177 201 } else if (str_lcmp(argv[index] + 2, "verbose", 8) == 0) { … … 240 264 } 241 265 // set the backlog 242 if (ERROR_OCCURRED(listen(listening_id, backlog))) { 243 socket_print_error(stderr, ERROR_CODE, "Socket listen: ", "\n"); 244 return ERROR_CODE; 266 rc = listen(listening_id, backlog); 267 if (rc != EOK) { 268 socket_print_error(stderr, rc, "Socket listen: ", "\n"); 269 return rc; 245 270 } 246 271 } 247 272 248 273 // bind the listenning socket 249 if (ERROR_OCCURRED(bind(listening_id, address, addrlen))) { 250 socket_print_error(stderr, ERROR_CODE, "Socket bind: ", "\n"); 251 return ERROR_CODE; 274 rc = bind(listening_id, address, addrlen); 275 if (rc != EOK) { 276 socket_print_error(stderr, rc, "Socket bind: ", "\n"); 277 return rc; 252 278 } 253 279 … … 301 327 // parse the source address 302 328 if (address_start) { 303 if (ERROR_OCCURRED(inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string)))) { 304 fprintf(stderr, "Received address error %d\n", ERROR_CODE); 329 rc = inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string)); 330 if (rc != EOK) { 331 fprintf(stderr, "Received address error %d\n", rc); 305 332 } else { 306 333 data[length] = '\0'; … … 311 338 312 339 // answer the request either with the static reply or the original data 313 if (ERROR_OCCURRED(sendto(socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen))) 314 socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n"); 340 rc = sendto(socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen); 341 if (rc != EOK) 342 socket_print_error(stderr, rc, "Socket send: ", "\n"); 315 343 } 316 344 317 345 // close the accepted stream socket 318 346 if (type == SOCK_STREAM) { 319 if (ERROR_OCCURRED(closesocket(socket_id))) 320 socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n"); 347 rc = closesocket(socket_id); 348 if (rc != EOK) 349 socket_print_error(stderr, rc, "Close socket: ", "\n"); 321 350 } 322 351 … … 335 364 336 365 // close the listenning socket 337 if (ERROR_OCCURRED(closesocket(listening_id))) { 338 socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n"); 339 return ERROR_CODE; 366 rc = closesocket(listening_id); 367 if (rc != EOK) { 368 socket_print_error(stderr, rc, "Close socket: ", "\n"); 369 return rc; 340 370 } 341 371 -
uspace/app/netstart/netstart.c
r68f0c3a r9fd39d6 41 41 #include <task.h> 42 42 #include <str_error.h> 43 #include <err.h>44 43 #include <ipc/ipc.h> 45 44 #include <ipc/services.h> … … 57 56 static bool spawn(const char *desc, const char *path) 58 57 { 58 int rc; 59 59 60 printf("%s: Spawning %s (%s)\n", NAME, desc, path); 60 61 const char *argv[2]; 62 63 argv[0] = path; 64 argv[1] = NULL; 65 66 int err; 67 if (task_spawn(path, argv, &err) == 0) { 61 rc = task_spawnl(NULL, path, path, NULL); 62 if (rc != EOK) { 68 63 fprintf(stderr, "%s: Error spawning %s (%s)\n", NAME, path, 69 str_error( err));64 str_error(rc)); 70 65 return false; 71 66 } … … 76 71 int main(int argc, char *argv[]) 77 72 { 78 ERROR_DECLARE;73 int rc; 79 74 80 75 if (!spawn("networking service", "/srv/net")) … … 84 79 85 80 int net_phone = connect_to_service(SERVICE_NETWORKING); 86 if (ERROR_OCCURRED(ipc_call_sync_0_0(net_phone, NET_NET_STARTUP))) { 87 fprintf(stderr, "%s: Startup error %d\n", NAME, ERROR_CODE); 88 return ERROR_CODE; 81 rc = ipc_call_sync_0_0(net_phone, NET_NET_STARTUP); 82 if (rc != EOK) { 83 fprintf(stderr, "%s: Startup error %d\n", NAME, rc); 84 return rc; 89 85 } 90 86 -
uspace/app/nettest1/nettest.c
r68f0c3a r9fd39d6 36 36 37 37 #include <stdio.h> 38 #include <err.h>39 40 38 #include <net/socket.h> 41 39 … … 87 85 int sockets_close(int verbose, int *socket_ids, int sockets) 88 86 { 89 ERROR_DECLARE; 90 91 int index; 87 int index; 88 int rc; 92 89 93 90 if (verbose) … … 97 94 98 95 for (index = 0; index < sockets; index++) { 99 if (ERROR_OCCURRED(closesocket(socket_ids[index]))) { 96 rc = closesocket(socket_ids[index]); 97 if (rc != EOK) { 100 98 printf("Socket %d (%d) error:\n", index, socket_ids[index]); 101 socket_print_error(stderr, ERROR_CODE, "Socket close: ", "\n");102 return ERROR_CODE;99 socket_print_error(stderr, rc, "Socket close: ", "\n"); 100 return rc; 103 101 } 104 102 if (verbose) … … 121 119 int sockets_connect(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t addrlen) 122 120 { 123 ERROR_DECLARE; 124 125 int index; 121 int index; 122 int rc; 126 123 127 124 if (verbose) … … 131 128 132 129 for (index = 0; index < sockets; index++) { 133 if (ERROR_OCCURRED(connect(socket_ids[index], address, addrlen))) { 134 socket_print_error(stderr, ERROR_CODE, "Socket connect: ", "\n"); 135 return ERROR_CODE; 130 rc = connect(socket_ids[index], address, addrlen); 131 if (rc != EOK) { 132 socket_print_error(stderr, rc, "Socket connect: ", "\n"); 133 return rc; 136 134 } 137 135 if (verbose) … … 157 155 int sockets_sendto(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t addrlen, char *data, int size, int messages) 158 156 { 159 ERROR_DECLARE;160 161 157 int index; 162 158 int message; 159 int rc; 163 160 164 161 if (verbose) … … 169 166 for (index = 0; index < sockets; index++) { 170 167 for (message = 0; message < messages; message++) { 171 if (ERROR_OCCURRED(sendto(socket_ids[index], data, size, 0, address, addrlen))) { 172 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message); 173 socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n"); 174 return ERROR_CODE; 168 rc = sendto(socket_ids[index], data, size, 0, address, addrlen); 169 if (rc != EOK) { 170 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message); 171 socket_print_error(stderr, rc, "Socket send: ", "\n"); 172 return rc; 175 173 } 176 174 } … … 239 237 int sockets_sendto_recvfrom(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t *addrlen, char *data, int size, int messages) 240 238 { 241 ERROR_DECLARE;242 243 239 int value; 244 240 int index; 245 241 int message; 242 int rc; 246 243 247 244 if (verbose) … … 252 249 for (index = 0; index < sockets; index++) { 253 250 for (message = 0; message < messages; message++) { 254 if (ERROR_OCCURRED(sendto(socket_ids[index], data, size, 0, address, *addrlen))) { 255 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message); 256 socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n"); 257 return ERROR_CODE; 251 rc = sendto(socket_ids[index], data, size, 0, address, *addrlen); 252 if (rc != EOK) { 253 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message); 254 socket_print_error(stderr, rc, "Socket send: ", "\n"); 255 return rc; 258 256 } 259 257 value = recvfrom(socket_ids[index], data, size, 0, address, addrlen); -
uspace/app/nettest1/nettest1.c
r68f0c3a r9fd39d6 44 44 #include <time.h> 45 45 #include <arg_parse.h> 46 #include <err.h>47 46 48 47 #include <net/in.h> … … 58 57 #define NETTEST1_TEXT "Networking test 1 - sockets" 59 58 59 static int family = PF_INET; 60 static sock_type_t type = SOCK_DGRAM; 61 static char *data; 62 static size_t size = 27; 63 static int verbose = 0; 64 65 static struct sockaddr *address; 66 static socklen_t addrlen; 67 68 static int sockets; 69 static int messages; 70 static uint16_t port; 71 60 72 static void nettest1_print_help(void) 61 73 { 62 74 printf( 63 "Network Networking test 1 aplication - sockets\n" \ 64 "Usage: echo [options] numeric_address\n" \ 65 "Where options are:\n" \ 66 "-f protocol_family | --family=protocol_family\n" \ 67 "\tThe listenning socket protocol family. Only the PF_INET and PF_INET6 are supported.\n" 68 "\n" \ 69 "-h | --help\n" \ 70 "\tShow this application help.\n" 71 "\n" \ 72 "-m count | --messages=count\n" \ 73 "\tThe number of messages to send and receive per socket. The default is 10.\n" \ 74 "\n" \ 75 "-n sockets | --sockets=count\n" \ 76 "\tThe number of sockets to use. The default is 10.\n" \ 77 "\n" \ 78 "-p port_number | --port=port_number\n" \ 79 "\tThe port number the application should send messages to. The default is 7.\n" \ 80 "\n" \ 81 "-s packet_size | --size=packet_size\n" \ 82 "\tThe packet data size the application sends. The default is 28 bytes.\n" \ 83 "\n" \ 84 "-v | --verbose\n" \ 85 "\tShow all output messages.\n" 86 ); 75 "Network Networking test 1 aplication - sockets\n" 76 "Usage: echo [options] numeric_address\n" 77 "Where options are:\n" 78 "-f protocol_family | --family=protocol_family\n" 79 "\tThe listenning socket protocol family. Only the PF_INET and " 80 "PF_INET6 are supported.\n" 81 "\n" 82 "-h | --help\n" 83 "\tShow this application help.\n" 84 "\n" 85 "-m count | --messages=count\n" 86 "\tThe number of messages to send and receive per socket. The " 87 "default is 10.\n" 88 "\n" 89 "-n sockets | --sockets=count\n" 90 "\tThe number of sockets to use. The default is 10.\n" 91 "\n" 92 "-p port_number | --port=port_number\n" 93 "\tThe port number the application should send messages to. The " 94 "default is 7.\n" 95 "\n" 96 "-s packet_size | --size=packet_size\n" 97 "\tThe packet data size the application sends. The default is " 98 "28 bytes.\n" 99 "\n" 100 "-v | --verbose\n" 101 "\tShow all output messages.\n"); 87 102 } 88 103 89 /** Refreshes the data.104 /** Parse one command-line option. 90 105 * 91 * Fills the data block with the NETTEST1_TEXT pattern. 92 * 93 * @param[out] data The data block. 94 * @param[in] size The data block size in bytes. 95 */ 96 static void nettest1_refresh_data(char *data, size_t size) 106 * @param argc Number of all command-line arguments. 107 * @param argv All command-line arguments. 108 * @param index Current argument index (in, out). 109 */ 110 static int nettest1_parse_opt(int argc, char *argv[], int *index) 97 111 { 98 size_t length;99 100 // fill the data101 length = 0;102 while (size > length + sizeof(NETTEST1_TEXT) - 1) {103 memcpy(data + length, NETTEST1_TEXT, sizeof(NETTEST1_TEXT) - 1);104 length += sizeof(NETTEST1_TEXT) - 1;105 }106 memcpy(data + length, NETTEST1_TEXT, size - length);107 data[size] = '\0';108 }109 110 111 int main(int argc, char *argv[])112 {113 ERROR_DECLARE;114 115 size_t size = 27;116 int verbose = 0;117 sock_type_t type = SOCK_DGRAM;118 int sockets = 10;119 int messages = 10;120 int family = PF_INET;121 uint16_t port = 7;122 123 socklen_t max_length = sizeof(struct sockaddr_in6);124 uint8_t address_data[max_length];125 struct sockaddr *address = (struct sockaddr *) address_data;126 struct sockaddr_in *address_in = (struct sockaddr_in *) address;127 struct sockaddr_in6 *address_in6 = (struct sockaddr_in6 *) address;128 socklen_t addrlen;129 uint8_t *address_start;130 131 int *socket_ids;132 char *data;133 112 int value; 134 int index; 135 struct timeval time_before; 136 struct timeval time_after; 137 138 // parse the command line arguments 139 // stop before the last argument if it does not start with the minus sign ('-') 140 for (index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); index++) { 141 // options should start with the minus sign ('-') 142 if (argv[index][0] == '-') { 143 switch (argv[index][1]) { 144 // short options with only one letter 145 case 'f': 146 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family)); 147 break; 148 case 'h': 149 nettest1_print_help(); 150 return EOK; 151 break; 152 case 'm': 153 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &messages, 0)); 154 break; 155 case 'n': 156 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &sockets, 0)); 157 break; 158 case 'p': 159 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 160 port = (uint16_t) value; 161 break; 162 case 's': 163 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 164 size = (value >= 0) ? (size_t) value : 0; 165 break; 166 case 't': 167 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type)); 168 type = (sock_type_t) value; 169 break; 170 case 'v': 171 verbose = 1; 172 break; 173 // long options with the double minus sign ('-') 174 case '-': 175 if (str_lcmp(argv[index] + 2, "family=", 7) == 0) { 176 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family)); 177 } else if (str_lcmp(argv[index] + 2, "help", 5) == 0) { 178 nettest1_print_help(); 179 return EOK; 180 } else if (str_lcmp(argv[index] + 2, "messages=", 6) == 0) { 181 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &messages, 8)); 182 } else if (str_lcmp(argv[index] + 2, "sockets=", 6) == 0) { 183 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &sockets, 8)); 184 } else if (str_lcmp(argv[index] + 2, "port=", 5) == 0) { 185 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7)); 186 port = (uint16_t) value; 187 } else if (str_lcmp(argv[index] + 2, "type=", 5) == 0) { 188 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type)); 189 type = (sock_type_t) value; 190 } else if (str_lcmp(argv[index] + 2, "verbose", 8) == 0) { 191 verbose = 1; 192 } else { 193 nettest1_print_help(); 194 return EINVAL; 195 } 196 break; 197 default: 198 nettest1_print_help(); 199 return EINVAL; 200 } 113 int rc; 114 115 switch (argv[*index][1]) { 116 /* 117 * Short options with only one letter 118 */ 119 case 'f': 120 rc = arg_parse_name_int(argc, argv, index, &family, 0, socket_parse_protocol_family); 121 if (rc != EOK) 122 return rc; 123 break; 124 case 'h': 125 nettest1_print_help(); 126 return EOK; 127 case 'm': 128 rc = arg_parse_int(argc, argv, index, &messages, 0); 129 if (rc != EOK) 130 return rc; 131 break; 132 case 'n': 133 rc = arg_parse_int(argc, argv, index, &sockets, 0); 134 if (rc != EOK) 135 return rc; 136 break; 137 case 'p': 138 rc = arg_parse_int(argc, argv, index, &value, 0); 139 if (rc != EOK) 140 return rc; 141 port = (uint16_t) value; 142 break; 143 case 's': 144 rc = arg_parse_int(argc, argv, index, &value, 0); 145 if (rc != EOK) 146 return rc; 147 size = (value >= 0) ? (size_t) value : 0; 148 break; 149 case 't': 150 rc = arg_parse_name_int(argc, argv, index, &value, 0, socket_parse_socket_type); 151 if (rc != EOK) 152 return rc; 153 type = (sock_type_t) value; 154 break; 155 case 'v': 156 verbose = 1; 157 break; 158 /* 159 * Long options with double dash ('-') 160 */ 161 case '-': 162 if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) { 163 rc = arg_parse_name_int(argc, argv, index, &family, 9, 164 socket_parse_protocol_family); 165 if (rc != EOK) 166 return rc; 167 } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) { 168 nettest1_print_help(); 169 return EOK; 170 } else if (str_lcmp(argv[*index] + 2, "messages=", 6) == 0) { 171 rc = arg_parse_int(argc, argv, index, &messages, 8); 172 if (rc != EOK) 173 return rc; 174 } else if (str_lcmp(argv[*index] + 2, "sockets=", 6) == 0) { 175 rc = arg_parse_int(argc, argv, index, &sockets, 8); 176 if (rc != EOK) 177 return rc; 178 } else if (str_lcmp(argv[*index] + 2, "port=", 5) == 0) { 179 rc = arg_parse_int(argc, argv, index, &value, 7); 180 if (rc != EOK) 181 return rc; 182 port = (uint16_t) value; 183 } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) { 184 rc = arg_parse_name_int(argc, argv, index, &value, 7, 185 socket_parse_socket_type); 186 if (rc != EOK) 187 return rc; 188 type = (sock_type_t) value; 189 } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) { 190 verbose = 1; 201 191 } else { 202 192 nettest1_print_help(); 203 193 return EINVAL; 204 194 } 205 } 206 207 // if not before the last argument containing the address 195 break; 196 default: 197 nettest1_print_help(); 198 return EINVAL; 199 } 200 201 return EOK; 202 } 203 204 /** Fill buffer with the NETTEST1_TEXT pattern. 205 * 206 * @param buffer Data buffer. 207 * @param size Buffer size in bytes. 208 */ 209 static void nettest1_fill_buffer(char *buffer, size_t size) 210 { 211 size_t length; 212 213 length = 0; 214 while (size > length + sizeof(NETTEST1_TEXT) - 1) { 215 memcpy(buffer + length, NETTEST1_TEXT, 216 sizeof(NETTEST1_TEXT) - 1); 217 length += sizeof(NETTEST1_TEXT) - 1; 218 } 219 220 memcpy(buffer + length, NETTEST1_TEXT, size - length); 221 buffer[size] = '\0'; 222 } 223 224 static int nettest1_test(int *socket_ids, int nsockets, int nmessages) 225 { 226 int rc; 227 228 if (verbose) 229 printf("%d sockets, %d messages\n", nsockets, nmessages); 230 231 rc = sockets_create(verbose, socket_ids, nsockets, family, type); 232 if (rc != EOK) 233 return rc; 234 235 if (type == SOCK_STREAM) { 236 rc = sockets_connect(verbose, socket_ids, nsockets, address, 237 addrlen); 238 if (rc != EOK) 239 return rc; 240 } 241 242 rc = sockets_sendto_recvfrom(verbose, socket_ids, nsockets, address, 243 &addrlen, data, size, nmessages); 244 if (rc != EOK) 245 return rc; 246 247 rc = sockets_close(verbose, socket_ids, nsockets); 248 if (rc != EOK) 249 return rc; 250 251 if (verbose) 252 printf("\tOK\n"); 253 254 /****/ 255 256 rc = sockets_create(verbose, socket_ids, nsockets, family, type); 257 if (rc != EOK) 258 return rc; 259 260 if (type == SOCK_STREAM) { 261 rc = sockets_connect(verbose, socket_ids, nsockets, address, 262 addrlen); 263 if (rc != EOK) 264 return rc; 265 } 266 267 rc = sockets_sendto(verbose, socket_ids, nsockets, address, addrlen, 268 data, size, nmessages); 269 if (rc != EOK) 270 return rc; 271 272 rc = sockets_recvfrom(verbose, socket_ids, nsockets, address, &addrlen, 273 data, size, nmessages); 274 if (rc != EOK) 275 return rc; 276 277 rc = sockets_close(verbose, socket_ids, nsockets); 278 if (rc != EOK) 279 return rc; 280 281 if (verbose) 282 printf("\tOK\n"); 283 284 return EOK; 285 } 286 287 int main(int argc, char *argv[]) 288 { 289 290 socklen_t max_length; 291 uint8_t *address_data[sizeof(struct sockaddr_in6)]; 292 struct sockaddr_in *address_in; 293 struct sockaddr_in6 *address_in6; 294 uint8_t *address_start; 295 296 int *socket_ids; 297 int index; 298 struct timeval time_before; 299 struct timeval time_after; 300 301 int rc; 302 303 max_length = sizeof(address_data); 304 address = (struct sockaddr *) address_data; 305 address_in = (struct sockaddr_in *) address; 306 address_in6 = (struct sockaddr_in6 *) address; 307 308 sockets = 10; 309 messages = 10; 310 port = 7; 311 312 /* 313 * Parse the command line arguments. Stop before the last argument 314 * if it does not start with dash ('-') 315 */ 316 for (index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); index++) { 317 /* Options should start with dash ('-') */ 318 if (argv[index][0] == '-') { 319 rc = nettest1_parse_opt(argc, argv, &index); 320 if (rc != EOK) 321 return rc; 322 } else { 323 nettest1_print_help(); 324 return EINVAL; 325 } 326 } 327 328 /* If not before the last argument containing the address */ 208 329 if (index >= argc) { 209 330 printf("Command line error: missing address\n"); … … 212 333 } 213 334 214 / / prepare the address buffer335 /* Prepare the address buffer */ 215 336 bzero(address_data, max_length); 337 216 338 switch (family) { 217 339 case PF_INET: … … 232 354 } 233 355 234 // parse the last argument which should contain the address 235 if (ERROR_OCCURRED(inet_pton(family, argv[argc - 1], address_start))) { 236 fprintf(stderr, "Address parse error %d\n", ERROR_CODE); 237 return ERROR_CODE; 238 } 239 240 // check the buffer size 356 /* Parse the last argument which should contain the address */ 357 rc = inet_pton(family, argv[argc - 1], address_start); 358 if (rc != EOK) { 359 fprintf(stderr, "Address parse error %d\n", rc); 360 return rc; 361 } 362 363 /* Check data buffer size */ 241 364 if (size <= 0) { 242 fprintf(stderr, "Data buffer size too small (%d). Using 1024 bytes instead.\n", size); 365 fprintf(stderr, "Data buffer size too small (%d). Using 1024 " 366 "bytes instead.\n", size); 243 367 size = 1024; 244 368 } 245 369 246 // prepare the buffer 247 // size plus the terminating null (\0) 370 /* 371 * Prepare data buffer. Allocate size bytes plus one for the 372 * trailing null character. 373 */ 248 374 data = (char *) malloc(size + 1); 249 375 if (!data) { … … 251 377 return ENOMEM; 252 378 } 253 nettest1_ refresh_data(data, size);254 255 / / check the socket count379 nettest1_fill_buffer(data, size); 380 381 /* Check socket count */ 256 382 if (sockets <= 0) { 257 fprintf(stderr, "Socket count too small (%d). Using 2 instead.\n", sockets); 383 fprintf(stderr, "Socket count too small (%d). Using " 384 "2 instead.\n", sockets); 258 385 sockets = 2; 259 386 } 260 387 261 // prepare the socket buffer 262 // count plus the terminating null (\0) 388 /* 389 * Prepare socket buffer. Allocate count fields plus the terminating 390 * null (\0). 391 */ 263 392 socket_ids = (int *) malloc(sizeof(int) * (sockets + 1)); 264 393 if (!socket_ids) { … … 271 400 printf("Starting tests\n"); 272 401 273 if (verbose) 274 printf("1 socket, 1 message\n"); 275 276 if (ERROR_OCCURRED(gettimeofday(&time_before, NULL))) { 277 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 278 return ERROR_CODE; 279 } 280 281 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type)); 282 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1)); 283 if (verbose) 284 printf("\tOK\n"); 285 286 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type)); 287 if (type == SOCK_STREAM) 288 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, 1, address, addrlen)); 289 ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, 1, address, &addrlen, data, size, 1)); 290 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1)); 291 if (verbose) 292 printf("\tOK\n"); 293 294 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type)); 295 if (type == SOCK_STREAM) 296 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, 1, address, addrlen)); 297 ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, 1, address, addrlen, data, size, 1)); 298 ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, 1, address, &addrlen, data, size, 1)); 299 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1)); 300 if (verbose) 301 printf("\tOK\n"); 302 303 if (verbose) 304 printf("1 socket, %d messages\n", messages); 305 306 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type)); 307 if (type == SOCK_STREAM) 308 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, 1, address, addrlen)); 309 ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, 1, address, &addrlen, data, size, messages)); 310 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1)); 311 if (verbose) 312 printf("\tOK\n"); 313 314 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type)); 315 if (type == SOCK_STREAM) 316 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, 1, address, addrlen)); 317 ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, 1, address, addrlen, data, size, messages)); 318 ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, 1, address, &addrlen, data, size, messages)); 319 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1)); 320 if (verbose) 321 printf("\tOK\n"); 322 323 if (verbose) 324 printf("%d sockets, 1 message\n", sockets); 325 326 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 327 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 328 if (verbose) 329 printf("\tOK\n"); 330 331 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 332 if (type == SOCK_STREAM) 333 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen)); 334 ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, 1)); 335 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 336 if (verbose) 337 printf("\tOK\n"); 338 339 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 340 if (type == SOCK_STREAM) 341 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen)); 342 ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, sockets, address, addrlen, data, size, 1)); 343 ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, 1)); 344 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 345 if (verbose) 346 printf("\tOK\n"); 347 348 if (verbose) 349 printf("%d sockets, %d messages\n", sockets, messages); 350 351 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 352 if (type == SOCK_STREAM) 353 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen)); 354 ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages)); 355 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 356 if (verbose) 357 printf("\tOK\n"); 358 359 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 360 if (type == SOCK_STREAM) 361 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen)); 362 ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, sockets, address, addrlen, data, size, messages)); 363 ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages)); 364 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 365 366 if (ERROR_OCCURRED(gettimeofday(&time_after, NULL))) { 367 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 368 return ERROR_CODE; 369 } 370 371 if (verbose) 372 printf("\tOK\n"); 373 374 printf("Tested in %d microseconds\n", tv_sub(&time_after, &time_before)); 402 rc = gettimeofday(&time_before, NULL); 403 if (rc != EOK) { 404 fprintf(stderr, "Get time of day error %d\n", rc); 405 return rc; 406 } 407 408 nettest1_test(socket_ids, 1, 1); 409 nettest1_test(socket_ids, 1, messages); 410 nettest1_test(socket_ids, sockets, 1); 411 nettest1_test(socket_ids, sockets, messages); 412 413 rc = gettimeofday(&time_after, NULL); 414 if (rc != EOK) { 415 fprintf(stderr, "Get time of day error %d\n", rc); 416 return rc; 417 } 418 419 printf("Tested in %d microseconds\n", tv_sub(&time_after, 420 &time_before)); 375 421 376 422 if (verbose) -
uspace/app/nettest2/nettest2.c
r68f0c3a r9fd39d6 44 44 #include <time.h> 45 45 #include <arg_parse.h> 46 #include < err.h>46 #include <bool.h> 47 47 48 48 #include <net/in.h> … … 58 58 #define NETTEST2_TEXT "Networking test 2 - transfer" 59 59 60 static size_t size; 61 static bool verbose; 62 static sock_type_t type; 63 static int sockets; 64 static int messages; 65 static int family; 66 static uint16_t port; 67 60 68 static void nettest2_print_help(void) 61 69 { 62 70 printf( 63 "Network Networking test 2 aplication - UDP transfer\n" \ 64 "Usage: echo [options] numeric_address\n" \ 65 "Where options are:\n" \ 66 "-f protocol_family | --family=protocol_family\n" \ 67 "\tThe listenning socket protocol family. Only the PF_INET and PF_INET6 are supported.\n" 68 "\n" \ 69 "-h | --help\n" \ 70 "\tShow this application help.\n" 71 "\n" \ 72 "-m count | --messages=count\n" \ 73 "\tThe number of messages to send and receive per socket. The default is 10.\n" \ 74 "\n" \ 75 "-n sockets | --sockets=count\n" \ 76 "\tThe number of sockets to use. The default is 10.\n" \ 77 "\n" \ 78 "-p port_number | --port=port_number\n" \ 79 "\tThe port number the application should send messages to. The default is 7.\n" \ 80 "\n" \ 81 "-s packet_size | --size=packet_size\n" \ 82 "\tThe packet data size the application sends. The default is 29 bytes.\n" \ 83 "\n" \ 84 "-v | --verbose\n" \ 85 "\tShow all output messages.\n" 86 ); 71 "Network Networking test 2 aplication - UDP transfer\n" 72 "Usage: echo [options] address\n" 73 "Where options are:\n" 74 "-f protocol_family | --family=protocol_family\n" 75 "\tThe listenning socket protocol family. Only the PF_INET and " 76 "PF_INET6 are supported.\n" 77 "\n" 78 "-h | --help\n" 79 "\tShow this application help.\n" 80 "\n" 81 "-m count | --messages=count\n" 82 "\tThe number of messages to send and receive per socket. The " 83 "default is 10.\n" 84 "\n" 85 "-n sockets | --sockets=count\n" 86 "\tThe number of sockets to use. The default is 10.\n" 87 "\n" 88 "-p port_number | --port=port_number\n" 89 "\tThe port number the application should send messages to. The " 90 "default is 7.\n" 91 "\n" 92 "-s packet_size | --size=packet_size\n" 93 "\tThe packet data size the application sends. The default is 29 " 94 "bytes.\n" 95 "\n" 96 "-v | --verbose\n" 97 "\tShow all output messages.\n"); 87 98 } 88 99 89 /** Refreshes the data.100 /** Fill buffer with the NETTEST2_TEXT pattern. 90 101 * 91 * Fills the data block with the NETTEST1_TEXT pattern. 92 * 93 * @param[out] data The data block. 94 * @param[in] size The data block size in bytes. 95 */ 96 static void nettest2_refresh_data(char *data, size_t size) 102 * @param buffer Data buffer. 103 * @param size Buffer size in bytes. 104 */ 105 static void nettest2_fill_buffer(char *buffer, size_t size) 97 106 { 98 107 size_t length; 99 108 100 // fill the data101 109 length = 0; 102 110 while (size > length + sizeof(NETTEST2_TEXT) - 1) { 103 memcpy(data + length, NETTEST2_TEXT, sizeof(NETTEST2_TEXT) - 1); 111 memcpy(buffer + length, NETTEST2_TEXT, 112 sizeof(NETTEST2_TEXT) - 1); 104 113 length += sizeof(NETTEST2_TEXT) - 1; 105 114 } 106 memcpy(data + length, NETTEST2_TEXT, size - length); 107 data[size] = '\0'; 115 116 memcpy(buffer + length, NETTEST2_TEXT, size - length); 117 buffer[size] = '\0'; 108 118 } 109 119 110 111 int main(int argc, char *argv[]) 120 /** Parse one command-line option. 121 * 122 * @param argc Number of all command-line arguments. 123 * @param argv All command-line arguments. 124 * @param index Current argument index (in, out). 125 */ 126 static int nettest2_parse_opt(int argc, char *argv[], int *index) 112 127 { 113 ERROR_DECLARE;114 115 size_t size = 28;116 int verbose = 0;117 sock_type_t type = SOCK_DGRAM;118 int sockets = 10;119 int messages = 10;120 int family = PF_INET;121 uint16_t port = 7;122 123 socklen_t max_length = sizeof(struct sockaddr_in6);124 uint8_t address_data[max_length];125 struct sockaddr *address = (struct sockaddr *) address_data;126 struct sockaddr_in *address_in = (struct sockaddr_in *) address;127 struct sockaddr_in6 *address_in6 = (struct sockaddr_in6 *) address;128 socklen_t addrlen;129 uint8_t *address_start;130 131 int *socket_ids;132 char *data;133 128 int value; 134 int index; 135 struct timeval time_before; 136 struct timeval time_after; 137 138 // parse the command line arguments 139 // stop before the last argument if it does not start with the minus sign ('-') 140 for (index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); ++ index) { 141 // options should start with the minus sign ('-') 142 if (argv[index][0] == '-') { 143 switch(argv[index][1]){ 144 // short options with only one letter 145 case 'f': 146 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family)); 147 break; 148 case 'h': 149 nettest2_print_help(); 150 return EOK; 151 break; 152 case 'm': 153 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &messages, 0)); 154 break; 155 case 'n': 156 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &sockets, 0)); 157 break; 158 case 'p': 159 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 160 port = (uint16_t) value; 161 break; 162 case 's': 163 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 164 size = (value >= 0) ? (size_t) value : 0; 165 break; 166 case 't': 167 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type)); 168 type = (sock_type_t) value; 169 break; 170 case 'v': 171 verbose = 1; 172 break; 173 // long options with the double minus sign ('-') 174 case '-': 175 if (str_lcmp(argv[index] + 2, "family=", 7) == 0) { 176 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family)); 177 } else if (str_lcmp(argv[index] + 2, "help", 5) == 0) { 178 nettest2_print_help(); 179 return EOK; 180 } else if (str_lcmp(argv[index] + 2, "messages=", 6) == 0) { 181 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &messages, 8)); 182 } else if (str_lcmp(argv[index] + 2, "sockets=", 6) == 0) { 183 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &sockets, 8)); 184 } else if (str_lcmp(argv[index] + 2, "port=", 5) == 0) { 185 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7)); 186 port = (uint16_t) value; 187 } else if (str_lcmp(argv[index] + 2, "type=", 5) == 0) { 188 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type)); 189 type = (sock_type_t) value; 190 } else if (str_lcmp(argv[index] + 2, "verbose", 8) == 0) { 191 verbose = 1; 192 } else { 193 nettest2_print_help(); 194 return EINVAL; 195 } 196 break; 197 default: 198 nettest2_print_help(); 199 return EINVAL; 200 } 129 int rc; 130 131 switch (argv[*index][1]) { 132 /* 133 * Short options with only one letter 134 */ 135 case 'f': 136 rc = arg_parse_name_int(argc, argv, index, &family, 0, 137 socket_parse_protocol_family); 138 if (rc != EOK) 139 return rc; 140 break; 141 case 'h': 142 nettest2_print_help(); 143 return EOK; 144 break; 145 case 'm': 146 rc = arg_parse_int(argc, argv, index, &messages, 0); 147 if (rc != EOK) 148 return rc; 149 break; 150 case 'n': 151 rc = arg_parse_int(argc, argv, index, &sockets, 0); 152 if (rc != EOK) 153 return rc; 154 break; 155 case 'p': 156 rc = arg_parse_int(argc, argv, index, &value, 0); 157 if (rc != EOK) 158 return rc; 159 port = (uint16_t) value; 160 break; 161 case 's': 162 rc = arg_parse_int(argc, argv, index, &value, 0); 163 if (rc != EOK) 164 return rc; 165 size = (value >= 0) ? (size_t) value : 0; 166 break; 167 case 't': 168 rc = arg_parse_name_int(argc, argv, index, &value, 0, 169 socket_parse_socket_type); 170 if (rc != EOK) 171 return rc; 172 type = (sock_type_t) value; 173 break; 174 case 'v': 175 verbose = true; 176 break; 177 /* 178 * Long options with double dash ('-') 179 */ 180 case '-': 181 if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) { 182 rc = arg_parse_name_int(argc, argv, index, &family, 9, 183 socket_parse_protocol_family); 184 if (rc != EOK) 185 return rc; 186 } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) { 187 nettest2_print_help(); 188 return EOK; 189 } else if (str_lcmp(argv[*index] + 2, "messages=", 6) == 0) { 190 rc = arg_parse_int(argc, argv, index, &messages, 8); 191 if (rc != EOK) 192 return rc; 193 } else if (str_lcmp(argv[*index] + 2, "sockets=", 6) == 0) { 194 rc = arg_parse_int(argc, argv, index, &sockets, 8); 195 if (rc != EOK) 196 return rc; 197 } else if (str_lcmp(argv[*index] + 2, "port=", 5) == 0) { 198 rc = arg_parse_int(argc, argv, index, &value, 7); 199 if (rc != EOK) 200 return rc; 201 port = (uint16_t) value; 202 } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) { 203 rc = arg_parse_name_int(argc, argv, index, &value, 7, 204 socket_parse_socket_type); 205 if (rc != EOK) 206 return rc; 207 type = (sock_type_t) value; 208 } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) { 209 verbose = 1; 201 210 } else { 202 211 nettest2_print_help(); 203 212 return EINVAL; 204 213 } 205 } 206 207 // if not before the last argument containing the address 214 break; 215 default: 216 nettest2_print_help(); 217 return EINVAL; 218 } 219 220 return EOK; 221 } 222 223 int main(int argc, char *argv[]) 224 { 225 socklen_t max_length; 226 uint8_t address_data[sizeof(struct sockaddr_in6)]; 227 struct sockaddr *address; 228 struct sockaddr_in *address_in; 229 struct sockaddr_in6 *address_in6; 230 socklen_t addrlen; 231 uint8_t *address_start; 232 233 int *socket_ids; 234 char *data; 235 int index; 236 struct timeval time_before; 237 struct timeval time_after; 238 239 int rc; 240 241 size = 28; 242 verbose = false; 243 type = SOCK_DGRAM; 244 sockets = 10; 245 messages = 10; 246 family = PF_INET; 247 port = 7; 248 249 max_length = sizeof(address_data); 250 address = (struct sockaddr *) address_data; 251 address_in = (struct sockaddr_in *) address; 252 address_in6 = (struct sockaddr_in6 *) address; 253 254 /* 255 * Parse the command line arguments. 256 * 257 * Stop before the last argument if it does not start with dash ('-') 258 */ 259 for (index = 1; (index < argc - 1) || ((index == argc - 1) && 260 (argv[index][0] == '-')); ++index) { 261 262 /* Options should start with dash ('-') */ 263 if (argv[index][0] == '-') { 264 rc = nettest2_parse_opt(argc, argv, &index); 265 if (rc != EOK) 266 return rc; 267 } else { 268 nettest2_print_help(); 269 return EINVAL; 270 } 271 } 272 273 /* If not before the last argument containing the address */ 208 274 if (index >= argc) { 209 275 printf("Command line error: missing address\n"); … … 212 278 } 213 279 214 / / prepare the address buffer280 /* Prepare the address buffer */ 215 281 bzero(address_data, max_length); 282 216 283 switch (family) { 217 284 case PF_INET: … … 232 299 } 233 300 234 // parse the last argument which should contain the address 235 if (ERROR_OCCURRED(inet_pton(family, argv[argc - 1], address_start))) { 236 fprintf(stderr, "Address parse error %d\n", ERROR_CODE); 237 return ERROR_CODE; 238 } 239 240 // check the buffer size 301 /* Parse the last argument which should contain the address. */ 302 rc = inet_pton(family, argv[argc - 1], address_start); 303 if (rc != EOK) { 304 fprintf(stderr, "Address parse error %d\n", rc); 305 return rc; 306 } 307 308 /* Check data buffer size. */ 241 309 if (size <= 0) { 242 fprintf(stderr, "Data buffer size too small (%d). Using 1024 bytes instead.\n", size); 310 fprintf(stderr, "Data buffer size too small (%d). Using 1024 " 311 "bytes instead.\n", size); 243 312 size = 1024; 244 313 } 245 314 246 // prepare the buffer 247 // size plus terminating null (\0) 315 /* 316 * Prepare the buffer. Allocate size bytes plus one for terminating 317 * null character. 318 */ 248 319 data = (char *) malloc(size + 1); 249 320 if (!data) { … … 251 322 return ENOMEM; 252 323 } 253 nettest2_refresh_data(data, size); 254 255 // check the socket count 324 325 /* Fill buffer with a pattern. */ 326 nettest2_fill_buffer(data, size); 327 328 /* Check socket count. */ 256 329 if (sockets <= 0) { 257 fprintf(stderr, "Socket count too small (%d). Using 2 instead.\n", sockets); 330 fprintf(stderr, "Socket count too small (%d). Using " 331 "2 instead.\n", sockets); 258 332 sockets = 2; 259 333 } 260 334 261 // prepare the socket buffer 262 // count plus the terminating null (\0) 335 /* 336 * Prepare the socket buffer. 337 * Allocate count entries plus the terminating null (\0) 338 */ 263 339 socket_ids = (int *) malloc(sizeof(int) * (sockets + 1)); 264 340 if (!socket_ids) { … … 271 347 printf("Starting tests\n"); 272 348 273 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 274 275 if (type == SOCK_STREAM) 276 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen)); 349 rc = sockets_create(verbose, socket_ids, sockets, family, type); 350 if (rc != EOK) 351 return rc; 352 353 if (type == SOCK_STREAM) { 354 rc = sockets_connect(verbose, socket_ids, sockets, 355 address, addrlen); 356 if (rc != EOK) 357 return rc; 358 } 277 359 278 360 if (verbose) 279 361 printf("\n"); 280 362 281 if (ERROR_OCCURRED(gettimeofday(&time_before, NULL))) { 282 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 283 return ERROR_CODE; 284 } 285 286 ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages)); 287 288 if (ERROR_OCCURRED(gettimeofday(&time_after, NULL))) { 289 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 290 return ERROR_CODE; 363 rc = gettimeofday(&time_before, NULL); 364 if (rc != EOK) { 365 fprintf(stderr, "Get time of day error %d\n", rc); 366 return rc; 367 } 368 369 rc = sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, 370 &addrlen, data, size, messages); 371 if (rc != EOK) 372 return rc; 373 374 rc = gettimeofday(&time_after, NULL); 375 if (rc != EOK) { 376 fprintf(stderr, "Get time of day error %d\n", rc); 377 return rc; 291 378 } 292 379 … … 294 381 printf("\tOK\n"); 295 382 296 printf("sendto + recvfrom tested in %d microseconds\n", tv_sub(&time_after, &time_before)); 297 298 if (ERROR_OCCURRED(gettimeofday(&time_before, NULL))) { 299 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 300 return ERROR_CODE; 301 } 302 303 ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, sockets, address, addrlen, data, size, messages)); 304 ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages)); 305 306 if (ERROR_OCCURRED(gettimeofday(&time_after, NULL))) { 307 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 308 return ERROR_CODE; 383 printf("sendto + recvfrom tested in %d microseconds\n", 384 tv_sub(&time_after, &time_before)); 385 386 rc = gettimeofday(&time_before, NULL); 387 if (rc != EOK) { 388 fprintf(stderr, "Get time of day error %d\n", rc); 389 return rc; 390 } 391 392 rc = sockets_sendto(verbose, socket_ids, sockets, address, addrlen, 393 data, size, messages); 394 if (rc != EOK) 395 return rc; 396 397 rc = sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, 398 data, size, messages); 399 if (rc != EOK) 400 return rc; 401 402 rc = gettimeofday(&time_after, NULL); 403 if (rc != EOK) { 404 fprintf(stderr, "Get time of day error %d\n", rc); 405 return rc; 309 406 } 310 407 … … 312 409 printf("\tOK\n"); 313 410 314 printf("sendto, recvfrom tested in %d microseconds\n", tv_sub(&time_after, &time_before)); 315 316 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 411 printf("sendto, recvfrom tested in %d microseconds\n", 412 tv_sub(&time_after, &time_before)); 413 414 rc = sockets_close(verbose, socket_ids, sockets); 415 if (rc != EOK) 416 return rc; 317 417 318 418 if (verbose) -
uspace/app/redir/redir.c
r68f0c3a r9fd39d6 43 43 #include <task.h> 44 44 #include <str_error.h> 45 #include <errno.h> 45 46 46 47 #define NAME "redir" … … 76 77 static task_id_t spawn(int argc, char *argv[]) 77 78 { 78 const char **args = (const char **) calloc(argc + 1, sizeof(char *)); 79 const char **args; 80 task_id_t id; 81 int rc; 82 83 args = (const char **) calloc(argc + 1, sizeof(char *)); 79 84 if (!args) { 80 85 printf("No memory available\n"); … … 88 93 args[argc] = NULL; 89 94 90 int err; 91 task_id_t id = task_spawn(argv[0], args, &err); 95 rc = task_spawnv(&id, argv[0], args); 92 96 93 97 free(args); 94 98 95 if ( id == 0)99 if (rc != EOK) { 96 100 printf("%s: Error spawning %s (%s)\n", NAME, argv[0], 97 str_error(err)); 101 str_error(rc)); 102 } 98 103 99 104 return id; -
uspace/app/sbi/src/os/helenos.c
r68f0c3a r9fd39d6 249 249 task_id_t tid; 250 250 task_exit_t texit; 251 int r etval;252 253 tid = task_spawn(cmd[0], (char const * const *) cmd, &retval);254 if ( tid == 0) {251 int rc, retval; 252 253 rc = task_spawnv(&tid, cmd[0], (char const * const *) cmd); 254 if (rc != EOK) { 255 255 printf("Error: Failed spawning '%s' (%s).\n", cmd[0], 256 str_error(r etval));256 str_error(rc)); 257 257 exit(1); 258 258 } 259 259 260 260 /* XXX Handle exit status and return value. */ 261 task_wait(tid, &texit, &retval); 261 rc = task_wait(tid, &texit, &retval); 262 (void) rc; 262 263 263 264 return EOK; -
uspace/lib/c/generic/task.c
r68f0c3a r9fd39d6 39 39 #include <errno.h> 40 40 #include <loader/loader.h> 41 #include <stdarg.h> 41 42 #include <str.h> 42 43 #include <ipc/ns.h> … … 68 69 * 69 70 * This is really just a convenience wrapper over the more complicated 70 * loader API. 71 * 72 * @param path Pathname of the binary to execute. 73 * @param argv Command-line arguments. 74 * @param err If not NULL, the error value is stored here. 75 * 76 * @return ID of the newly created task or zero on error. 77 * 78 */ 79 task_id_t task_spawn(const char *path, const char *const args[], int *err) 80 { 71 * loader API. Arguments are passed as a null-terminated array of strings. 72 * 73 * @param id If not NULL, the ID of the task is stored here on success. 74 * @param path Pathname of the binary to execute. 75 * @param argv Command-line arguments. 76 * 77 * @return Zero on success or negative error code. 78 */ 79 int task_spawnv(task_id_t *id, const char *path, const char *const args[]) 80 { 81 loader_t *ldr; 82 task_id_t task_id; 83 int rc; 84 81 85 /* Connect to a program loader. */ 82 loader_t *ldr = loader_connect(); 83 if (ldr == NULL) { 84 if (err != NULL) 85 *err = EREFUSED; 86 87 return 0; 88 } 86 ldr = loader_connect(); 87 if (ldr == NULL) 88 return EREFUSED; 89 89 90 90 /* Get task ID. */ 91 task_id_t task_id; 92 int rc = loader_get_task_id(ldr, &task_id); 91 rc = loader_get_task_id(ldr, &task_id); 93 92 if (rc != EOK) 94 93 goto error; … … 149 148 free(ldr); 150 149 151 if ( err!= NULL)152 * err = EOK;153 154 return task_id;150 if (id != NULL) 151 *id = task_id; 152 153 return EOK; 155 154 156 155 error: … … 158 157 loader_abort(ldr); 159 158 free(ldr); 160 161 if (err != NULL) 162 *err = rc; 163 164 return 0; 159 return rc; 160 } 161 162 /** Create a new task by running an executable from the filesystem. 163 * 164 * This is really just a convenience wrapper over the more complicated 165 * loader API. Arguments are passed as a null-terminated list of arguments. 166 * 167 * @param id If not NULL, the ID of the task is stored here on success. 168 * @param path Pathname of the binary to execute. 169 * @param ... Command-line arguments. 170 * 171 * @return Zero on success or negative error code. 172 */ 173 int task_spawnl(task_id_t *task_id, const char *path, ...) 174 { 175 va_list ap; 176 int rc, cnt; 177 const char *arg; 178 const char **arglist; 179 180 /* Count the number of arguments. */ 181 cnt = 0; 182 va_start(ap, path); 183 do { 184 arg = va_arg(ap, const char *); 185 cnt++; 186 } while (arg != NULL); 187 va_end(ap); 188 189 /* Allocate argument list. */ 190 arglist = malloc(cnt * sizeof(const char *)); 191 if (arglist == NULL) 192 return ENOMEM; 193 194 /* Fill in arguments. */ 195 cnt = 0; 196 va_start(ap, path); 197 do { 198 arg = va_arg(ap, const char *); 199 arglist[cnt++] = arg; 200 } while (arg != NULL); 201 va_end(ap); 202 203 /* Spawn task. */ 204 rc = task_spawnv(task_id, path, arglist); 205 206 /* Free argument list. */ 207 free(arglist); 208 return rc; 165 209 } 166 210 -
uspace/lib/c/include/task.h
r68f0c3a r9fd39d6 48 48 extern int task_set_name(const char *); 49 49 extern task_id_t task_spawn(const char *, const char *const[], int *); 50 extern int task_spawnv(task_id_t *, const char *path, const char *const []); 51 extern int task_spawnl(task_id_t *, const char *path, ...); 52 50 53 extern int task_wait(task_id_t id, task_exit_t *, int *); 51 54 extern int task_retval(int); -
uspace/lib/net/adt/module_map.c
r68f0c3a r9fd39d6 132 132 task_id_t spawn(const char *fname) 133 133 { 134 const char *argv[2];135 task_id_t res;134 task_id_t id; 135 int rc; 136 136 137 argv[0] = fname;138 argv[1] = NULL;139 res = task_spawn(fname, argv, NULL);137 rc = task_spawnl(&id, fname, fname, NULL); 138 if (rc != EOK) 139 return 0; 140 140 141 return res;141 return id; 142 142 } 143 143 -
uspace/srv/devman/devman.c
r68f0c3a r9fd39d6 37 37 #include <ipc/devman.h> 38 38 #include <devmap.h> 39 #include <str_error.h> 39 40 40 41 #include "devman.h" … … 446 447 bool start_driver(driver_t *drv) 447 448 { 449 int rc; 450 448 451 printf(NAME ": start_driver '%s'\n", drv->name); 449 452 450 const char *argv[2]; 451 452 argv[0] = drv->name; 453 argv[1] = NULL; 454 455 int err; 456 if (task_spawn(drv->binary_path, argv, &err) == 0) { 457 printf(NAME ": error spawning %s, errno = %d\n", 458 drv->name, err); 453 rc = task_spawnl(NULL, drv->binary_path, drv->binary_path, NULL); 454 if (rc != EOK) { 455 printf(NAME ": error spawning %s (%s)\n", 456 drv->name, str_error(rc)); 459 457 return false; 460 458 } -
uspace/srv/taskmon/taskmon.c
r68f0c3a r9fd39d6 50 50 static void fault_event(ipc_callid_t callid, ipc_call_t *call) 51 51 { 52 const char *argv[6];53 52 const char *fname; 54 char *dump_fname;55 53 char *s_taskid; 56 const char **s;54 int rc; 57 55 58 56 task_id_t taskid; … … 67 65 } 68 66 67 printf(NAME ": Task %" PRIuTASKID " fault in thread %p.\n", taskid, thread); 68 69 fname = "/app/taskdump"; 70 71 #ifdef CONFIG_WRITE_CORE_FILES 72 char *dump_fname; 73 69 74 if (asprintf(&dump_fname, "/data/core%" PRIuTASKID, taskid) < 0) { 70 75 printf("Memory allocation failed.\n"); … … 72 77 } 73 78 74 printf(NAME ": Task %" PRIuTASKID " fault in thread %p.\n", taskid, thread); 75 76 #ifdef CONFIG_WRITE_CORE_FILES 77 argv[0] = "/app/taskdump"; 78 argv[1] = "-c"; 79 argv[2] = dump_fname; 80 argv[3] = "-t"; 81 argv[4] = s_taskid; 82 argv[5] = NULL; 79 printf(NAME ": Executing %s -c %s -t %s\n", dump_fname, s_taskid); 80 rc = task_spawnl(NULL, fname, fname, "-c", dump_fname, "-t", s_taskid, 81 NULL); 83 82 #else 84 argv[0] = "/app/taskdump"; 85 argv[1] = "-t"; 86 argv[2] = s_taskid; 87 argv[3] = NULL; 83 printf(NAME ": Executing %s -t %s\n", s_taskid); 84 rc = task_spawnl(NULL, fname, fname, "-t", s_taskid, NULL); 88 85 #endif 89 fname = argv[0]; 90 91 printf(NAME ": Executing"); 92 93 s = argv; 94 while (*s != NULL) { 95 printf(" %s", *s); 96 ++s; 86 if (rc != EOK) { 87 printf("%s: Error spawning %s (%s).\n", NAME, fname, 88 str_error(rc)); 97 89 } 98 putchar('\n');99 100 int err;101 if (!task_spawn(fname, argv, &err))102 printf("%s: Error spawning %s (%s).\n", NAME, fname,103 str_error(err));104 90 } 105 91
Note:
See TracChangeset
for help on using the changeset viewer.