Révision 52905275
p/multicpu1sec-c: use stdlib IO
| plugins/cpu/multicpu1sec-c.c | ||
|---|---|---|
| 95 | 95 |
/* fork ourselves if not asked otherwise */ |
| 96 | 96 |
char* no_fork = getenv("no_fork");
|
| 97 | 97 |
if (! no_fork || strcmp("1", no_fork)) {
|
| 98 |
if (fork()) return; |
|
| 98 |
if (fork()) return 0;
|
|
| 99 | 99 |
// we are the child, complete the daemonization |
| 100 | 100 |
|
| 101 | 101 |
/* Close standard IO */ |
| ... | ... | |
| 116 | 116 |
int f = open(PROC_STAT, O_RDONLY); |
| 117 | 117 |
|
| 118 | 118 |
/* open the spoolfile */ |
| 119 |
int cache_file = open(cache_filename, O_CREAT | O_APPEND | O_WRONLY); |
|
| 119 |
FILE* cache_file = fopen(cache_filename, "a"); |
|
| 120 |
if (!cache_file) {
|
|
| 121 |
return fail("cannot create cache_file");
|
|
| 122 |
} |
|
| 123 |
|
|
| 124 |
int cache_file_fd = fileno(cache_file); |
|
| 120 | 125 |
|
| 121 | 126 |
/* loop each second */ |
| 122 | 127 |
while (1) {
|
| ... | ... | |
| 142 | 147 |
line = strtok(buffer, newl); |
| 143 | 148 |
|
| 144 | 149 |
/* lock */ |
| 145 |
flock(cache_file, LOCK_EX); |
|
| 150 |
flock(cache_file_fd, LOCK_EX);
|
|
| 146 | 151 |
|
| 147 | 152 |
for (line = strtok(NULL, newl); line; line = strtok(NULL, newl)) {
|
| 148 | 153 |
// Not on CPU lines anymore |
| ... | ... | |
| 154 | 159 |
|
| 155 | 160 |
long used = usr + nice + sys + iowait + irq + softirq; |
| 156 | 161 |
|
| 157 |
char out_buffer[1024]; |
|
| 158 |
sprintf(out_buffer, "%s.value %ld:%ld\n", cpu_id, epoch, used); |
|
| 159 |
|
|
| 160 |
write(cache_file, out_buffer, strlen(out_buffer)); |
|
| 162 |
fprintf(cache_file, "%s.value %ld:%ld\n", cpu_id, epoch, used); |
|
| 163 |
fflush(cache_file); |
|
| 161 | 164 |
} |
| 162 | 165 |
|
| 163 | 166 |
/* unlock */ |
| 164 |
flock(cache_file, LOCK_UN); |
|
| 167 |
flock(cache_file_fd, LOCK_UN);
|
|
| 165 | 168 |
} |
| 166 | 169 |
|
| 167 |
close(cache_file); |
|
| 170 |
fclose(cache_file);
|
|
| 168 | 171 |
close(f); |
| 169 | 172 |
|
| 170 | 173 |
return 0; |
| ... | ... | |
| 172 | 175 |
|
| 173 | 176 |
int fetch() {
|
| 174 | 177 |
FILE* cache_file = fopen(cache_filename, "r+"); |
| 178 |
if (!cache_file) {
|
|
| 179 |
return fail("cannot read cache_file");
|
|
| 180 |
} |
|
| 175 | 181 |
|
| 176 | 182 |
/* lock */ |
| 177 |
flock(fileno(cache_file), LOCK_EX); |
|
| 183 |
int cache_file_fd = fileno(cache_file); |
|
| 184 |
flock(cache_file_fd, LOCK_EX); |
|
| 178 | 185 |
|
| 179 | 186 |
/* cat the cache_file to stdout */ |
| 180 | 187 |
char buffer[1024]; |
| ... | ... | |
| 182 | 189 |
printf("%s", buffer);
|
| 183 | 190 |
} |
| 184 | 191 |
|
| 185 |
ftruncate(fileno(cache_file), 0);
|
|
| 192 |
ftruncate(cache_file_fd, 0);
|
|
| 186 | 193 |
fclose(cache_file); |
| 187 | 194 |
|
| 188 | 195 |
return 0; |
Formats disponibles : Unified diff