summaryrefslogtreecommitdiff
path: root/libvtv/vtv_utils.cc
diff options
context:
space:
mode:
authorCaroline Tice <ctice@gcc.gnu.org>2013-08-20 13:43:16 -0700
committerCaroline Tice <ctice@gcc.gnu.org>2013-08-20 13:43:16 -0700
commit8bc16536d66fce30459a3d35a4a648f4a9e95703 (patch)
tree6edb95b379595c8ad4d073f2353e858eaa4d6488 /libvtv/vtv_utils.cc
parentddfee90670e69a72c4446d02e7b3936f4d74d147 (diff)
downloadgcc-8bc16536d66fce30459a3d35a4a648f4a9e95703.tar.gz
Fix logging to not use /tmp or the current directory...
Fix logging to not use /tmp or the current directory; get the location for writing log files from an environment variable; use secure getenv whenever possible. From-SVN: r201890
Diffstat (limited to 'libvtv/vtv_utils.cc')
-rw-r--r--libvtv/vtv_utils.cc50
1 files changed, 40 insertions, 10 deletions
diff --git a/libvtv/vtv_utils.cc b/libvtv/vtv_utils.cc
index 480bfa308dc..9cf4b08dc24 100644
--- a/libvtv/vtv_utils.cc
+++ b/libvtv/vtv_utils.cc
@@ -31,6 +31,7 @@
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <execinfo.h>
#include <unistd.h>
@@ -38,24 +39,53 @@
#include "vtv_utils.h"
-/* This is the directory into which all vtable verication log files
- get written. */
-static const char * const logs_dir = "/tmp/vtv_logs";
-static int vtv_failures_log_fd = -1;
-
+#ifndef HAVE_SECURE_GETENV
+# ifdef HAVE___SECURE_GETENV
+# define secure_getenv __secure_getenv
+# else
+# define secure_getenv getenv
+# endif
+#endif
+static int vtv_failures_log_fd = -1;
/* This function takes the NAME of a log file to open, attempts to
open it in the logs_dir directory, and returns the resulting file
- decriptor. */
+ descriptor.
+
+ This function first checks to see if the user has specifed (via
+ the environment variable VTV_LOGS_DIR) a directory to use for the
+ vtable verification logs. If that fails, the function will open
+ the logs in the current directory.
+*/
int
__vtv_open_log (const char *name)
{
- char log_name[256];
- snprintf (log_name, sizeof (log_name), "%s/%s", logs_dir, name);
- mkdir (logs_dir, S_IRWXU);
- int fd = open (log_name, O_WRONLY | O_APPEND | O_CREAT, S_IRWXU);
+ char log_name[1024];
+ char log_dir[512];
+ uid_t user_id = getuid ();
+ pid_t process_id = getpid ();
+ char *logs_prefix;
+ bool logs_dir_specified = false;
+ int fd = -1;
+
+ logs_prefix = secure_getenv ("VTV_LOGS_DIR");
+ if (logs_prefix && strlen (logs_prefix) > 0)
+ {
+ logs_dir_specified = true;
+ mkdir (logs_prefix, S_IRWXU);
+ snprintf (log_dir, sizeof (log_dir), "%s/vtv_logs", logs_prefix);
+ mkdir (log_dir, S_IRWXU);
+
+ snprintf (log_name, sizeof (log_name), "%s/%d_%d_%s", log_dir,
+ (unsigned) user_id, (unsigned) process_id, name);
+ fd = open (log_name, O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW,
+ S_IRWXU);
+ }
+ else
+ fd = dup (2);
+
if (fd == -1)
__vtv_add_to_log (2, "Cannot open log file %s %s\n", name,
strerror (errno));