"Fossies" - the Fresh Open Source Software Archive

Member "systemtap-4.3/staprun/staprun.c" (11 Jun 2020, 25928 Bytes) of package /linux/misc/systemtap-4.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "staprun.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 4.2_vs_4.3.

    1 /* -*- linux-c -*-
    2  *
    3  * staprun.c - SystemTap module loader
    4  *
    5  * Copyright (C) 2005-2019 Red Hat, Inc.
    6  *
    7  * This program is free software; you can redistribute it and/or modify
    8  * it under the terms of the GNU General Public License as published by
    9  * the Free Software Foundation; either version 2 of the License, or
   10  * (at your option) any later version.
   11  *
   12  * This program is distributed in the hope that it will be useful,
   13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15  * GNU General Public License for more details.
   16  *
   17  * You should have received a copy of the GNU General Public License
   18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   19  *
   20  */
   21 
   22 #define _XOPEN_SOURCE
   23 #define _BSD_SOURCE
   24 #define _DEFAULT_SOURCE
   25 #include "staprun.h"
   26 #include "../privilege.h"
   27 #include "../runtime/k_syms.h"
   28 #include <string.h>
   29 #include <sys/uio.h>
   30 #include <glob.h>
   31 #include <time.h>
   32 #include <sys/prctl.h>
   33 #include <sys/utsname.h>
   34 
   35 /* used in dbug, _err and _perr */
   36 char *__name__ = "staprun";
   37 
   38 extern long delete_module(const char *, unsigned int);
   39 
   40 int send_relocations ();
   41 int send_tzinfo ();
   42 int send_privilege_credentials (privilege_t user_credentials);
   43 int send_remote_id ();
   44 
   45 static int remove_module(const char *name, int verb);
   46 
   47 static int stap_module_inserted = -1;
   48 
   49 static void term_signal_handler(int signum __attribute ((unused)))
   50 {
   51     if (stap_module_inserted == 0) {
   52         remove_module(modname, 1);
   53         free(modname);
   54     }
   55     _exit(1);
   56 }
   57 
   58 void setup_term_signals(void)
   59 {
   60     sigset_t s;
   61     struct sigaction a;
   62 
   63     /* blocking all signals while we set things up */
   64     sigfillset(&s);
   65     sigprocmask(SIG_SETMASK, &s, NULL);
   66 
   67     /* handle signals */
   68     memset(&a, 0, sizeof(a));
   69     sigfillset(&a.sa_mask);
   70     a.sa_handler = term_signal_handler;
   71     sigaction(SIGHUP, &a, NULL);
   72     sigaction(SIGINT, &a, NULL);
   73     sigaction(SIGTERM, &a, NULL);
   74     sigaction(SIGQUIT, &a, NULL);
   75 
   76     /* unblock all signals */
   77     sigemptyset(&s);
   78     sigprocmask(SIG_SETMASK, &s, NULL);
   79 }
   80 
   81 static int run_as(int exec_p, uid_t uid, gid_t gid, const char *path, char *const argv[])
   82 {
   83     pid_t pid;
   84     int rstatus;
   85 
   86     if (verbose >= 2) {
   87         int i = 0;
   88         eprintf(exec_p ? "execing: ": "spawning: ");
   89         while (argv[i]) {
   90             eprintf("%s ", argv[i]);
   91             i++;
   92         }
   93         eprintf("\n");
   94     }
   95 
   96         if (exec_p)
   97           pid = 0;
   98         else
   99           pid = fork();
  100 
  101         if (pid < 0)
  102           {
  103             _perr("fork");
  104             return -1;
  105           }
  106 
  107     if (pid == 0) /* child process, or exec_p */
  108           {
  109             /* Make sure we run as the full user.  If we're
  110              * switching to a non-root user, this won't allow
  111              * that process to switch back to root (since the
  112              * original process is setuid). */
  113             if (setresgid (gid, gid, gid) < 0) {
  114               _perr("setresgid");
  115               exit(1);
  116             }
  117             if (setresuid (uid, uid, uid) < 0) {
  118               _perr("setresuid");
  119               exit(1);
  120             }
  121 
  122             /* Actually run the command. */
  123             if (execv(path, argv) < 0)
  124               perror(path);
  125             _exit(1);
  126           }
  127 
  128     if (waitpid(pid, &rstatus, 0) < 0)
  129           return -1;
  130 
  131     if (WIFEXITED(rstatus))
  132           return WEXITSTATUS(rstatus);
  133     return -1;
  134 }
  135 
  136 /*
  137  * Module to be inserted has one or more user-space probes.  Make sure
  138  * uprobes is enabled.
  139  * If /proc/kallsyms lists a symbol in uprobes (e.g. unregister_uprobe),
  140  * we're done.
  141  * Else try "modprobe uprobes" to load the uprobes module (if any)
  142  * built with the kernel.
  143  * If that fails, load the uprobes module built in runtime/uprobes.
  144  */
  145 static int enable_uprobes(void)
  146 {
  147     char *argv[10];
  148     char runtimeko[2048];
  149         int rc;
  150 
  151         /* Formerly, we did a grep /proc/kallsyms search to see if
  152            uprobes was already loaded into the kernel.  But this is
  153            a race waiting to happen.  Just try to load the thing.
  154            Quietly accept a -EEXIST error. */
  155 
  156         /* NB: don't use /sbin/modprobe, without more env. sanitation. */
  157 
  158     /* Try the specified module or the one from the runtime.  */
  159     if (uprobes_path)
  160       snprintf (runtimeko, sizeof(runtimeko), "%s", uprobes_path);
  161     else
  162           /* NB: since PR5163, share/runtime/uprobes/uprobes.ko is not built 
  163              by systemtap. */
  164       snprintf (runtimeko, sizeof(runtimeko), "%s/uprobes/uprobes.ko",
  165             (getenv("SYSTEMTAP_RUNTIME") ?: PKGDATADIR "/runtime"));
  166     dbug(2, "Inserting uprobes module from %s.\n", runtimeko);
  167     /* This module may be signed, so use insert_module to load it.  */
  168     argv[0] = NULL;
  169 
  170     rc = insert_module(runtimeko, NULL, argv, assert_uprobes_module_permissions, NULL);
  171         if ((rc == 0) || /* OK */
  172             (rc == -EEXIST)) /* Someone else might have loaded it */
  173         return 0;
  174 
  175         err("Couldn't insert module '%s': %s\n", runtimeko, moderror(errno));
  176     return 1; /* failure */
  177 }
  178 
  179 static int insert_stap_module(privilege_t *user_credentials)
  180 {
  181     char special_options[128];
  182         int rc, fips_mode_fd;
  183         char fips_mode = '0';
  184         char *misc = "";
  185 
  186     /* Add the _stp_bufsize option.  */
  187     if (snprintf_chk(special_options, sizeof (special_options),
  188              "_stp_bufsize=%d", (int)buffer_size))
  189         return -1;
  190 
  191         fips_mode_fd = open("/proc/sys/crypto/fips_enabled", O_RDONLY);
  192         if (fips_mode_fd >= 0) {
  193                 char c;
  194                 rc = read(fips_mode_fd, &c, 1);
  195                 if (rc == 1) fips_mode = c;
  196                 close (fips_mode_fd);
  197         }
  198 
  199         /* In FIPS mode, a kernel may panic if given an improperly-signed module.
  200            Right now, we have no way of signing them with the kernel build-time keys,
  201            so we punt.  See also SecureBoot. */
  202         if ((fips_mode != '0') && !getenv("STAP_FIPS_OVERRIDE")) {
  203                 errno = EPERM;
  204                 stap_module_inserted = -1;
  205                 misc = "in FIPS mode ";
  206         } else {
  207             stap_module_inserted = insert_module(modpath, special_options,
  208                                                      modoptions,
  209                                                      assert_stap_module_permissions,
  210                                                      user_credentials);
  211         }
  212         if (stap_module_inserted != 0)
  213                 err("Couldn't insert module %s'%s': %s\n", misc, modpath, moderror(errno));
  214     return stap_module_inserted;
  215 }
  216 
  217 static void remove_all_modules(void)
  218 {
  219     char *base;
  220     struct statfs st;
  221     struct dirent *d;
  222     DIR *moddir;
  223 
  224         /* NB: nothing to do with PR14245 */
  225     if (statfs("/sys/kernel/debug", &st) == 0 && (int)st.f_type == (int)DEBUGFS_MAGIC)
  226         base = "/sys/kernel/debug/systemtap";
  227     else
  228         base = "/proc/systemtap";
  229 
  230     moddir = opendir(base);
  231     if (moddir) {
  232         while ((d = readdir(moddir))) {
  233                         if (strcmp(d->d_name, ".") == 0) continue;
  234                         if (strcmp(d->d_name, "..") == 0) continue;
  235                         relay_basedir_fd = -1; /* each time! */
  236             if (remove_module(d->d_name, 0) == 0)
  237                 printf("Module %s removed.\n", d->d_name);
  238                 }
  239         closedir(moddir);
  240     }
  241 }
  242 
  243 static int remove_module(const char *name, int verb)
  244 {
  245     int ret;
  246     dbug(2, "%s\n", name);
  247 
  248 #ifdef PR_SET_NAME
  249         /* Make self easier to identify in vmcrash images */
  250         prctl (PR_SET_NAME, "staprun-d");
  251 #endif
  252 
  253         (void) verb; /* XXX: ignore */
  254 
  255     if (strcmp(name, "*") == 0) {
  256         remove_all_modules();
  257         return 0;
  258     }
  259 
  260         /* We call init_ctl_channel/close_ctl_channel to check whether
  261            the module is a systemtap-built one (having the right files),
  262            and that it's already unattached (because otherwise it'd EBUSY
  263            the opens, and that it belongs to our uid (because otherwise
  264            a faccessat(2) test on the .cmd file will fail). */
  265         ret = init_ctl_channel (name, 0);
  266         if (ret < 0) {
  267                 err("'%s' is not a zombie systemtap module.\n", name);
  268                 return ret;
  269         }
  270         close_ctl_channel ();
  271 
  272     dbug(2, "removing module %s\n", name);
  273     PROBE1(staprun, remove__module, name);
  274     ret = delete_module (name, O_NONBLOCK);
  275     if (ret != 0) {
  276                 /* XXX: maybe we should just accept this, with a
  277                    diagnostic, but without an error.  Might it be
  278                    possible for the same module to be started up just
  279                    as we're shutting down?  */
  280         err("Couldn't remove module '%s': %s.\n", name, strerror(errno));
  281         return 1;
  282     }
  283 
  284     dbug(1, "Module %s removed.\n", name);
  285     return 0;
  286 }
  287 
  288 
  289 /* As per PR13193 & PR1548, some kernels have a buggy
  290    kprobes-optimization code, which results in BUG/panics in certain
  291    circumstances.  We turn off kprobes optimization as a conservative
  292    measure, unless told otherwise by an environment variable.
  293 */
  294 void disable_kprobes_optimization()
  295 {
  296         /* Test if the file exists at all. */
  297         const char* proc_kprobes = "/proc/sys/debug/kprobes-optimization";
  298         char prev;
  299         int rc, fd;
  300         struct utsname uts;
  301 
  302         /* PR13814; disable this facility for new enough kernels, containing
  303          * these fix commits: 86b4ce31 46484688 3f33ab1c */
  304         /* PR15484; whoops, not enough, problem still seen on Debian
  305          * 3.8.12 kernel. */
  306         if (0 && (uname (&uts) == 0) && (strverscmp (uts.release, "3.4") >= 0))
  307                 return;
  308         /* Disable kprobes optimization due to problems seen on F29 5.0 kernel.
  309            PR24416; RCU hang detection with uprobes_onthefly.exp. */
  310         /* RHBZ1697531 - x86 kprobe optimization causes rcu hang */
  311         if ((0 && uname (&uts) == 0) && (strverscmp (uts.release, "4.8") >= 0))
  312                 return;
  313 
  314         if (getenv ("STAP_PR13193_OVERRIDE"))
  315                 return;
  316 
  317         /* See the initial state; if it's already disabled, we do nothing. */
  318         fd = open (proc_kprobes, O_RDONLY);
  319         if (fd < 0) 
  320                 return;
  321         rc = read (fd, &prev, sizeof(prev));
  322         (void) close (fd);
  323         if (rc < 1 || prev == '0') /* Already disabled or unavailable */
  324                 return;
  325 
  326         fd = open (proc_kprobes, O_WRONLY);
  327         if (fd < 0) 
  328                 return;
  329         prev = '0'; /* really, next */
  330         rc = write (fd, &prev, sizeof(prev));
  331         (void) close (fd);
  332         if (rc == 1)
  333                 dbug(1, "Disabled %s.\n", proc_kprobes);
  334         else
  335                 dbug(1, "Error %d/%d disabling %s.\n", rc, errno, proc_kprobes);
  336 }
  337 
  338 
  339 /* BZ1552745: /proc/sys/kernel/kptr_restrict makes /sys/module
  340    ... addresses unreliable on 2018+ kernels.  circumstances.  We
  341    tweak this security measure (setting it to '1'), unless told
  342    otherwise by an environment variable.  We could turn it back later,
  343    but this would create a race condition between concurrent runs of
  344    staprun.  The '1' setting is nominally more secure than the default
  345    '0', except that for /sys/module/$MODULE/sections/$SECTION the '0'
  346    case produces obfuscated 0-based pointers, and '1' produces good
  347    ones (to a root user).  Strange but true.
  348 */
  349 void tweak_kptr_restrict()
  350 {
  351         const char* proc_kptr = "/proc/sys/kernel/kptr_restrict";
  352         char prev;
  353         int rc, fd;
  354         struct utsname uts;
  355 
  356         /* Relevant change appears to have been introduced in v4.15 in
  357          * commit ef0010a30935de4e0211. */
  358         if ((uname (&uts) == 0) && (strverscmp (uts.release, "4.15") < 0))
  359                 return;
  360 
  361         if (getenv ("STAP_BZ1552745_OVERRIDE"))
  362                 return;
  363 
  364         /* See the initial state; if it's already set, we do nothing. */
  365         fd = open (proc_kptr, O_RDONLY);
  366         if (fd < 0) 
  367                 return;
  368         rc = read (fd, &prev, sizeof(prev));
  369         (void) close (fd);
  370         if (rc < 1 || prev == '1') /* Already set or unavailable */
  371                 return;
  372 
  373         fd = open (proc_kptr, O_WRONLY);
  374         if (fd < 0) 
  375                 return;
  376         prev = '1'; /* really, next */
  377         rc = write (fd, &prev, sizeof(prev));
  378         (void) close (fd);
  379         if (rc == 1)
  380                 dbug(1, "Set %s.\n", proc_kptr);
  381         else
  382                 dbug(1, "Error %d/%d setting %s.\n", rc, errno, proc_kptr);
  383 }
  384 
  385 
  386 
  387 int init_staprun(void)
  388 {
  389     privilege_t user_credentials = pr_unknown;
  390     int rc;
  391     dbug(2, "init_staprun\n");
  392 
  393     if (mountfs() < 0)
  394         return -1;
  395 
  396     rc = 0;
  397     if (delete_mod)
  398         exit(remove_module(modname, 1));
  399         if (attach_mod) {
  400                 /* PR14245: prime the relay_basedir_fd pump. */
  401         rc = init_ctl_channel (modname, 0);
  402         if (rc >= 0)
  403           close_ctl_channel ();
  404         } else /* if (!attach_mod) */ {
  405         if (need_uprobes && enable_uprobes() != 0)
  406             return -1;
  407 
  408                 disable_kprobes_optimization();
  409 
  410         if (insert_stap_module(& user_credentials) < 0) {
  411             if(!rename_mod && errno == EEXIST)
  412                 err("Rerun with staprun option '-R' to rename this module.\n");
  413             return -1;
  414         }
  415         rc = init_ctl_channel (modname, 0);
  416         if (rc >= 0) {
  417           /* If we are unable to send privilege credentials then we have an old
  418              (pre 1.7) stap module or a non-stap module. In either case, the privilege
  419              credentials required for loading the module have already been determined and
  420              checked (see check_groups, get_module_required_credentials).
  421           */
  422           send_privilege_credentials(user_credentials);
  423           rc = send_relocations();
  424           if (rc == 0) {
  425             rc = send_tzinfo();
  426             if (rc == 0 && remote_id >= 0)
  427               send_remote_id();
  428           }
  429           close_ctl_channel ();
  430         }
  431         if (rc != 0)
  432           remove_module(modname, 1);
  433     }
  434     return rc;
  435 }
  436 
  437 int main(int argc, char **argv)
  438 {
  439     int rc;
  440 
  441     /* NB: Don't do the geteuid()!=0 check here, since we want to
  442        test command-line error-handling while running non-root. */
  443     /* Get rid of a few standard environment variables (which */
  444     /* might cause us to do unintended things). */
  445     rc = unsetenv("IFS") || unsetenv("CDPATH") || unsetenv("ENV")
  446         || unsetenv("BASH_ENV");
  447     if (rc) {
  448         _perr("unsetenv failed");
  449         exit(-1);
  450     }
  451 
  452     if (getuid() != geteuid()) { /* setuid? */
  453         rc = unsetenv("SYSTEMTAP_STAPRUN") ||
  454                   unsetenv("SYSTEMTAP_STAPIO") ||
  455                   unsetenv("SYSTEMTAP_RUNTIME");
  456 
  457         if (rc) {
  458             _perr("unsetenv failed");
  459             exit(-1);
  460         }
  461     }
  462 
  463     setup_signals();
  464     setup_term_signals();
  465 
  466     parse_args(argc, argv);
  467 
  468         /* PR14245, For security reasons, preclude "staprun -F fd".
  469            The -F option is only for stapio, but the overzealous quest
  470            for commonality doesn't let us express that nicer. */
  471         if (relay_basedir_fd >= 0) {
  472                 err(_("Relay basedir -F option is invalid for staprun\n"));
  473                 exit(1);
  474         }
  475         /* NB: later on, some of our own code may set relay_basedir_fd, for
  476            passing onto stapio - or for our own reuse.  That's OK.  */
  477 
  478 
  479     if (buffer_size)
  480         dbug(2, "Using a buffer of %u MB.\n", buffer_size);
  481 
  482     int mod_optind = optind;
  483     if (optind < argc) {
  484         parse_modpath(argv[optind++]);
  485         dbug(2, "modpath=\"%s\", modname=\"%s\"\n", modpath, modname);
  486     }
  487 
  488     if (optind < argc) {
  489         if (attach_mod) {
  490             err("Cannot have module options with attach (-A).\n");
  491             usage(argv[0],1);
  492         } else {
  493             unsigned start_idx = 0;
  494             while (optind < argc && start_idx + 1 < MAXMODOPTIONS)
  495                 modoptions[start_idx++] = argv[optind++];
  496             modoptions[start_idx] = NULL;
  497         }
  498     }
  499 
  500     if (modpath == NULL || *modpath == '\0') {
  501         err("Need a module name or path to load.\n");
  502         usage(argv[0],1);
  503     }
  504 
  505     if (geteuid() != 0) {
  506         err("The effective user ID of staprun must be set to the root user.\n"
  507             "  Check permissions on staprun and ensure it is a setuid root program.\n");
  508         exit(1);
  509     }
  510 
  511     char verbose_level[33];
  512     sprintf(verbose_level, "%d", verbose);
  513     rc = setenv("SYSTEMTAP_VERBOSE", verbose_level, 0);
  514     if (rc) {
  515         _perr("SYSTEMTAP_VERBOSE setenv failed");
  516         exit(-1);
  517     }
  518 
  519     if (init_staprun())
  520         exit(1);
  521 
  522     argv[0] = getenv ("SYSTEMTAP_STAPIO") ?: PKGLIBDIR "/stapio";
  523 
  524     /* Copy nenamed modname into argv */
  525     if(rename_mod)
  526         argv[mod_optind] = modname;
  527 
  528         /* PR14245: pass -F fd to stapio. Unfortunately, this requires
  529            us to extend argv[], with all the C fun that entails. */
  530 #ifdef HAVE_OPENAT
  531         if (relay_basedir_fd >= 0) {
  532                 char ** new_argv = calloc(sizeof(char *),argc+2);
  533                 const int new_Foption_size = 10; /* -FNNNNN */
  534                 char * new_Foption = malloc(new_Foption_size);
  535                 int i;
  536 
  537                 if (new_argv && new_Foption) {
  538                         snprintf (new_Foption, new_Foption_size, "-F%d", relay_basedir_fd);
  539                         for (i=0; i < argc && argv[i] != NULL; i++)
  540                                 new_argv[i] = argv[i];
  541                         new_argv[i++] = new_Foption; /* overwrite the NULL */
  542                         new_argv[i++] = NULL; /* ensconce a new NULL */
  543 
  544                         argv = new_argv;
  545                 }
  546         }
  547 #endif
  548 
  549     /* Run stapio */
  550     if (run_as (1, getuid(), getgid(), argv[0], argv) < 0) {
  551         perror(argv[0]);
  552         goto err;
  553     }
  554 
  555     free(modname);
  556     return 0;
  557 
  558 err:
  559     remove_module(modname, 1);
  560     free(modname);
  561     return 1;
  562 }
  563 
  564 
  565 
  566 /* Send a variety of relocation-related data to the kernel: for the
  567    kernel proper, just the "_stext" symbol address; for all loaded
  568    modules, a variety of symbol base addresses.
  569 
  570    We do this under protest.  The kernel ought expose this data to
  571    modules such as ourselves, but instead the upstream community
  572    continually shrinks its module-facing interfaces, including this
  573    stuff, even when users exist.
  574 
  575    PR26074: as of kernel 5.7+ / commit 0bd476e6c671 and under further
  576    protest, we must also send the address of kallsyms_lookup_name and
  577    kallsyms_for_each_symbol.
  578 */
  579 
  580 
  581 int send_a_relocation (const char* module, const char* reloc, unsigned long long address)
  582 {
  583   struct _stp_msg_relocation msg;
  584   int rc;
  585 
  586   if (strlen(module) >= STP_MODULE_NAME_LEN-1) {
  587           dbug (1, "module name too long: %s\n", module);
  588           return -EINVAL; 
  589   }
  590   strncpy (msg.module, module, STP_MODULE_NAME_LEN - 1);
  591   
  592   if (strlen(reloc) >= STP_SYMBOL_NAME_LEN-1) {
  593           dbug (1, "reloc name too long: %s\n", reloc);
  594           return -EINVAL; 
  595   }
  596   strncpy (msg.reloc, reloc, STP_MODULE_NAME_LEN - 1);
  597 
  598   msg.address = address;
  599 
  600   rc = send_request (STP_RELOCATION, & msg, sizeof (msg));
  601   if (rc != 0)
  602     perror ("Unable to send relocation");
  603   return rc;
  604 }
  605 
  606 
  607 int send_relocation_kernel ()
  608 {
  609   FILE* kallsyms;
  610   int rc = 0;
  611 
  612   errno = 0;
  613   kallsyms = fopen ("/proc/kallsyms", "r");
  614   if (kallsyms == NULL)
  615     {
  616       perror("cannot open /proc/kallsyms");
  617       // ... and the kernel module will almost certainly fail to initialize.
  618       return errno;
  619     }
  620   else
  621     {
  622       int found_stext = 0;
  623       int found_kallsyms_lookup_name = 0;
  624       int found_kallsyms_on_each_symbol = 0;
  625       int done_with_kallsyms = 0;
  626       char *line = NULL;
  627       size_t linesz = 0;
  628       while (! feof(kallsyms) && !done_with_kallsyms)
  629         {
  630           ssize_t linesize = getline (& line, & linesz, kallsyms);
  631           if (linesize > 0)
  632             {
  633               unsigned long long address;
  634           int pos = -1;
  635               if (sscanf (line, "%llx %*c %n", &address, &pos) != 1
  636                   || pos == -1)
  637                 continue; // no symbols here
  638           if (linesize - pos == sizeof KERNEL_RELOC_SYMBOL
  639           && !strcmp(line + pos, KERNEL_RELOC_SYMBOL "\n"))
  640                 {
  641                   /* NB: even on ppc, we use the _stext relocation name. */
  642                   rc = send_a_relocation ("kernel", "_stext", address);
  643           if (rc != 0)
  644             break;
  645 
  646                   found_stext=1;
  647                 }
  648               else if (linesize - pos == sizeof "kallsyms_lookup_name"
  649                        && !strcmp(line + pos, "kallsyms_lookup_name" "\n"))
  650                 {
  651                   rc = send_a_relocation ("kernel", "kallsyms_lookup_name", address);
  652           if (rc != 0) // non fatal, follows perror()
  653                     dbug(1, "Relocation was kallsyms_lookup_name=%llx\n", address);
  654 
  655                   found_kallsyms_lookup_name = 1;
  656                 }
  657               else if (linesize - pos == sizeof "kallsyms_on_each_symbol"
  658                        && !strcmp(line + pos, "kallsyms_on_each_symbol" "\n"))
  659                 {
  660                   rc = send_a_relocation ("kernel", "kallsyms_on_each_symbol", address);
  661           if (rc != 0) // non fatal, follows perror()
  662                     dbug(1, "Relocation was reloc kallsyms_on_each_symbol=%llx\n", address);
  663 
  664                   found_kallsyms_on_each_symbol = 1;
  665                 }
  666             }
  667           done_with_kallsyms = found_stext
  668             && found_kallsyms_lookup_name
  669             && found_kallsyms_on_each_symbol;
  670         }
  671       free (line);
  672       fclose (kallsyms);
  673 
  674       /* PR26074: Arguably, failure to find the kallsyms_* symbols may
  675        * not be a fatal error. The fallback kallsyms_lookup_name()
  676        * function in sym.c then returns 0, but it's barely conceivable
  677        * some modules never call it. */
  678       /* if (!done_with_kallsyms) */
  679       if (!found_stext)
  680         return rc;
  681 
  682       /* detect note section, send flag if there
  683        * NB: address=2 represents existed note, the real one in _stp_module
  684        */
  685       if (!access("/sys/kernel/notes", R_OK))
  686     rc = send_a_relocation ("kernel", ".note.gnu.build-id", 2);
  687     }
  688 
  689   return rc;
  690 }
  691 
  692 
  693 int send_relocation_modules ()
  694 {
  695   unsigned i = 0;
  696   glob_t globbuf;
  697   globbuf.gl_pathc = 0;
  698   int r = glob("/sys/module/*/sections/*", GLOB_PERIOD, NULL, &globbuf);
  699 
  700   if (r == GLOB_NOSPACE || r == GLOB_ABORTED)
  701     return r;
  702 
  703   for (i=0; i<globbuf.gl_pathc; i++)
  704     {
  705       char *module_section_file;
  706       char *section_name;
  707       char *module_name;
  708       char *module_name_end;
  709       FILE* secfile;
  710       unsigned long long section_address;
  711 
  712       module_section_file = globbuf.gl_pathv[i];
  713 
  714       /* Tokenize the file name.
  715          Sample gl_pathv[]: /sys/modules/zlib_deflate/sections/.text
  716          Pieces:                         ^^^^^^^^^^^^          ^^^^^
  717       */
  718       section_name = strrchr (module_section_file, '/');
  719       if (! section_name) continue;
  720       section_name ++;
  721 
  722       if (!strcmp (section_name, ".")) continue;
  723       if (!strcmp (section_name, "..")) continue;
  724 
  725       module_name = strchr (module_section_file, '/');
  726       if (! module_name) continue;
  727       module_name ++;
  728       module_name = strchr (module_name, '/');
  729       if (! module_name) continue;
  730       module_name ++;
  731       module_name = strchr (module_name, '/');
  732       if (! module_name) continue;
  733       module_name ++;
  734 
  735       module_name_end = strchr (module_name, '/');
  736       if (! module_name_end) continue;
  737 
  738       secfile = fopen (module_section_file, "r");
  739       if (! secfile) continue;
  740 
  741       if (1 == fscanf (secfile, "0x%llx", &section_address))
  742         {
  743           /* Now we destructively modify the string, but by now the file
  744              is open so we won't need the full name again. */
  745           *module_name_end = '\0';
  746 
  747           /* PR6503.  /sys/module/.../sections/...init.... sometimes contain
  748              non-0 addresses, even though the respective module-initialization
  749              sections were already unloaded.  We override the addresses here. */
  750           if (strstr (section_name, "init.") != NULL) /* .init.text, .devinit.rodata, ... */
  751              section_address = 0;
  752 
  753           (void) send_a_relocation (module_name, section_name, section_address);
  754           /* PR14005: take a pill, dude, a failure with an overlong
  755            * name does not call for freaking out.  Nor does an error
  756            * coming back from the write(2) into the module.  We will
  757            * just stagger along without that particular module/section
  758            * being present in the _stp_sections[] tables. */
  759         }
  760 
  761       if (strcmp (section_name, ".gnu.linkonce.this_module"))
  762         fclose (secfile);
  763       else
  764         {
  765           (void)set_clexec (fileno (secfile));
  766           /* NB: don't fclose this arbitrarily-chosen section file.
  767              This forces the kernel to keep a nonzero reference count
  768              on the subject module, until staprun exits, by which time
  769              the kernel module will have inserted its separate claws
  770              into the probeworthy modules.  This prevents a race
  771              condition where a probe may be just starting up at the
  772              same time that a probeworthy module is being unloaded. */
  773         }
  774     }
  775 
  776   globfree (& globbuf);
  777   return 0;
  778 }
  779 
  780 
  781 
  782 int send_relocations ()
  783 {
  784   int rc;
  785 
  786   tweak_kptr_restrict();
  787   
  788   rc = send_relocation_kernel ();
  789   if (rc == 0)
  790     rc = send_relocation_modules ();
  791   return rc;
  792 }
  793 
  794 
  795 int send_tzinfo ()
  796 {
  797   struct _stp_msg_tzinfo tzi;
  798   time_t now_t;
  799   struct tm* now;
  800   int rc;
  801 
  802   /* NB: This is not good enough; it sends DST-unaware numbers. */
  803 #if 0
  804   tzset ();
  805   tzi.tz_gmtoff = timezone;
  806   strncpy (tzi.tz_name, tzname[0], STP_TZ_NAME_LEN - 1);
  807 #endif
  808 
  809   time (& now_t);
  810   now = localtime (& now_t);
  811   tzi.tz_gmtoff = - now->tm_gmtoff;
  812   strncpy (tzi.tz_name, now->tm_zone, STP_TZ_NAME_LEN - 1);
  813 
  814   rc = send_request(STP_TZINFO, & tzi, sizeof(tzi));
  815   if (rc != 0)
  816     perror ("Unable to send time zone information");
  817   return rc;
  818 }
  819 
  820 int send_privilege_credentials (privilege_t user_credentials)
  821 {
  822   struct _stp_msg_privilege_credentials pc;
  823   int rc;
  824   pc.pc_group_mask = user_credentials;
  825   rc = send_request(STP_PRIVILEGE_CREDENTIALS, & pc, sizeof(pc));
  826   if (rc != 0) {
  827     /* Not an error. Happens when pre 1.7 modules are loaded.  */
  828     dbug (1, "Unable to send user privilege credentials\n");
  829   }
  830   return rc;
  831 }
  832 
  833 int send_remote_id ()
  834 {
  835   struct _stp_msg_remote_id rem;
  836   int rc;
  837 
  838   rem.remote_id = remote_id;
  839   strncpy (rem.remote_uri, remote_uri, STP_REMOTE_URI_LEN - 1);
  840   rem.remote_uri [STP_REMOTE_URI_LEN-1]='\0'; /* XXX: quietly truncate */
  841   rc = send_request(STP_REMOTE_ID, & rem, sizeof(rem));
  842   if (rc != 0)
  843     perror ("Unable to send remote id");
  844   return rc;
  845 }