Skip to content
This repository has been archived by the owner on Oct 26, 2021. It is now read-only.

Commit

Permalink
Update after static code analysis; Added test for crashed app handlin…
Browse files Browse the repository at this point in the history
…g; removed obsolete test data
  • Loading branch information
Ingo Huerner committed Jun 1, 2017
1 parent dd667d4 commit 7d397ae
Show file tree
Hide file tree
Showing 7 changed files with 730 additions and 217 deletions.
13 changes: 13 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,19 @@ fi



dnl **********************************************
dnl *** location of pidfile, default is /var/run *
dnl **********************************************
AC_ARG_WITH([pidfilelocation],
[AS_HELP_STRING([--with-pidfilelocation=path of pidfile],[Path to the pifile])],
[with_pidfilelocation=$withval],[with_pidfilelocation="/var/run"])

AC_SUBST([pidfilelocationpath], [$with_pidfilelocation])
AC_MSG_NOTICE([PID File Path: $pidfilelocationpath])
AC_DEFINE_UNQUOTED(PIDFILEDIR, "$pidfilelocationpath", "location for pidfile")



######################################################################
### max numberr of database slots, default is 100.000
######################################################################
Expand Down
107 changes: 107 additions & 0 deletions src/key-value-store/database/kissdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#define _FILE_OFFSET_BITS 64
#define KISSDB_HEADER_SIZE sizeof(Header_s)

#define FILE_DIR_NOT_SELF_OR_PARENT(s) ((s)[0]!='.'&&(((s)[1]!='.'||(s)[2]!='\0')||(s)[1]=='\0'))


#include "./kissdb.h"
#include "../crc32.h"
#include <string.h>
Expand All @@ -35,6 +38,7 @@
#include <sys/time.h>
#include <semaphore.h>
#include <dlt.h>
#include <dirent.h>
#include "persComErrors.h"

//
Expand Down Expand Up @@ -541,6 +545,17 @@ int KISSDB_open(KISSDB* db, const char* path, int openMode, int writeMode, uint1
}
}
}
else
{
// we are not the shm creator, now check if someone else has the file open
// if num of open fd's and ref count does not match, report an error and don't increment ref counter
//printf("#### Database closed N O T O K - %d!!!!!\n\n", db->shared->refCount);
if(searchOpenFDs(path) != db->shared->refCount)
{
Kdb_unlock(&db->shared->rwlock);
return KISSDB_ERROR_APPCRASH;
}
}
Kdb_unlock(&db->shared->rwlock);
return 0;
}
Expand Down Expand Up @@ -1511,6 +1526,98 @@ int checkErrorFlags(KISSDB* db)
}



int searchProcFileSys(pid_t pid, const char* path)
{
int rval = 0;
DIR* procdir;
struct dirent* procDirEnt;
char procPidDir[128] = {0};

memset(procPidDir, 0, 128);
snprintf(procPidDir, 128, "/proc/%d/fd", pid);

if((procdir = opendir(procPidDir)) != NULL) // search for open fd's
{
for(procDirEnt = readdir(procdir); NULL != procDirEnt; procDirEnt = readdir(procdir))
{
if(FILE_DIR_NOT_SELF_OR_PARENT(procDirEnt->d_name))
{
char buffer[128] = {0};
char fullPath[256] = {0};
memset(buffer, 0, 128);
memset(fullPath, 0, 256);

snprintf(fullPath, 256, "%s/%s", procPidDir, procDirEnt->d_name);
if(readlink(fullPath, buffer, 128) != -1)
{
if(strcmp(path, buffer) == 0)
{
//printf(" FOUND [%d]: %s | %s | %s\n", pid, path, fullPath, buffer);
rval = 1;
break;
}
}
else
{
printf("Error readlink: %s\n", strerror(errno));
}
}
}
closedir(procdir);
}

return rval;
}


int searchOpenFDs(const char* path)
{
DIR* dir;
struct dirent* ent;
int rval = 0;
int fd = -1;

if ((dir = opendir(PIDFILEDIR)) != NULL) // search for pidfiles
{
for(ent = readdir(dir); NULL != ent; ent = readdir(dir))
{
if(FILE_DIR_NOT_SELF_OR_PARENT(ent->d_name))
{
if(strstr(ent->d_name, PIDFILE_PREFIX) != NULL)
{
// extract the PID form filename
char pidArray[10] = {0};
char* pidArrayPtr = pidArray;
pid_t pid = 0;
int start = strlen(PIDFILE_PREFIX);
memset(pidArray, 0, 10);

while(ent->d_name[start] != '.')
{
*(pidArrayPtr)++ = ent->d_name[start++];
}
pid = atoi(pidArray);
if(getpid() != pid) // check only other pids not own
{
if(searchProcFileSys(pid, path) == 1)
{
rval++; // count number of open fd's
}
}
}
}
}
closedir(dir);
}
else
{
printf("Failed to open directory %s: %s\n", PIDFILEDIR, strerror(errno));
}

return rval;
}

int verifyHashtableCS(KISSDB* db)
{
char* ptr;
Expand Down
9 changes: 9 additions & 0 deletions src/key-value-store/database/kissdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ extern "C" {
#endif


#define PIDFILE_PREFIX "perslib_"
#define PIDFILE_TEMPLATE PIDFILEDIR "/" PIDFILE_PREFIX"%d.pid" // PIDFILEDIR is defined via configure switch -pidfiledir (default is /var/run if not set)

/**
* Version: 2.3
*
Expand Down Expand Up @@ -247,6 +250,12 @@ typedef struct {
* buffer where data should be returned is too small
*/
#define KISSDB_ERROR_WRONG_BUFSIZE -13


/**
* don't increment ref counter, possible application detected
*/
#define KISSDB_ERROR_APPCRASH -14


/**
Expand Down
Loading

0 comments on commit 7d397ae

Please sign in to comment.