Debian Codegraph @

packages/seacas/libraries/suplib/addlog.c

raw
1 /*
2 * Copyright(C) 1999-2023 National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8
9 #if !defined(WIN32) && !defined(__WIN32__) && !defined(_WIN32) && !defined(_MSC_VER) && \
10 !defined(__MINGW32__) && !defined(_WIN64) && !defined(__MINGW64__)
11 /* Currently we just disable this functionality for windows-based systems... */
12 #include <ctype.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include <sys/times.h>
17 #include <sys/utsname.h>
18
19 #include <time.h>
20 #include <unistd.h>
21
22 #ifndef __USE_XOPEN
23 #define __USE_XOPEN
24 #endif
25 #include <stdio.h>
26 #endif
27
28 #if defined(ADDC_)
29 void addlog_(char *name, int len)
30 #else
31 void addlog(char *name, int len)
32 #endif
33 {
34 #if !defined(WIN32) && !defined(__WIN32__) && !defined(_WIN32) && !defined(_MSC_VER) && \
35 !defined(__MINGW32__) && !defined(_WIN64) && !defined(__MINGW64__)
36 #define LEN 512
37 /* Don't log information if this environment variable is set */
38 if (getenv("SEACAS_NO_LOGGING") != NULL) {
39 fprintf(stderr, "SEACAS Audit logging disabled via SEACAS_NO_LOGGING setting.\n");
40 return;
41 }
42
43 /* Now try to find the $ACCESS/etc/audit.log file */
44 /* Don't need to try too hard since information is not critical; just useful */
45 char *access_dir = getenv("ACCESS");
46 if (access_dir != NULL) {
47 char filename[LEN];
48 snprintf(filename, LEN, "%s/etc/audit.log", access_dir);
49 FILE *audit = fopen(filename, "a");
50 if (audit != NULL) {
51 char *codename = strrchr(name, '/');
52
53 const char *username = getlogin();
54 if (username == NULL) {
55 username = getenv("LOGNAME");
56 }
57 if (username == NULL) {
58 username = "UNKNOWN";
59 }
60
61 if (codename == NULL) {
62 codename = name;
63 }
64 else {
65 codename++;
66 }
67 char time_string[LEN];
68 time_t calendar_time = time(NULL);
69 struct tm *local_time = localtime(&calendar_time);
70 strftime(time_string, LEN, "%a %b %d %H:%M:%S %Z %Y", local_time);
71
72 int ticks_per_second;
73 struct tms time_buf;
74 times(&time_buf);
75 ticks_per_second = sysconf(_SC_CLK_TCK);
76 double u_time = (double)(time_buf.tms_utime + time_buf.tms_cutime) / ticks_per_second;
77 double s_time = (double)(time_buf.tms_stime + time_buf.tms_cstime) / ticks_per_second;
78
79 struct utsname sys_info;
80 uname(&sys_info);
81
82 char log_string[LEN];
83 for (size_t i = 0; i < strlen(codename); i++) {
84 if (!isalnum(codename[i]) && codename[i] != '.' && codename[i] != '-' &&
85 codename[i] != '_') {
86 codename[i] = '\0';
87 break;
88 }
89 }
90 snprintf(log_string, LEN, "%s %s %s %.3fu %.3fs 0:00.00 0.0%% 0+0k 0+0io 0pf+0w %s\n",
91 codename, username, time_string, u_time, s_time, sys_info.nodename) < 0
92 ? abort()
93 : (void)0;
94
95 fprintf(audit, "%s", log_string);
96 fclose(audit);
97 }
98 }
99 #else
100 /* Try to avoid unused variable warning/error on windows-based system */
101 (void)name;
102 (void)len;
103 #endif
104 }