summaryrefslogtreecommitdiff
path: root/ioreplay/src/utils/utils.c
blob: 57d67370668f60fef75115cc0bfcaa9cf52a0dae (plain)
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
// Copyright 2018 Mimecast Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "utils.h"

#include <sys/resource.h>
#include <sys/time.h>

void* notnull(void *p, char *file, int line, int count)
{
    if (p == NULL) {
        Errno("%s:%d count:%d Could not allocate memory", file, line, count);
    }
    return p;
}


FILE* fnotnull(FILE *fd, const char *path, char *file, int line)
{
    if (fd == NULL) {
        Errno("%s:%d Could not open file '%s'", file, line, path);
    }
    return fd;
}

void* mmapok(void *p, char *file, int line)
{
    if (p == MAP_FAILED) {
        Errno("%s:%d: Mmap failed", file, line);
    }
    return p;
}

char* strtok2_r(char *str, char *delim, char **saveptr)
{
    int len = strlen(delim);

    if (str == NULL)
        str = *saveptr;

    char *next = strstr(str, delim);
    if (next) {
        next[0] = '\0';
        for (int i = 0; i < len; ++i)
            next++;
        *saveptr = next;
        return str;
    }

    return NULL;
}

void chreplace(char *str, char replace, char with)
{
    for (int i = 0; ; ++i) {
        if (str[i] == '\0')
            break;
        if (str[i] == replace)
            str[i] = with;
    }
}

void strunquote(char *str)
{
    int len = strlen(str);

    if (str[0] == '"') {
        if (str[len-1] == '"')
            str[len-1] = '\0';
        for (int i = 1; i < len; ++i)
            str[i-1] = str[i];
    }
}

void drop_root(const char *user)
{
    if (getuid() == 0) {
        Put("Dropping root privileges to user %s", user);

        struct passwd *pw = getpwnam(user);

        /* process is running as root, drop privileges */
        if (setgid(pw->pw_gid) != 0) {
            Errno("setgid: Unable to drop group privileges!");
        }
        if (setuid(pw->pw_uid) != 0) {
            Errno("setuid: Unable to drop user privileges!");
        }
    }
}

void get_loadavg_s(char *readbuf)
{
    FILE *fp = Fopen("/proc/loadavg", "r");
    fgets(readbuf, 128, fp);
    char *pos = strchr(readbuf, ' ');
    pos[0] = '\0';
    fclose(fp);
}

double get_loadavg()
{
    // Not thread safe, but multi processing safe
    static char buf[128];
    get_loadavg_s(buf);

    return atof(buf);
}

bool is_number(char *str)
{
    for (int i = 0; ; ++i) {
        if (str[i] == '\0')
            return true;
        if (isdigit(str[i]) == 0 && str[i] != '-')
            return false;
    }

    return true;
}

void start_pthread(pthread_t *thread, void*(*cb)(void*), void *data)
{
    int rc = pthread_create(thread, NULL, cb, data);

    switch (rc) {
    case 0:
        break;
    case EAGAIN:
        Error("Out of resources while creating pthread (%d)", rc);
        break;
    case EINVAL:
        Error("Ivalid settings while creating pthread (%d)", rc);
        break;
    case EPERM:
        Error("No permissions to configure pthread (%d)", rc);
    default:
        Error("Unknown error while creating pthread (%d)", rc);
        break;
    }
}