File: utildl.c

package info (click to toggle)
ntop 3%3A5.0.1%2Bdfsg1-2.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 15,720 kB
  • ctags: 11,480
  • sloc: ansic: 79,804; sh: 21,658; python: 1,948; awk: 1,504; perl: 971; makefile: 745; php: 123; xml: 71; sql: 13; sed: 11
file content (143 lines) | stat: -rw-r--r-- 4,314 bytes parent folder | download | duplicates (2)
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
/**
 * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 *                          http://www.ntop.org
 *
 * Copyright (C) 2005 Burton Strauss <burton@ntopsupport.com>
 *
 * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/*

 A bit ugly...

  dlfcn is referenced in ntop.h, w/o the __USE_GNU (i.e. w/o the GNU
  extensions).  In FreeBSD it doesn't matter, but for Linux it does.
  I don't want to chase that bug-a-boo, but I do need the extension
  so dladdr is available.

  Hence this special routine w __USE_GNU and w/o ntop.h


  If you need anything from regular ntop, remember we don't have ntop.h,
  so you will have to copy the things you need from globals-defines.h and
  globals-core.h

 */

#ifndef WIN32

#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
 
#ifndef __USE_GNU
#define __USE_GNU
#endif

#include <dlfcn.h>

#include "config.h"

/* Forward */
int getDynamicLoadPaths(char *main, int mainLen, char *lib, int libLen, char *env, int envLen);

/* Copied declarations */

extern void welcome(FILE * fp);

#define safe_strncat(a, b, c) _safe_strncat(__FILE__, __LINE__, a, b, c)
extern int _safe_strncat(char* file, int line,
                         char* dest, size_t sizeofdest,
                         char* src);

//extern void traceEvent(int eventTraceLevel, char* file,
//                       int line, char * format, ...)
//     __attribute__ ((format (printf, 4, 5)));

/*
 * Used in traceEvent()
 */
//#define CONST_ALWAYSDISPLAY_TRACE_LEVEL     -1
//#define CONST_FATALERROR_TRACE_LEVEL        0
//#define CONST_ERROR_TRACE_LEVEL             1
//#define CONST_WARNING_TRACE_LEVEL           2
//#define CONST_INFO_TRACE_LEVEL              3
//#define CONST_NOISY_TRACE_LEVEL             4

//#define CONST_TRACE_ALWAYSDISPLAY           CONST_ALWAYSDISPLAY_TRACE_LEVEL, __FILE__, __LINE__
//#define CONST_TRACE_FATALERROR              CONST_FATALERROR_TRACE_LEVEL, __FILE__, __LINE__
//#define CONST_TRACE_ERROR                   CONST_ERROR_TRACE_LEVEL, __FILE__, __LINE__
//#define CONST_TRACE_WARNING                 CONST_WARNING_TRACE_LEVEL, __FILE__, __LINE__
//#define CONST_TRACE_INFO                    CONST_INFO_TRACE_LEVEL, __FILE__, __LINE__
//#define CONST_TRACE_NOISY                   CONST_NOISY_TRACE_LEVEL, __FILE__, __LINE__

/* ************************************************************************ */

int getDynamicLoadPaths(char *main, int mainLen, char *lib, int libLen, char *env, int envLen) {
  int rc = 0;

#ifdef HAVE_DLADDR
  char *lastslash, *_env;
  Dl_info info;

  memset(main, 0, mainLen);
  memset(lib, 0, libLen);
  memset(env, 0, envLen);
  memset(&info, 0, sizeof(info));

  rc = dladdr((void *)&welcome, &info);
  if(rc == 0)
    return(-2);

  strncpy(main, info.dli_fname, mainLen);
  lastslash = strrchr(main, '/');
  if(lastslash != NULL) lastslash[0] = '\0';

  rc = dladdr((void *)&getDynamicLoadPaths, &info);
  if(rc == 0)
    return(-3);

  strncpy(lib, info.dli_fname, libLen);
  lastslash = strrchr(lib, '/');
  if(lastslash != NULL) lastslash[0] = '\0';

#ifdef DARWIN
  _env = getenv("DYLD_LIBRARY_PATH");
  if((_env != NULL) && (_env[0] != '\0')) {
    strncpy(env, "DYLD_LIBRARY_PATH: ", envLen);
    safe_strncat(env, envLen, _env);
  }
#endif
  _env = getenv("LD_LIBRARY_PATH");
  if((_env != NULL) && (_env[0] != '\0')) {
    safe_strncat(env, envLen, "LD_LIBRARY_PATH ");
    safe_strncat(env, envLen, _env);
  }

  return(0);

#else

  return(-1); /* "failed" */

#endif /* HAVE_DLADDR */

}

#endif /* WIN32 */