author | viric <viriketo@gmail.com> |
Mon, 18 Jul 2011 22:48:54 +0200 | |
branch | queuelimit |
changeset 293 | bb87d5e7c466 |
parent 267 | 11631dd11ff8 |
child 345 | c8fd555369d1 |
permissions | -rw-r--r-- |
165
7b914d4003c2
Fixing licenses in some files, and updating some documents.
viric@mandarina
parents:
147
diff
changeset
|
1 |
/* |
7b914d4003c2
Fixing licenses in some files, and updating some documents.
viric@mandarina
parents:
147
diff
changeset
|
2 |
Task Spooler - a task queue system for the unix user |
267 | 3 |
Copyright (C) 2007-2009 LluĂs Batlle i Rossell |
165
7b914d4003c2
Fixing licenses in some files, and updating some documents.
viric@mandarina
parents:
147
diff
changeset
|
4 |
|
7b914d4003c2
Fixing licenses in some files, and updating some documents.
viric@mandarina
parents:
147
diff
changeset
|
5 |
Please find the license in the provided COPYING file. |
7b914d4003c2
Fixing licenses in some files, and updating some documents.
viric@mandarina
parents:
147
diff
changeset
|
6 |
*/ |
147 | 7 |
#include <stdio.h> |
8 |
#include <sys/time.h> |
|
9 |
#include <sys/types.h> |
|
10 |
#include <sys/wait.h> |
|
11 |
#include <unistd.h> |
|
12 |
#include <signal.h> |
|
13 |
#include <fcntl.h> |
|
14 |
#include <stdlib.h> |
|
15 |
#include "main.h" |
|
16 |
||
17 |
static int fork_command(const char *command) |
|
18 |
{ |
|
19 |
int pid; |
|
20 |
int p[2]; |
|
21 |
int fdnull; |
|
22 |
||
23 |
fdnull = open("/dev/null", O_RDONLY); |
|
24 |
if (fdnull == -1) error("Cannot open /dev/null"); |
|
25 |
||
26 |
pipe(p); |
|
27 |
||
28 |
pid = fork(); |
|
29 |
||
30 |
switch(pid) |
|
31 |
{ |
|
32 |
case 0: |
|
33 |
restore_sigmask(); |
|
34 |
close(server_socket); |
|
35 |
dup2(fdnull, 0); |
|
36 |
if (fdnull != 0) |
|
37 |
close(fdnull); |
|
38 |
dup2(p[1], 1); |
|
39 |
dup2(p[1], 2); |
|
40 |
if (p[1] != 1 && p[1] != 2) |
|
41 |
close(p[1]); |
|
42 |
close(p[0]); |
|
43 |
execlp("/bin/sh", "/bin/sh", "-c", command, 0); |
|
44 |
error("/bin/sh exec error"); |
|
45 |
case -1: |
|
46 |
error("Fork error"); |
|
47 |
default: |
|
48 |
close(p[1]); |
|
49 |
} |
|
50 |
||
51 |
return p[0]; |
|
52 |
} |
|
53 |
||
54 |
char * get_environment() |
|
55 |
{ |
|
56 |
char *ptr; |
|
57 |
char *command; |
|
58 |
int readfd; |
|
59 |
int bytes = 0; |
|
60 |
int alloc = 0; |
|
61 |
||
62 |
command = getenv("TS_ENV"); |
|
63 |
if (command == 0) |
|
64 |
return 0; |
|
65 |
||
66 |
readfd = fork_command(command); |
|
67 |
||
68 |
ptr = 0; |
|
69 |
do |
|
70 |
{ |
|
71 |
int next; |
|
72 |
int res; |
|
73 |
next = bytes + 1000; |
|
74 |
if (next > alloc) |
|
75 |
{ |
|
76 |
ptr = realloc(ptr, next); |
|
77 |
alloc = next; |
|
78 |
} |
|
79 |
res = read(readfd, ptr + bytes, 1000); |
|
80 |
if (res < 0) |
|
81 |
error("Cannot read from the TS_ENV command (%s)", command); |
|
82 |
else if (res == 0) |
|
83 |
break; |
|
84 |
else |
|
85 |
bytes += res; |
|
86 |
} while(1); |
|
87 |
||
88 |
/* We will always have 1000 bytes more to be written, on end. |
|
89 |
* We add a null for it to be an ASCIIZ string. */ |
|
90 |
ptr[bytes] = '\0'; |
|
91 |
||
92 |
close(readfd); |
|
93 |
wait(0); |
|
94 |
||
95 |
return ptr; |
|
96 |
} |