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
|
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License
//--------------------------------------------------------------------
//
// General purpose helpers
//
//--------------------------------------------------------------------
#ifndef GENHELPERS_H
#define GENHELPERS_H
#include <linux/version.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <sys/utsname.h>
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#define MIN_KERNEL_VERSION 3
#define MIN_KERNEL_PATCH 5
//-------------------------------------------------------------------------------------
// Auto clean up of memory using free (void)
//-------------------------------------------------------------------------------------
static inline void cleanup_void(void* val)
{
void **ppVal = (void**)val;
free(*ppVal);
}
//-------------------------------------------------------------------------------------
// Auto clean up of file descriptors using close
//-------------------------------------------------------------------------------------
#if (__GNUC__ >= 13)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wanalyzer-fd-leak"
#endif
static inline void cleanup_fd(int* val)
{
if (*val)
{
close(*val);
}
else
{
// Make static analyzer happy, otherwise it thinks we leak when *val == 0
}
}
#if (__GNUC__ >= 13)
#pragma GCC diagnostic pop
#endif
//-------------------------------------------------------------------------------------
// Auto clean up of dir using closedir
//-------------------------------------------------------------------------------------
static inline void cleanup_dir(DIR** val)
{
if(*val)
{
closedir(*val);
}
}
//-------------------------------------------------------------------------------------
// Auto clean up of FILE using fclose
//-------------------------------------------------------------------------------------
static inline void cleanup_file(FILE** val)
{
if(*val)
{
fclose(*val);
}
}
//-------------------------------------------------------------------------------------
// Auto cancel pthread
//-------------------------------------------------------------------------------------
static inline void cancel_pthread(unsigned long* val)
{
if(*val!=-1)
{
pthread_cancel(*val);
}
}
#define auto_free __attribute__ ((__cleanup__(cleanup_void)))
#define auto_free_fd __attribute__ ((__cleanup__(cleanup_fd)))
#define auto_free_dir __attribute__ ((__cleanup__(cleanup_dir)))
#define auto_free_file __attribute__ ((__cleanup__(cleanup_file)))
#define auto_cancel_thread __attribute__ ((__cleanup__(cancel_pthread)))
int* GetSeparatedValues(char* src, char* separator, int* numValues);
bool ConvertToInt(const char* src, int* conv);
bool IsValidNumberArg(const char *arg);
bool CheckKernelVersion();
uint16_t* GetUint16(char* buffer);
char* GetPath(char* lineBuf);
FILE *popen2(const char *command, const char *type, pid_t *pid);
char *sanitize(char *processName);
int StringToGuid(char* szGuid, struct CLSID* pGuid);
int GetHex(char* szStr, int size, void* pResult);
bool createDir(const char *dir, mode_t perms);
char* GetSocketPath(char* prefix, pid_t pid, pid_t targetPid);
int send_all(int socket, void *buffer, size_t length);
int recv_all(int socket, void* buffer, size_t length);
pid_t gettid();
#endif // GENHELPERS_H
|