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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <pthread.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#include "mt-udev-wrap.h"
#include "util.h"
#include "debug.h"
#include "checkers.h"
#include "vector.h"
#include "structs.h"
#include "config.h"
#include "log.h"
size_t
strchop(char *str)
{
size_t i;
for (i = strlen(str); i != 0 && isspace(str[i - 1]); i--) ;
str[i] = '\0';
return i;
}
/*
* glibc's non-destructive version of basename()
* License: LGPL-2.1-or-later
*/
const char *libmp_basename(const char *filename)
{
const char *p = strrchr(filename, '/');
return p ? p + 1 : filename;
}
int
basenamecpy (const char *src, char *dst, size_t size)
{
const char *p, *e;
if (!src || !dst || !strlen(src))
return 0;
p = basename(src);
for (e = p + strlen(p) - 1; e >= p && isspace(*e); --e) ;
if (e < p || (size_t)(e - p) > size - 2)
return 0;
strlcpy(dst, p, e - p + 2);
return strlen(dst);
}
int
filepresent (const char *run) {
struct stat buf;
if(!stat(run, &buf))
return 1;
return 0;
}
char *get_next_string(char **temp, const char *split_char)
{
char *token = NULL;
token = strsep(temp, split_char);
while (token != NULL && !strcmp(token, ""))
token = strsep(temp, split_char);
return token;
}
int
get_word (const char *sentence, char **word)
{
const char *p;
int len;
int skip = 0;
if (word)
*word = NULL;
while (*sentence == ' ') {
sentence++;
skip++;
}
if (*sentence == '\0')
return 0;
p = sentence;
while (*p != ' ' && *p != '\0')
p++;
len = (int) (p - sentence);
if (!word)
return skip + len;
*word = calloc(1, len + 1);
if (!*word) {
condlog(0, "get_word : oom");
return 0;
}
strncpy(*word, sentence, len);
strchop(*word);
condlog(5, "*word = %s, len = %i", *word, len);
if (*p == '\0')
return 0;
return skip + len;
}
size_t libmp_strlcpy(char * restrict dst, const char * restrict src, size_t size)
{
size_t bytes = 0;
char ch;
while ((ch = *src++)) {
if (bytes + 1 < size)
*dst++ = ch;
bytes++;
}
/* If size == 0 there is no space for a final null... */
if (size)
*dst = '\0';
return bytes;
}
size_t libmp_strlcat(char * restrict dst, const char * restrict src, size_t size)
{
size_t bytes = 0;
char ch;
while (bytes < size && *dst) {
dst++;
bytes++;
}
if (bytes == size)
return (bytes + strlen(src));
while ((ch = *src++)) {
if (bytes + 1 < size)
*dst++ = ch;
bytes++;
}
*dst = '\0';
return bytes;
}
/* This function returns a pointer inside of the supplied pathname string.
* If is_path_device is true, it may also modify the supplied string */
char *convert_dev(char *name, int is_path_device)
{
char *ptr;
if (!name)
return NULL;
if (is_path_device) {
ptr = strstr(name, "cciss/");
if (ptr) {
ptr += 5;
*ptr = '!';
}
}
if (!strncmp(name, "/dev/", 5) && strlen(name) > 5)
ptr = name + 5;
else
ptr = name;
return ptr;
}
dev_t parse_devt(const char *dev_t)
{
int maj, min;
if (sscanf(dev_t,"%d:%d", &maj, &min) != 2)
return 0;
return makedev(maj, min);
}
void
setup_thread_attr(pthread_attr_t *attr, size_t stacksize, int detached)
{
int ret;
ret = pthread_attr_init(attr);
assert(ret == 0);
if (PTHREAD_STACK_MIN > 0 && stacksize < (size_t)PTHREAD_STACK_MIN)
stacksize = (size_t)PTHREAD_STACK_MIN;
ret = pthread_attr_setstacksize(attr, stacksize);
assert(ret == 0);
if (detached) {
ret = pthread_attr_setdetachstate(attr,
PTHREAD_CREATE_DETACHED);
assert(ret == 0);
}
}
static int _linux_version_code;
static pthread_once_t _lvc_initialized = PTHREAD_ONCE_INIT;
/* Returns current kernel version encoded as major*65536 + minor*256 + patch,
* so, for example, to check if the kernel is greater than 2.2.11:
*
* if (get_linux_version_code() > KERNEL_VERSION(2,2,11)) { <stuff> }
*
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
* Code copied from busybox (GPLv2 or later)
*/
static void
_set_linux_version_code(void)
{
struct utsname name;
char *t;
int i, r;
uname(&name); /* never fails */
t = name.release;
r = 0;
for (i = 0; i < 3; i++) {
t = strtok(t, ".");
r = r * 256 + (t ? atoi(t) : 0);
t = NULL;
}
_linux_version_code = r;
}
int get_linux_version_code(void)
{
pthread_once(&_lvc_initialized, _set_linux_version_code);
return _linux_version_code;
}
int safe_write(int fd, const void *buf, size_t count)
{
while (count > 0) {
ssize_t r = write(fd, buf, count);
if (r < 0) {
if (errno == EINTR)
continue;
return -errno;
}
count -= r;
buf = (const char *)buf + r;
}
return 0;
}
void set_max_fds(rlim_t max_fds)
{
struct rlimit fd_limit;
if (!max_fds)
return;
if (getrlimit(RLIMIT_NOFILE, &fd_limit) < 0) {
condlog(0, "can't get open fds limit: %s",
strerror(errno));
fd_limit.rlim_cur = 0;
fd_limit.rlim_max = 0;
}
if (fd_limit.rlim_cur < max_fds) {
fd_limit.rlim_cur = max_fds;
if (fd_limit.rlim_max < max_fds)
fd_limit.rlim_max = max_fds;
if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0) {
condlog(0, "can't set open fds limit to "
"%lu/%lu : %s",
(unsigned long)fd_limit.rlim_cur,
(unsigned long)fd_limit.rlim_max,
strerror(errno));
} else {
condlog(3, "set open fds limit to %lu/%lu",
(unsigned long)fd_limit.rlim_cur,
(unsigned long)fd_limit.rlim_max);
}
}
}
void free_scandir_result(struct scandir_result *res)
{
int i;
for (i = 0; i < res->n; i++)
free(res->di[i]);
free(res->di);
}
void cleanup_free_ptr(void *arg)
{
void **p = arg;
if (p && *p) {
free(*p);
*p = NULL;
}
}
void cleanup_fd_ptr(void *arg)
{
int *fd = arg;
if (*fd >= 0) {
close(*fd);
*fd = -1;
}
}
void cleanup_mutex(void *arg)
{
pthread_mutex_unlock(arg);
}
void cleanup_vector_free(void *arg)
{
if (arg)
vector_free((vector)arg);
}
void cleanup_fclose(void *p)
{
if (p)
fclose(p);
}
void cleanup_bitfield(union bitfield **p)
{
free(*p);
}
union bitfield *alloc_bitfield(unsigned int maxbit)
{
unsigned int n;
union bitfield *bf;
if (maxbit == 0) {
errno = EINVAL;
return NULL;
}
n = (maxbit - 1) / bits_per_slot + 1;
bf = calloc(1, sizeof(union bitfield) + (n - 1) * sizeof(bitfield_t));
if (bf)
bf->len = maxbit;
return bf;
}
void log_bitfield_overflow__(const char *f, unsigned int bit, unsigned int len)
{
condlog(0, "%s: bitfield overflow: %u >= %u", f, bit, len);
}
int should_exit(void)
{
return 0;
}
void cleanup_charp(char **p)
{
free(*p);
}
void cleanup_ucharp(unsigned char **p)
{
free(*p);
}
void cleanup_udev_device(struct udev_device **udd)
{
if (*udd)
udev_device_unref(*udd);
}
|