-
Notifications
You must be signed in to change notification settings - Fork 8
/
log.c
67 lines (59 loc) · 1.83 KB
/
log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// log.c
// FuseHFS
//
// Created by Joel Cretan on 7/8/14.
//
// Licensed under GPLv2: https://www.gnu.org/licenses/gpl-2.0.html
//
#include "common.h"
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <time.h>
#include <pwd.h>
#include <limits.h>
#include "log.h"
#define MAC_FIRST_USER 501
#define MEGABYTE 1 << 20
int log_to_file() {
char logpath[PATH_MAX];
// you can't write to /Library any more, so use ~/Library instead
char *home = getpwuid(MAC_FIRST_USER)->pw_dir;
if (strlen(home) + strlen(LOGPATH) >= PATH_MAX)
return -1;
strncpy(logpath, home, sizeof(logpath));
strcat(logpath, LOGPATH);
// delete old log if larger than 1MB so it doesn't get out of control
// if we can't...that's probably fine.
struct stat st;
int rc = stat(logpath, &st);
if (rc == 0) {
if (st.st_size > MEGABYTE) {
unlink(logpath);
}
}
int log = open(logpath, O_CREAT | O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR);
chown(logpath, MAC_FIRST_USER, -1); // it's inconvenient for root, the owner of this process, to own the log
if (log < 0) {
fprintf(stderr, "open errno: %d\n", errno);
return log;
}
if (dup2(log, STDERR_FILENO) < 0)
fprintf(stderr, "stderr dup2 errno: %d\n", errno);
fflush(stderr);
return log;
}
void log_invoking_command(char *filename, int argc, char *argv[]) {
time_t curr_time = time(NULL);
char *current_time = ctime(&curr_time);
current_time[strlen(current_time) - 1] = 0; // remove the \n at the end of the string
fprintf(stderr, "\n%s %s -- invoked with argv: ", filename, current_time);
for (int i = 0; i < argc; i++)
fprintf(stderr, "%s ", argv[i]);
fprintf(stderr, "\n");
fflush(stderr);
}