summaryrefslogtreecommitdiff
path: root/snmplib/file_utils.c
blob: 526143147f28a83b542535167347eae0af941560 (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
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-features.h>
#include <net-snmp/net-snmp-includes.h>

#include <stdio.h>
#include <ctype.h>
#if HAVE_STDLIB_H
#   include <stdlib.h>
#endif
#if HAVE_UNISTD_H
#   include <unistd.h>
#endif
#if HAVE_STRING_H
#   include <string.h>
#else
#  include <strings.h>
#endif

#include <sys/types.h>

#if HAVE_SYS_PARAM_H
#   include <sys/param.h>
#endif
#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#   include <fcntl.h>
#endif

#include <errno.h>

#if HAVE_DMALLOC_H
#  include <dmalloc.h>
#endif

#include <net-snmp/types.h>
#include <net-snmp/library/container.h>
#include <net-snmp/library/file_utils.h>

netsnmp_feature_child_of(file_utils_all, libnetsnmp)
netsnmp_feature_child_of(file_utils, file_utils_all)
netsnmp_feature_child_of(file_close, file_utils_all)

#ifndef NETSNMP_FEATURE_REMOVE_FILE_UTILS
/*------------------------------------------------------------------
 *
 * Prototypes
 *
 */




/*------------------------------------------------------------------
 *
 * Core Functions
 *
 */

/**
 * allocate a netsnmp_file structure
 *
 * This routine should be used instead of allocating on the stack,
 * for future compatability.
 */
netsnmp_file *
netsnmp_file_create(void)
{
    netsnmp_file *filei = SNMP_MALLOC_TYPEDEF(netsnmp_file);

    /*
     * 0 is a valid file descriptor, so init to -1
     */
    if (NULL != filei)
        filei->fd = -1;
    else {
        snmp_log(LOG_WARNING,"failed to malloc netsnmp_file structure\n");
    }

    return filei;
}

/**
 * open file and get stats
 */
netsnmp_file *
netsnmp_file_new(const char *name, int fs_flags, mode_t mode, u_int ns_flags)
{
    netsnmp_file *filei = netsnmp_file_fill(NULL, name, fs_flags, mode, 0);
    if (NULL == filei)
        return NULL;

    if (ns_flags & NETSNMP_FILE_STATS) {
        filei->stats = (struct stat*)calloc(1, sizeof(*(filei->stats)));
        if (NULL == filei->stats)
            DEBUGMSGT(("nsfile:new", "no memory for stats\n"));
        else if (stat(name, filei->stats) != 0)
            DEBUGMSGT(("nsfile:new", "error getting stats\n"));
    }

    if (ns_flags & NETSNMP_FILE_AUTO_OPEN)
        netsnmp_file_open(filei);

    return filei;
}

        
/**
 * fill core members in a netsnmp_file structure
 *
 * @param filei      structure to fill; if NULL, a new one will be allocated
 * @param name       file name
 * @param fs_flags   filesystem flags passed to open()
 * @param mode       optional filesystem open modes passed to open()
 * @param ns_flags   net-snmp flags
 */
netsnmp_file *
netsnmp_file_fill(netsnmp_file * filei, const char* name,
                  int fs_flags, mode_t mode, u_int ns_flags)
{
    if (NULL == filei) {
        filei = netsnmp_file_create();
        if (NULL == filei) /* failure already logged */
            return NULL;
    }

    if (NULL != name)
        filei->name = strdup(name);

    filei->fs_flags = fs_flags;
    filei->ns_flags = ns_flags;
    filei->mode = mode;

    return filei;
}

/**
 * release a netsnmp_file structure
 *
 * @retval  see close() man page
 */
int
netsnmp_file_release(netsnmp_file * filei)
{
    int rc = 0;

    if (NULL == filei)
        return -1;

    if ((filei->fd > 0) && NS_FI_AUTOCLOSE(filei->ns_flags))
        rc = close(filei->fd);

    if (NULL != filei->name)
        free(filei->name); /* no point in SNMP_FREE */

    if (NULL != filei->extras)
        netsnmp_free_all_list_data(filei->extras);

    if (NULL != filei->stats)
        free(filei->stats);

    SNMP_FREE(filei);

    return rc;
}

/**
 * open the file associated with a netsnmp_file structure
 *
 * @retval -1  : error opening file
 * @retval >=0 : file descriptor for opened file
 */
int
netsnmp_file_open(netsnmp_file * filei)
{
    /*
     * basic sanity checks
     */
    if ((NULL == filei) || (NULL == filei->name))
        return -1;

    /*
     * if file is already open, just return the fd.
     */
    if (-1 != filei->fd)
        return filei->fd;

    /*
     * try to open the file, loging an error if we failed
     */
    if (0 == filei->mode)
        filei->fd = open(filei->name, filei->fs_flags);
    else
        filei->fd = open(filei->name, filei->fs_flags, filei->mode);

    if (filei->fd < 0) {
        DEBUGMSGTL(("netsnmp_file", "error opening %s (%d)\n", filei->name, errno));
    }

    /*
     * return results
     */
    return filei->fd;
}


/**
 * close the file associated with a netsnmp_file structure
 *
 * @retval  0 : success
 * @retval -1 : error
 */
#ifndef NETSNMP_FEATURE_REMOVE_FILE_CLOSE
int
netsnmp_file_close(netsnmp_file * filei)
{
    int rc;

    /*
     * basic sanity checks
     */
    if ((NULL == filei) || (NULL != filei->name))
        return -1;

    /*
     * make sure it's not already closed
     */
    if (-1 == filei->fd) {
        return 0;
    }

    /*
     * close the file, logging an error if we failed
     */
    rc = close(filei->fd);
    if (rc < 0) {
        DEBUGMSGTL(("netsnmp_file", "error closing %s (%d)\n", filei->name, errno));
    }
    else
        filei->fd = -1;

    return rc;
}
#endif /* NETSNMP_FEATURE_REMOVE_FILE_CLOSE */

void
netsnmp_file_container_free(netsnmp_file *file, void *context)
{
    netsnmp_file_release(file);
}

int
netsnmp_file_compare_name(netsnmp_file *lhs, netsnmp_file *rhs)
{
    netsnmp_assert((NULL != lhs) && (NULL != rhs));
    netsnmp_assert((NULL != lhs->name) && (NULL != rhs->name));

    return strcmp(lhs->name, rhs->name);
}
#else /* NETSNMP_FEATURE_REMOVE_FILE_UTILS */
netsnmp_feature_unused(file_utils);
#endif /* NETSNMP_FEATURE_REMOVE_FILE_UTILS */