e4e640
commit 82349a5eb0e1122d5532a03367d91d5ee838722d
e4e640
Author: Serhei Makarov <smakarov@redhat.com>
e4e640
Date:   Fri Mar 1 11:16:32 2019 -0500
e4e640
e4e640
    Don't print to /dev/kmsg if it's unavailable
e4e640
    
e4e640
    * stapbpf/stapbpf.cxx (kmsg): initialize to NULL.
e4e640
    (prog_load): only print to kmsg if it's available.
e4e640
    (main): warn if kmsg is unavailable.
e4e640
e4e640
diff --git a/stapbpf/stapbpf.cxx b/stapbpf/stapbpf.cxx
e4e640
index 188724b..ae97d4b 100644
e4e640
--- a/stapbpf/stapbpf.cxx
e4e640
+++ b/stapbpf/stapbpf.cxx
e4e640
@@ -73,7 +73,7 @@ static int warnings = 1;
e4e640
 static int exit_phase = 0;
e4e640
 static int interrupt_message = 0;
e4e640
 static FILE *output_f = stdout;
e4e640
-static FILE *kmsg;
e4e640
+static FILE *kmsg = NULL;
e4e640
 
e4e640
 static const char *module_name;
e4e640
 static const char *module_basename;
e4e640
@@ -341,9 +341,12 @@ prog_load(Elf_Data *data, const char *name)
e4e640
   if (data->d_size % sizeof(bpf_insn))
e4e640
     fatal("program size not a multiple of %zu\n", sizeof(bpf_insn));
e4e640
 
e4e640
-  fprintf (kmsg, "%s (%s): stapbpf: %s, name: %s, d_size: %lu\n",
e4e640
-           module_basename, script_name, VERSION, name, (unsigned long)data->d_size);
e4e640
-  fflush (kmsg); // Otherwise, flush will only happen after the prog runs.
e4e640
+  if (kmsg != NULL)
e4e640
+    {
e4e640
+      fprintf (kmsg, "%s (%s): stapbpf: %s, name: %s, d_size: %lu\n",
e4e640
+               module_basename, script_name, VERSION, name, (unsigned long)data->d_size);
e4e640
+      fflush (kmsg); // Otherwise, flush will only happen after the prog runs.
e4e640
+    }
e4e640
   int fd = bpf_prog_load(prog_type, static_cast<bpf_insn *>(data->d_buf),
e4e640
 			 data->d_size, module_license, kernel_version);
e4e640
   if (fd < 0)
e4e640
@@ -1575,5 +1578,7 @@ main(int argc, char **argv)
e4e640
 
e4e640
-  kmsg = fopen("/dev/kmsg", "a");
e4e640
+  kmsg = fopen("/dev/kmsg", "w");
e4e640
+  if (kmsg == NULL)
e4e640
+    fprintf(stderr, "WARNING: could not open /dev/kmsg for diagnostics: %s\n", strerror(errno));
e4e640
 
e4e640
   load_bpf_file(argv[optind]);
e4e640
   init_internal_globals();
e4e640