| 1 |
/* -*- linux-c -*- |
| 2 |
* |
| 3 |
* ctl.c - staprun control channel |
| 4 |
* |
| 5 |
* This file is part of systemtap, and is free software. You can |
| 6 |
* redistribute it and/or modify it under the terms of the GNU General |
| 7 |
* Public License (GPL); either version 2, or (at your option) any |
| 8 |
* later version. |
| 9 |
* |
| 10 |
* Copyright (C) 2012-2018 Red Hat Inc. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "staprun.h" |
| 14 |
|
| 15 |
#define CTL_CHANNEL_NAME ".cmd" |
| 16 |
|
| 17 |
|
| 18 |
#ifndef HAVE_OPENAT |
| 19 |
#error "need openat" |
| 20 |
#endif |
| 21 |
|
| 22 |
|
| 23 |
// This function does multiple things: |
| 24 |
// |
| 25 |
// 1) if needed, open the running module's directory (the one that |
| 26 |
// contains .ctl), stash fd in relay_basedir_fd; this will be |
| 27 |
// passed to stapio children via -F$fd for privilege passing |
| 28 |
// |
| 29 |
// 2) (re)open the running module's .ctl file, stash fd in the |
| 30 |
// control_channel global; this will be used all over the place. |
| 31 |
// |
| 32 |
// Return 0 on success. |
| 33 |
// |
| 34 |
// See also PR14245, PR26665, RHBZ1902696 = PR23512 |
| 35 |
// |
| 36 |
int init_ctl_channel(const char *name, int verb) |
| 37 |
{ |
| 38 |
(void) verb; |
| 39 |
|
| 40 |
// Already got them both? |
| 41 |
if (control_channel >= 0 && relay_basedir_fd >= 0) |
| 42 |
return 0; |
| 43 |
|
| 44 |
// Need relay_basedir_fd .... ok try /sys/kernel/debug/systemtap/ |
| 45 |
if (relay_basedir_fd < 0) { |
| 46 |
char buf[PATH_MAX] = ""; |
| 47 |
struct statfs st; |
| 48 |
|
| 49 |
if (sprintf_chk(buf, "/sys/kernel/debug/systemtap/%s", name)) |
| 50 |
return -EINVAL; |
| 51 |
|
| 52 |
if (statfs("/sys/kernel/debug", &st) == 0 && (int)st.f_type == (int)DEBUGFS_MAGIC) |
| 53 |
relay_basedir_fd = open (buf, O_DIRECTORY | O_RDONLY); |
| 54 |
} |
| 55 |
|
| 56 |
// Still need relay_basedir_fd ... ok try /proc/systemtap/ |
| 57 |
if (relay_basedir_fd < 0) { |
| 58 |
char buf[PATH_MAX] = ""; |
| 59 |
|
| 60 |
if (sprintf_chk(buf, "/proc/systemtap/%s", name)) |
| 61 |
return -EINVAL; |
| 62 |
|
| 63 |
relay_basedir_fd = open (buf, O_DIRECTORY | O_RDONLY); |
| 64 |
} |
| 65 |
|
| 66 |
// Got relay_basedir_fd, need .ctl |
| 67 |
if (relay_basedir_fd >= 0) { |
| 68 |
// verify that the ctl file is accessible to our real uid/gid |
| 69 |
if (faccessat(relay_basedir_fd, CTL_CHANNEL_NAME, R_OK|W_OK, 0) != 0) |
| 70 |
return -EPERM; |
| 71 |
|
| 72 |
control_channel = openat_cloexec(relay_basedir_fd, |
| 73 |
CTL_CHANNEL_NAME, O_RDWR, 0); |
| 74 |
} |
| 75 |
|
| 76 |
// Fell through |
| 77 |
if (relay_basedir_fd < 0 || control_channel < 0) { |
| 78 |
err(_("Cannot attach to module %s control channel; not running?\n"), |
| 79 |
name); |
| 80 |
return -EINVAL; |
| 81 |
} |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
void close_ctl_channel(void) |
| 86 |
{ |
| 87 |
if (control_channel >= 0) { |
| 88 |
dbug(2, "Closed ctl fd %d\n", control_channel); |
| 89 |
close(control_channel); |
| 90 |
control_channel = -1; |
| 91 |
} |
| 92 |
} |