00001
00002
00003 #include <stdlib.h>
00004 #include <dlfcn.h>
00005 #include <stdio.h>
00006 #include <string.h>
00007 #include "drms_dsdsapi.h"
00008
00009 const char *DSDS_PortNS[] =
00010 {
00011 "dsds",
00012 "ds_mdi",
00013 NULL
00014 };
00015
00016 void *DSDS_GetFPtr(void *hDSDS, const char *symbol)
00017 {
00018 void *ret = NULL;
00019 char *msg = NULL;
00020
00021 if (hDSDS)
00022 {
00023 dlerror();
00024 ret = dlsym(hDSDS, symbol);
00025 if ((msg = dlerror()) != NULL)
00026 {
00027
00028 fprintf(stderr, "Symbol %s not found: %s.\n", symbol, msg);
00029 }
00030 }
00031
00032 return ret;
00033 }
00034
00035 int DSDS_IsDSDSSpec(const char *spec)
00036 {
00037 return (strstr(spec, "prog:") == spec);
00038 }
00039
00040 int DSDS_IsDSDSPort(const char *query)
00041 {
00042 int isport = 0;
00043 int ins = 0;
00044 const char *portns = NULL;
00045
00046 while ((portns = DSDS_PortNS[ins]) != NULL)
00047 {
00048 if (strstr(query, portns) == query)
00049 {
00050 isport = 1;
00051 break;
00052 }
00053
00054 ins++;
00055 }
00056
00057 return isport;
00058 }
00059
00060 int DSDS_GetDSDSParams(DRMS_SeriesInfo_t *si, char *out)
00061 {
00062 int err = 1;
00063 char buf[kDSDS_MaxHandle];
00064 snprintf(buf, sizeof(buf), "%s", si->description);
00065 char *lasts;
00066 char *ans;
00067
00068 if (si)
00069 {
00070 if ((ans = strtok_r(buf, "[]", &lasts)) != NULL)
00071 {
00072 snprintf(out, kDSDS_MaxHandle, "%s", ans);
00073 err = 0;
00074 }
00075 }
00076
00077 return err;
00078 }
00079
00080 int DSDS_SetDSDSParams(void *hDSDS, DRMS_SeriesInfo_t *si, DSDS_Handle_t in)
00081 {
00082 int err = 1;
00083
00084 if (hDSDS && si)
00085 {
00086 pDSDSFn_DSDS_handle_todesc_t pFn_DSDS_handle_todesc =
00087 (pDSDSFn_DSDS_handle_todesc_t)DSDS_GetFPtr(hDSDS, kDSDS_DSDS_HANDLE_TODESC);
00088
00089 if (pFn_DSDS_handle_todesc)
00090 {
00091 kDSDS_Stat_t dsdsStat;
00092 (*pFn_DSDS_handle_todesc)(in, si->description, &dsdsStat);
00093 err = (dsdsStat != kDSDS_Stat_Success);
00094 }
00095 }
00096
00097 return err;
00098 }
00099
00100 void *DSDS_GetLibHandle(const char *libname, kDSDS_Stat_t *status)
00101 {
00102 kDSDS_Stat_t stat = kDSDS_Stat_Success;
00103 void *ret = NULL;
00104 char lpath[PATH_MAX];
00105 char spath[PATH_MAX];
00106 char *msg = NULL;
00107
00108 #ifdef DRMS_ARCH
00109 #else
00110 #error Ensure the DRMS_ARCH macro is defined in make_basic.mk
00111 #endif
00112
00113
00114 if (readlink("/proc/self/exe", spath, sizeof(spath)) == -1)
00115 {
00116 fprintf(stderr, "Cannot locate this binary.\n");
00117 stat = kDSDS_Stat_CantOpenLibrary;
00118 }
00119 else
00120 {
00121
00122 char *needle = NULL;
00123
00124 if ((needle = strstr(spath, DRMS_ARCH)) != NULL)
00125 {
00126 needle += strlen(DRMS_ARCH);
00127 *needle = '\0';
00128
00129
00130
00131 snprintf(lpath,
00132 sizeof(lpath),
00133 "%s/../lib/%s/%s",
00134 spath,
00135 DRMS_ARCH,
00136 libname);
00137 }
00138 else
00139 {
00140 fprintf(stderr, "Cannot find architecture %s subpath.\n", DRMS_ARCH);
00141 stat = kDSDS_Stat_CantOpenLibrary;
00142 }
00143 }
00144
00145 if (!stat)
00146 {
00147 dlerror();
00148 ret = dlopen(lpath, RTLD_NOW);
00149 if ((msg = dlerror()) != NULL)
00150 {
00151
00152 fprintf(stderr, "dlopen(%s) error: %s.\n", lpath, msg);
00153 if (ret)
00154 {
00155 dlclose(ret);
00156 ret = NULL;
00157 }
00158 stat = kDSDS_Stat_CantOpenLibrary;
00159 }
00160 }
00161
00162 if (status)
00163 {
00164 *status = stat;
00165 }
00166
00167 return ret;
00168 }