eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

backtrace.c (3966B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2013-2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library 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 GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #define _GNU_SOURCE
     28 #include <features.h>
     29 
     30 #ifdef HAVE_BACKTRACE
     31 #include <stdlib.h>
     32 #include <string.h>
     33 #include <sys/types.h>
     34 #include <signal.h>
     35 #include <unistd.h>
     36 #include <stdio.h>
     37 #include <execinfo.h>
     38 #include <dlfcn.h>
     39 #include <linux/limits.h>
     40 
     41 #include <ell/ell.h>
     42 
     43 #include "src/backtrace.h"
     44 
     45 static const char *program_exec;
     46 static const char *program_path;
     47 
     48 void __iwd_backtrace_print(unsigned int offset)
     49 {
     50 	void *frames[99];
     51 	size_t n_ptrs;
     52 	unsigned int i;
     53 	int outfd[2], infd[2];
     54 	int pathlen;
     55 	pid_t pid;
     56 
     57 	if (!program_exec)
     58 		return;
     59 
     60 	pathlen = strlen(program_path);
     61 
     62 	n_ptrs = backtrace(frames, L_ARRAY_SIZE(frames));
     63 	if (n_ptrs < offset)
     64 		return;
     65 
     66 	if (pipe(outfd) < 0)
     67 		return;
     68 
     69 	if (pipe(infd) < 0) {
     70 		close(outfd[0]);
     71 		close(outfd[1]);
     72 		return;
     73 	}
     74 
     75 	pid = fork();
     76 	if (pid < 0) {
     77 		close(outfd[0]);
     78 		close(outfd[1]);
     79 		close(infd[0]);
     80 		close(infd[1]);
     81 		return;
     82 	}
     83 
     84 	if (pid == 0) {
     85 		close(outfd[1]);
     86 		close(infd[0]);
     87 
     88 		dup2(outfd[0], STDIN_FILENO);
     89 		dup2(infd[1], STDOUT_FILENO);
     90 
     91 		execlp("addr2line", "-C", "-f", "-e", program_exec, NULL);
     92 
     93 		exit(EXIT_FAILURE);
     94 	}
     95 
     96 	close(outfd[0]);
     97 	close(infd[1]);
     98 
     99 	l_error("++++++++ backtrace ++++++++");
    100 
    101 	for (i = offset; i < n_ptrs - 1; i++) {
    102 		Dl_info info;
    103 		char addr[20], buf[PATH_MAX * 2];
    104 		int len, written;
    105 		char *ptr, *pos;
    106 
    107 		dladdr(frames[i], &info);
    108 
    109 		len = snprintf(addr, sizeof(addr), "%p\n", frames[i]);
    110 		if (len < 0)
    111 			break;
    112 
    113 		written = write(outfd[1], addr, len);
    114 		if (written < 0)
    115 			break;
    116 
    117 		len = read(infd[0], buf, sizeof(buf) - 1);
    118 		if (len < 0)
    119 			break;
    120 
    121 		buf[len] = '\0';
    122 
    123 		pos = strchr(buf, '\n');
    124 		if (!pos)
    125 			continue;
    126 
    127 		*pos++ = '\0';
    128 
    129 		if (strcmp(buf, "??") == 0) {
    130 			l_error("#%-2u %p in %s", i - offset,
    131 						frames[i], info.dli_fname);
    132 			continue;
    133 		}
    134 
    135 		ptr = strchr(pos, '\n');
    136 		if (!ptr)
    137 			continue;
    138 
    139 		*ptr++ = '\0';
    140 
    141 		if (strncmp(pos, program_path, pathlen) == 0)
    142 			pos += pathlen + 1;
    143 
    144 		l_error("#%-2u %p in %s() at %s", i - offset,
    145 						frames[i], buf, pos);
    146 	}
    147 
    148 	l_error("+++++++++++++++++++++++++++");
    149 
    150 	kill(pid, SIGTERM);
    151 
    152 	close(outfd[1]);
    153 	close(infd[0]);
    154 }
    155 
    156 static void signal_handler(int signo)
    157 {
    158 	l_error("Aborting (signal %d) [%s]", signo, program_exec);
    159 
    160 	__iwd_backtrace_print(2);
    161 
    162 	exit(EXIT_FAILURE);
    163 }
    164 
    165 void __iwd_backtrace_init(void)
    166 {
    167 	static char path[PATH_MAX];
    168 	static char cwd[PATH_MAX];
    169 	struct sigaction sa;
    170 	sigset_t mask;
    171 	ssize_t len;
    172 
    173 	/* Attempt to get the full path to our executable */
    174 	len = readlink("/proc/self/exe", path, sizeof(path) - 1);
    175 	if (len > 0) {
    176 		int i;
    177 
    178 		path[len] = '\0';
    179 
    180 		for (i = len - 1; i >= 0; i--) {
    181 			if (path[i] != '/')
    182 				continue;
    183 
    184 			program_exec = path;
    185 			break;
    186 		}
    187 	}
    188 
    189 	if (!program_exec)
    190 		return;
    191 
    192 	program_path = getcwd(cwd, sizeof(cwd));
    193 
    194 	sigemptyset(&mask);
    195 	sa.sa_handler = signal_handler;
    196 	sa.sa_mask = mask;
    197 	sa.sa_flags = 0;
    198 	sigaction(SIGBUS, &sa, NULL);
    199 	sigaction(SIGILL, &sa, NULL);
    200 	sigaction(SIGFPE, &sa, NULL);
    201 	sigaction(SIGSEGV, &sa, NULL);
    202 	sigaction(SIGABRT, &sa, NULL);
    203 	sigaction(SIGPIPE, &sa, NULL);
    204 }
    205 #endif