File: helpers.c

package info (click to toggle)
libstatgrab 0.92.1-1.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,252 kB
  • sloc: ansic: 12,623; sh: 4,364; perl: 467; makefile: 282
file content (158 lines) | stat: -rw-r--r-- 3,441 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * libstatgrab
 * https://libstatgrab.org
 * Copyright (C) 2003-2004 Peter Saunders
 * Copyright (C) 2003-2019 Tim Bishop
 * Copyright (C) 2003-2013 Adam Sampson
 * Copyright (C) 2012-2019 Jens Rehsack
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include "helpers.h"
#include <tools.h>
#include <statgrab.h>
#include <poll.h>
#ifdef WITH_LIBLOG4CPLUS
#include <libgen.h>
#endif
#include <stdarg.h>

int *sig_flags[NSIG];

static void
sig_flagger(int signo) {
	if( sig_flags[signo] != NULL )
		++(*sig_flags[signo]);
}

int
register_sig_flagger(int signo, int *flag_ptr)
{
	if( ( sig_flags[signo] != NULL ) && (flag_ptr != NULL) )
		return EINVAL;

	if(flag_ptr) {
		signal( signo, NULL );
		sig_flags[signo] = NULL;
	}
	else {
		sig_flags[signo] = flag_ptr;
		signal( signo, sig_flagger );
	}

	return 0;
}

int
sg_warn(const char *prefix)
{
	sg_error_details err_det;
	char *errmsg = NULL;
	int rc;
	sg_error errc;

	if( SG_ERROR_NONE != ( errc = sg_get_error_details(&err_det) ) ) {
		fprintf(stderr, "can't get error details (%d, %s)\n", errc, sg_str_error(errc));
		return 0;
	}

	if( NULL == sg_strperror(&errmsg, &err_det) ) {
		errc = sg_get_error();
		fprintf(stderr, "panic: can't prepare error message (%d, %s)\n", errc, sg_str_error(errc));
		return 0;
	}

	rc = fprintf( stderr, "%s: %s\n", prefix, errmsg );

	free( errmsg );

	return rc;
}

void
sg_die(const char *prefix, int exit_code)
{
	sg_error_details err_det;
	char *errmsg = NULL;
	sg_error errc;

	if( SG_ERROR_NONE != ( errc = sg_get_error_details(&err_det) ) ) {
		fprintf(stderr, "panic: can't get error details (%d, %s)\n", errc, sg_str_error(errc));
		exit(exit_code);
	}

	if( NULL == sg_strperror(&errmsg, &err_det) ) {
		errc = sg_get_error();
		fprintf(stderr, "panic: can't prepare error message (%d, %s)\n", errc, sg_str_error(errc));
		exit(exit_code);
	}

	fprintf( stderr, "%s: %s\n", prefix, errmsg );

	free( errmsg );

	exit(exit_code);
}

static void
err_doit(int errnoflag, int error, const char *fmt, va_list ap)
{
	char buf[4096];
	int printed;
	printed = vsnprintf( buf, sizeof(buf), fmt, ap);
	if(errnoflag)
		snprintf(buf + printed, sizeof(buf) - printed, ": %s", strerror(error) );
	fflush(stderr);
	fputs(buf, stderr);
	fputs("\n", stderr);
	fflush(NULL);
}

void
die(int error, const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	err_doit(1, error, fmt, ap);
	va_end(ap);
	exit(1);
}

int
inp_wait(int delay) {
	struct pollfd pfd[1];

	pfd[0].fd = STDIN_FILENO;
	pfd[0].events = POLLRDNORM;

	if( poll( pfd, 1, (int)(delay * 1000) ) > 0 ) {
		for(;;) {
			int ch;
			int len = read(STDIN_FILENO, &ch, 1);
			if (len == -1 && errno == EINTR)
				continue;
			if (len < 1) {
				perror("read");
				exit(1);
			}
			return ch;
		}
	}

	return 0;
}