summaryrefslogtreecommitdiff
path: root/src/CommonAPI/utils.h
blob: 7717103d06ed95694bbd5eded4d5b2298ea3decf (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
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
/* Copyright (C) 2013 BMW Group
 * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
 * Author: Juergen Gehring (juergen.gehring@bmw.de)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef COMMONAPI_UTILS_H_
#define COMMONAPI_UTILS_H_

#include <unistd.h>
#include <dirent.h>
#include <dlfcn.h>
#include <sys/stat.h>

#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iostream>


namespace CommonAPI {


#if  __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
#  define COMMONAPI_DEPRECATED __attribute__ ((__deprecated__))
#elif defined(_MSC_VER) && (_MSC_VER >= 1300)
#  define COMMONAPI_DEPRECATED __declspec(deprecated)
#else
#  define COMMONAPI_DEPRECATED
#endif


/**
 * \brief Returns the fully qualified name of the binary.
 *
 * @return The name of the currently executing binary.
 */
inline std::string getCurrentBinaryFileFQN() {
    char fqnOfBinary[FILENAME_MAX];
    char pathToProcessImage[FILENAME_MAX];

    sprintf(pathToProcessImage, "/proc/%d/exe", getpid());
    const ssize_t lengthOfFqn = readlink(pathToProcessImage, fqnOfBinary, sizeof(fqnOfBinary) - 1);

    if (lengthOfFqn != -1) {
        fqnOfBinary[lengthOfFqn] = '\0';
        return std::string(std::move(fqnOfBinary));
    } else {
        return std::string("");
    }
}

/**
 * \brief Splits a std::string according to the given delim-char.
 *
 * The string will be splitted at each position the delim char is encountered. The delim itself
 * will be removed from the result.
 *
 * @param s: The string that is to be splitted
 * @param delim: The character that separates the resulting string tokens in the original string
 * @param elems: Reference to the vector that shall be filled with the splitted string elements.
 *
 * @return A reference to the vector you passed in (elems)
 */
inline std::vector<std::string>& split(const std::string& s, char delim, std::vector<std::string>& elems) {
    std::istringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}

/**
 * \brief Splits a std::string according to the given delim-char.
 *
 * The string will be splitted at each position the delim char is encountered. The delim itself
 * will be removed from the result.
 *
 * @param s: The string that is to be splitted
 * @param delim: The character that separates the resulting string tokens in the original string
 *
 * @return A vector containing the splitted string elements.
 */
inline std::vector<std::string> split(const std::string& s, char delim) {
    std::vector<std::string> elems;
    return split(s, delim, elems);
}

/**
 * \brief Trims whitespaces from beginning and end of a std::string.
 *
 * @param toTrim: The string that is to be trimmed.
 */
inline void trim(std::string& toTrim) {
    toTrim.erase(
                    toTrim.begin(),
                    std::find_if(toTrim.begin(),
                                 toTrim.end(),
                                 std::not1(std::ptr_fun<int, int>(std::isspace)))
    );
    toTrim.erase(
                    std::find_if(toTrim.rbegin(),
                                 toTrim.rend(),
                                 std::not1(std::ptr_fun<int, int>(std::isspace))).base(),
                    toTrim.end()
    );
}

/**
 * \brief Checks whether the given string contains nothing but digits.
 *
 * @param toCheck: The string that is to be checked on the presence of anything but digits.
 *
 * @return true if toCheck contains nothing but digits, false otherwise.
 */
inline bool containsOnlyDigits(const std::string& toCheck) {
    auto firstNonDigitIt = std::find_if(
                    toCheck.begin(),
                    toCheck.end(),
                    [](char c) {
                        return !std::isdigit(c);
                    });

    return firstNonDigitIt == toCheck.end();
}

/**
 * \brief Checks whether the given string contains nothing but alphanumeric characters.
 *
 * @param toCheck: The string that is to be checked on the presence of anything but alphanumeric characters.
 *
 * @return true if toCheck contains nothing but alphanumeric characters, false otherwise.
 */
inline bool containsOnlyAlphanumericCharacters(const std::string& toCheck) {
    auto firstNonAlphanumericCharacterIt = std::find_if(
                    toCheck.begin(),
                    toCheck.end(),
                    [](char c) {
                        return !std::isalnum(c);
                    });

    return firstNonAlphanumericCharacterIt == toCheck.end();
}

/**
 * \brief Checks whether the given std::string is a valid CommonAPI domain name.
 *
 * @param domainName: The std::string that is to be checked.
 *
 * @return true if domainName is a valid CommonAPI domainName, false otherwise.
 */
inline bool isValidDomainName(const std::string& domainName) {
    return containsOnlyAlphanumericCharacters(domainName);
}

/**
 * \brief Checks whether the given std::string is a valid CommonAPI service name.
 *
 * @param serviceName: The std::string that is to be checked.
 *
 * @return true if serviceName is a valid CommonAPI serviceName, false otherwise.
 */
inline bool isValidServiceName(const std::string& serviceName) {
    bool isValid = serviceName[0] != '.' && serviceName[serviceName.size() - 1] != '.';

    if (isValid) {
        std::vector<std::string> splittedServiceName = split(serviceName, '.');

        for (auto serviceNameElementIt = splittedServiceName.begin();
                        serviceNameElementIt != splittedServiceName.end() && isValid;
                        ++serviceNameElementIt) {
            isValid &= containsOnlyAlphanumericCharacters(*serviceNameElementIt);
        }
    }

    return isValid;
}

/**
 * \brief Checks whether the given std::string is a valid CommonAPI instance ID.
 *
 * @param instanceId: The std::string that is to be checked.
 *
 * @return true if instanceId is a valid CommonAPI instance ID, false otherwise.
 */
inline bool isValidInstanceId(const std::string& instanceId) {
    //Validation rules for ServiceName and InstanceID are equivalent
    return isValidServiceName(instanceId);
}

/**
 * \brief Checks whether the given std::string is a valid CommonAPI address.
 *
 * @param commonApiAddressName: The std::string that is to be checked.
 *
 * @return true if commonApiAddress is a valid CommonAPI address, false otherwise.
 */
inline bool isValidCommonApiAddress(const std::string& commonApiAddress) {
    std::vector<std::string> splittedAddress = split(commonApiAddress, ':');
    if (splittedAddress.size() != 3) {
        return false;
    }
    return isValidDomainName(splittedAddress[0]) && isValidServiceName(splittedAddress[1]) && isValidInstanceId(splittedAddress[2]);
}


/**
 * \brief Loads a specific generic library at runtime.
 *
 * The library will be loaded using dlopen(3) with the flags (RTLD_NOW | RTLD_GLOBAL), if all pre-checks are
 * successful. Pre-checks include the verification that the given parameters actually point to a library and
 * optionally if the library matches the standard name pattern for CommonAPI generic libraries. The standard
 * name pattern is "lib<wellKnownMiddlewareName>Gen-<arbitraryName>.so[.major[.minor.revision]]".
 *
 * @param wellKnownMiddlewareName: The name of the middleware that requests the loading of the library.
 * @param libraryName: The name of the library that shall be loaded.
 * @param path: The path at which the library is to be found. path + library name together make up the fully
 *              qualified name of the library.
 * @param checkStandardNamePattern: If set to true, it will be ensured the library matches the CommonAPI
 *                                  standard name pattern for generic libraries. This is meant as a safety measure
 *                                  to prevent the loading of unnecessary or the wrong libraries. Set to false if
 *                                  you are sure about what you are doing.
 * @return true if the library could be loaded successfully, false otherwise.
 *
 * @note The well known middleware name is included as a parameter because the additional libraries normally are needed
 *       only by specific middlewares. This name however will only be taken into consideration during name checking
 *       if the checkStandardNamePattern flag is set to true.
 */
inline bool loadGenericLibrary(const std::string& wellKnownMiddlewareName, const std::string& libraryName, const std::string& path, bool checkStandardNamePattern = true) {
    std::string fqnOfLibrary = path + libraryName;
    struct stat filestat;
    if (stat(fqnOfLibrary.c_str(), &filestat)) {
        return false;
    }
    if (S_ISDIR(filestat.st_mode)) {
        return false;
    }

    if (checkStandardNamePattern) {
        const std::string generatedLibPrefix = "lib" + wellKnownMiddlewareName + "Gen-";
        if (strncmp(generatedLibPrefix.c_str(), libraryName.c_str(), generatedLibPrefix.length()) != 0) {
            return false;
        }

        const char* fileNamePtr = libraryName.c_str();
        while ((fileNamePtr = strchr(fileNamePtr + 1, '.'))) {
            if (strncmp(".so\0", fileNamePtr, 4) == 0 || strncmp(".so.", fileNamePtr, 4) == 0) {
                break;
            }
        }

        if (!fileNamePtr) {
            return false;
        }
    }

    dlopen(fqnOfLibrary.c_str(), RTLD_NOW | RTLD_GLOBAL);
    return true;
}

/**
 * \brief Loads a specific generic library at runtime.
 *
 * The library will be loaded using dlopen(3) with the flags (RTLD_NOW | RTLD_GLOBAL), if all pre-checks are
 * successful. Pre-checks include the verification that the given parameters actually point to a library and
 * optionally if the library matches the standard name pattern for CommonAPI generic libraries. The standard
 * name pattern is "lib<wellKnownMiddlewareName>Gen-<arbitraryName>.so[.major[.minor.revision]]".
 *
 * @param wellKnownMiddlewareName: The name of the middleware that requests the loading of the library.
 * @param fqnOfLibrary: The fully qualified name of the library.
 * @param checkStandardNamePattern: If set to true, it will be ensured the library matches the CommonAPI
 *                                  standard name pattern for generic libraries. This is meant as a safety measure
 *                                  to prevent the loading of unnecessary or the wrong libraries. Set to false if
 *                                  you are sure about what you are doing.
 * @return true if the library could be loaded successfully, false otherwise.
 *
 * @note The well known middleware name is included as a parameter because the additional libraries normally are needed
 *       only by specific middlewares. This name however will only be taken into consideration during name checking
 *       if the checkStandardNamePattern flag is set to true.
 */
inline bool loadGenericLibrary(const std::string& wellKnownMiddlewareName,
                               const std::string& fqnOfLibrary,
                               bool checkStandardNamePattern = true) {
    uint32_t position = fqnOfLibrary.find_last_of("/\\");
    std::string path = fqnOfLibrary.substr(0, position + 1);
    std::string file = fqnOfLibrary.substr(position + 1);
    return loadGenericLibrary(wellKnownMiddlewareName, file, path, checkStandardNamePattern);
}


/**
 * \brief Searches the given path for additional generic CommonAPI libraries and loads them.
 *
 * All libraries for which the pre-checks are successful will be loaded using dlopen(3) with the flags
 * (RTLD_NOW | RTLD_GLOBAL). Pre-checks include the verification that the given parameters actually point
 * to a library and if the library matches the standard name pattern for CommonAPI generic libraries.
 * The standard name pattern is "lib<wellKnownMiddlewareName>Gen-<arbitraryName>.so[.major[.minor.revision]]".
 *
 * @param wellKnownMiddlewareName: The name of the middleware that requests the loading of the library. Will
 *                                 be used to perform the name check.
 * @param singleSearchPath: The directory that is to be searched for libraries.
 */
inline void findAndLoadGenericLibraries(const std::string& requestedMiddlewareName, const std::string& singleSearchPath) {
    DIR *directory = opendir(singleSearchPath.c_str());

    if (directory != NULL) {
        struct dirent* entry;

        while ((entry = readdir(directory))) {
            loadGenericLibrary(requestedMiddlewareName, entry->d_name, singleSearchPath, true);
        }
    }
}

/**
 * \brief Searches the given paths for additional generic CommonAPI libraries and loads them.
 *
 * All libraries for which the pre-checks are successful will be loaded using dlopen(3) with the flags
 * (RTLD_NOW | RTLD_GLOBAL). Pre-checks include the verification that the given parameters actually point
 * to a library and if the library matches the standard name pattern for CommonAPI generic libraries.
 * The standard name pattern is "lib<wellKnownMiddlewareName>Gen-<arbitraryName>.so[.major[.minor.revision]]".
 *
 * @param wellKnownMiddlewareName: The name of the middleware that requests the loading of the library. Will
 *                                 be used to perform the name check.
 * @param searchPaths: The directories that are to be searched for libraries.
 */
inline void findAndLoadGenericLibraries(const std::string& requestedMiddlewareName, const std::vector<std::string>& searchPaths) {
    for (const std::string& singleSearchPath : searchPaths) {
        findAndLoadGenericLibraries(requestedMiddlewareName, singleSearchPath.c_str());
    }
}


} //namespace CommonAPI


#endif /* COMMONAPI_UTILS_H_ */