~xnox/ubuntu/saucy/lxc/dep8

« back to all changes in this revision

Viewing changes to src/lxc/log.c

  • Committer: Stéphane Graber
  • Date: 2013-02-18 15:20:18 UTC
  • mto: This revision was merged to the branch mainline in revision 190.
  • Revision ID: stgraber@ubuntu.com-20130218152018-ls2gi9hkqs2kuhj8
Tags: upstream-0.9.0~alpha3
Import upstream version 0.9.0~alpha3

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
 
42
42
int lxc_log_fd = -1;
43
43
static char log_prefix[LXC_LOG_PREFIX_SIZE] = "lxc";
 
44
static int lxc_loglevel_specified = 0;
 
45
// if logfile was specifed on command line, it won't be overridden by lxc.logfile
 
46
static int lxc_log_specified = 0;
44
47
 
45
48
lxc_log_define(lxc_log, lxc);
46
49
 
122
125
        log_prefix[sizeof(log_prefix) - 1] = 0;
123
126
}
124
127
 
 
128
static int build_dir(const char *name)
 
129
{
 
130
        char *n = strdup(name);  // because we'll be modifying it
 
131
        char *p, *e;
 
132
        int ret;
 
133
 
 
134
        if (!n) {
 
135
                ERROR("Out of memory while creating directory '%s'.", name);
 
136
                return -1;
 
137
        }
 
138
 
 
139
        e = &n[strlen(n)];
 
140
        for (p = n+1; p < e; p++) {
 
141
                if (*p != '/')
 
142
                        continue;
 
143
                *p = '\0';
 
144
                if (access(n, F_OK)) {
 
145
                        ret = lxc_unpriv(mkdir(n, 0755));
 
146
                        if (ret && errno != -EEXIST) {
 
147
                                SYSERROR("failed to create directory '%s'.", n);
 
148
                                free(n);
 
149
                                return -1;
 
150
                        }
 
151
                }
 
152
                *p = '/';
 
153
        }
 
154
        free(n);
 
155
        return 0;
 
156
}
 
157
 
125
158
/*---------------------------------------------------------------------------*/
126
159
static int log_open(const char *name)
127
160
{
147
180
        return newfd;
148
181
}
149
182
 
 
183
static char *build_log_path(const char *name)
 
184
{
 
185
        char *p;
 
186
        int len, ret;
 
187
 
 
188
        /*
 
189
         * '$logpath' + '/' + '$name' + '.log' + '\0'
 
190
         * or
 
191
         * '$logpath' + '/' + '$name' + '/' + '$name' + '.log' + '\0'
 
192
         * sizeof(LOGPATH) includes its \0
 
193
         */
 
194
        len = sizeof(LOGPATH) + strlen(name) + 6;
 
195
#if USE_CONFIGPATH_LOGS
 
196
        len += strlen(name) + 1;  /* add "/$container_name/" */
 
197
#endif
 
198
        p = malloc(len);
 
199
        if (!p)
 
200
                return p;
 
201
#if USE_CONFIGPATH_LOGS
 
202
        ret = snprintf(p, len, "%s/%s/%s.log", LOGPATH, name, name);
 
203
#else
 
204
        ret = snprintf(p, len, "%s/%s.log", LOGPATH, name);
 
205
#endif
 
206
        if (ret < 0 || ret >= len) {
 
207
                free(p);
 
208
                return NULL;
 
209
        }
 
210
        return p;
 
211
}
 
212
 
 
213
int do_lxc_log_set_file(const char *fname, int from_default);
 
214
 
150
215
/*---------------------------------------------------------------------------*/
151
 
extern int lxc_log_init(const char *file, const char *priority,
152
 
                        const char *prefix, int quiet)
 
216
extern int lxc_log_init(const char *name, const char *file,
 
217
                        const char *priority, const char *prefix, int quiet)
153
218
{
154
219
        int lxc_priority = LXC_LOG_PRIORITY_ERROR;
 
220
        int ret;
 
221
        char *tmpfile = NULL;
 
222
        int want_lxc_log_specified = 0;
 
223
 
 
224
        if (lxc_log_fd != -1)
 
225
                return 0;
155
226
 
156
227
        if (priority) {
 
228
                lxc_loglevel_specified = 1;
157
229
                lxc_priority = lxc_log_priority_to_int(priority);
158
230
 
159
231
                if (lxc_priority == LXC_LOG_PRIORITY_NOTSET) {
171
243
        if (prefix)
172
244
                lxc_log_setprefix(prefix);
173
245
 
174
 
        if (file) {
175
 
                int fd;
 
246
        if (file && strcmp(file, "none") == 0) {
 
247
                want_lxc_log_specified = 1;
 
248
                return 0;
 
249
        }
176
250
 
177
 
                fd = log_open(file);
178
 
                if (fd == -1) {
179
 
                        ERROR("failed to initialize log service");
 
251
        if (!file) {
 
252
                tmpfile = build_log_path(name);
 
253
                if (!tmpfile) {
 
254
                        ERROR("could not build log path");
180
255
                        return -1;
181
256
                }
182
 
 
183
 
                lxc_log_fd = fd;
184
 
        }
185
 
 
186
 
        return 0;
 
257
        } else {
 
258
                want_lxc_log_specified = 1;
 
259
        }
 
260
 
 
261
        ret = do_lxc_log_set_file(tmpfile ? tmpfile : file, !want_lxc_log_specified);
 
262
 
 
263
        if (want_lxc_log_specified)
 
264
                lxc_log_specified = 1;
 
265
        /*
 
266
         * If !want_lxc_log_specified, that is, if the user did not request
 
267
         * this logpath, then ignore failures and continue logging to console
 
268
         */
 
269
        if (!want_lxc_log_specified && ret != 0) {
 
270
                INFO("Ignoring failure to open default logfile.");
 
271
                ret = 0;
 
272
        }
 
273
 
 
274
        if (tmpfile)
 
275
                free(tmpfile);
 
276
 
 
277
        return ret;
 
278
}
 
279
 
 
280
/*
 
281
 * This is called when we read a lxc.loglevel entry in a lxc.conf file.  This
 
282
 * happens after processing command line arguments, which override the .conf
 
283
 * settings.  So only set the level if previously unset.
 
284
 */
 
285
extern int lxc_log_set_level(int level)
 
286
{
 
287
        if (lxc_loglevel_specified)
 
288
                return 0;
 
289
        if (level < 0 || level >= LXC_LOG_PRIORITY_NOTSET) {
 
290
                ERROR("invalid log priority %d", level);
 
291
                return -1;
 
292
        }
 
293
        lxc_log_category_lxc.priority = level;
 
294
        return 0;
 
295
}
 
296
 
 
297
char *log_fname;  // default to NULL, set in lxc_log_set_file.
 
298
/*
 
299
 * This can be called:
 
300
 *   1. when a program calls lxc_log_init with no logfile parameter (in which
 
301
 *      case the default is used).  In this case lxc.logfile can override this.
 
302
 *   2. when a program calls lxc_log_init with a logfile parameter.  In this
 
303
 *      case we don't want lxc.logfile to override this.
 
304
 *   3. When a lxc.logfile entry is found in config file.
 
305
 */
 
306
int do_lxc_log_set_file(const char *fname, int from_default)
 
307
{
 
308
        if (lxc_log_specified) {
 
309
                INFO("lxc.logfile overriden by command line");
 
310
                return 0;
 
311
        }
 
312
        if (lxc_log_fd != -1) {
 
313
                // we are overriding the default.
 
314
                close(lxc_log_fd);
 
315
                free(log_fname);
 
316
        }
 
317
 
 
318
#if USE_CONFIGPATH_LOGS
 
319
        // we don't build_dir for the default if the default is
 
320
        // i.e. /var/lib/lxc/$container/$container.log
 
321
        if (!from_default)
 
322
#endif
 
323
        if (build_dir(fname)) {
 
324
                ERROR("failed to create dir for log file \"%s\" : %s", fname,
 
325
                      strerror(errno));
 
326
                return -1;
 
327
        }
 
328
 
 
329
        lxc_log_fd = log_open(fname);
 
330
        if (lxc_log_fd == -1)
 
331
                return -1;
 
332
 
 
333
        log_fname = strdup(fname);
 
334
        return 0;
 
335
}
 
336
 
 
337
extern int lxc_log_set_file(const char *fname)
 
338
{
 
339
        return do_lxc_log_set_file(fname, 0);
 
340
}
 
341
 
 
342
extern int lxc_log_get_level(void)
 
343
{
 
344
        if (!lxc_loglevel_specified)
 
345
                return LXC_LOG_PRIORITY_NOTSET;
 
346
        return lxc_log_category_lxc.priority;
 
347
}
 
348
 
 
349
extern const char *lxc_log_get_file(void)
 
350
{
 
351
        return log_fname;
187
352
}