aboutsummaryrefslogtreecommitdiff
path: root/src/util/os_priority.c
diff options
context:
space:
mode:
authorSree Harsha Totakura <totakura@in.tum.de>2013-11-29 10:25:47 +0000
committerSree Harsha Totakura <totakura@in.tum.de>2013-11-29 10:25:47 +0000
commitd5f83fb8b9a2ae2797b7b0e46bf6cd346743f4b3 (patch)
treea9afdf6ba9fcc2051a5043b061ea9dc86779fb9f /src/util/os_priority.c
parentb0c50f22c52535650a7288c32a3ae3ed6336c32f (diff)
downloadgnunet-d5f83fb8b9a2ae2797b7b0e46bf6cd346743f4b3.tar.gz
gnunet-d5f83fb8b9a2ae2797b7b0e46bf6cd346743f4b3.zip
- move do_start_process to util/
Diffstat (limited to 'src/util/os_priority.c')
-rw-r--r--src/util/os_priority.c139
1 files changed, 139 insertions, 0 deletions
diff --git a/src/util/os_priority.c b/src/util/os_priority.c
index d772e3c87..0f2acca06 100644
--- a/src/util/os_priority.c
+++ b/src/util/os_priority.c
@@ -1329,6 +1329,145 @@ GNUNET_OS_start_process_v (int pipe_control,
1329 1329
1330 1330
1331/** 1331/**
1332 * Start a process. This function is similar to the GNUNET_OS_start_process_*
1333 * except that the @a filename and @argv can have whole strings which contain
1334 * the arguments. These arguments are to be separated by spaces and are parsed
1335 * in the order they appear. Arguments containing spaces can be used by
1336 * quoting them with @em ".
1337 *
1338 * @param pipe_control should a pipe be used to send signals to the child?
1339 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1340 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
1341 * must be NULL on platforms where dup is not supported
1342 * @param filename name of the binary. It is valid to have the arguments
1343 * in this string when they are separated by spaces.
1344 * @param ... more arguments. Should be of type <tt>char *</tt>. It is valid
1345 * to have the arguments in these strings when they are separated by
1346 * spaces.
1347 * @param argv NULL-terminated list of arguments to the process,
1348 * including the process name as the first argument
1349 * @return pointer to process structure of the new process, NULL on error
1350 */
1351struct GNUNET_OS_Process *
1352GNUNET_OS_start_process_s (int pipe_control,
1353 unsigned int std_inheritance,
1354 const SOCKTYPE * lsocks,
1355 const char *first_arg, ...)
1356{
1357 va_list ap;
1358 char **argv;
1359 unsigned int argv_size;
1360 const char *arg;
1361 const char *rpos;
1362 char *pos;
1363 char *cp;
1364 const char *last;
1365 struct GNUNET_OS_Process *proc;
1366 char *binary_path;
1367 int quote_on;
1368 unsigned int i;
1369 size_t len;
1370
1371 argv_size = 1;
1372 va_start (ap, first_arg);
1373 arg = first_arg;
1374 last = NULL;
1375 do
1376 {
1377 rpos = arg;
1378 quote_on = 0;
1379 while ('\0' != *rpos)
1380 {
1381 if ('"' == *rpos)
1382 {
1383 if (1 == quote_on)
1384 quote_on = 0;
1385 else
1386 quote_on = 1;
1387 }
1388 if ( (' ' == *rpos) && (0 == quote_on) )
1389 {
1390 if (NULL != last)
1391 argv_size++;
1392 last = NULL;
1393 rpos++;
1394 while (' ' == *rpos)
1395 rpos++;
1396 }
1397 if ( (NULL == last) && ('\0' != *rpos) ) // FIXME: == or !=?
1398 last = rpos;
1399 if ('\0' != *rpos)
1400 rpos++;
1401 }
1402 if (NULL != last)
1403 argv_size++;
1404 }
1405 while (NULL != (arg = (va_arg (ap, const char*))));
1406 va_end (ap);
1407
1408 argv = GNUNET_malloc (argv_size * sizeof (char *));
1409 argv_size = 0;
1410 va_start (ap, first_arg);
1411 arg = first_arg;
1412 last = NULL;
1413 do
1414 {
1415 cp = GNUNET_strdup (arg);
1416 quote_on = 0;
1417 pos = cp;
1418 while ('\0' != *pos)
1419 {
1420 if ('"' == *pos)
1421 {
1422 if (1 == quote_on)
1423 quote_on = 0;
1424 else
1425 quote_on = 1;
1426 }
1427 if ( (' ' == *pos) && (0 == quote_on) )
1428 {
1429 *pos = '\0';
1430 if (NULL != last)
1431 argv[argv_size++] = GNUNET_strdup (last);
1432 last = NULL;
1433 pos++;
1434 while (' ' == *pos)
1435 pos++;
1436 }
1437 if ( (NULL == last) && ('\0' != *pos)) // FIXME: == or !=?
1438 last = pos;
1439 if ('\0' != *pos)
1440 pos++;
1441 }
1442 if (NULL != last)
1443 argv[argv_size++] = GNUNET_strdup (last);
1444 last = NULL;
1445 GNUNET_free (cp);
1446 }
1447 while (NULL != (arg = (va_arg (ap, const char*))));
1448 va_end (ap);
1449 argv[argv_size] = NULL;
1450
1451 for(i = 0; i < argv_size; i++)
1452 {
1453 len = strlen (argv[i]);
1454 if ( (argv[i][0] == '"') && (argv[i][len-1] == '"'))
1455 {
1456 memmove (&argv[i][0], &argv[i][1], len - 2);
1457 argv[i][len-2] = '\0';
1458 }
1459 }
1460 binary_path = argv[0];
1461 proc = GNUNET_OS_start_process_v (pipe_control, std_inheritance, lsocks,
1462 binary_path, argv);
1463 while (argv_size > 0)
1464 GNUNET_free (argv[--argv_size]);
1465 GNUNET_free (argv);
1466 return proc;
1467}
1468
1469
1470/**
1332 * Retrieve the status of a process, waiting on him if dead. 1471 * Retrieve the status of a process, waiting on him if dead.
1333 * Nonblocking version. 1472 * Nonblocking version.
1334 * 1473 *