]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/log-file.c
Load all users only when really needed
[sojka/lightdm.git] / src / log-file.c
1 /*
2  * Copyright (C) 2015 Alexandros Frantzis
3  * Author: Alexandros Frantzis <alexandros.frantzis@canonical.com>
4  *
5  * This program is free software: you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License as published by the Free Software
7  * Foundation, either version 3 of the License, or (at your option) any later
8  * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
9  * license.
10  */
11
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <stdio.h>
15
16 #include "log-file.h"
17
18 int
19 log_file_open (const gchar *log_filename, LogMode log_mode)
20 {
21     int open_flags = O_WRONLY | O_CREAT;
22     int log_fd;
23
24     if (log_mode == LOG_MODE_BACKUP_AND_TRUNCATE)
25     {
26         /* Move old file out of the way */
27         gchar *old_filename;
28
29         old_filename = g_strdup_printf ("%s.old", log_filename);
30         rename (log_filename, old_filename);
31         g_free (old_filename);
32
33         open_flags |= O_TRUNC;
34     }
35     else if (log_mode == LOG_MODE_APPEND)
36     {
37         /* Keep appending to it */
38         open_flags |= O_APPEND;
39     }
40     else
41     {
42         g_warning ("Failed to open log file %s: invalid log mode %d specified",
43                    log_filename, log_mode);
44         return -1;
45     }
46
47     /* Open file and log to it */
48     log_fd = open (log_filename, open_flags, 0600);
49     if (log_fd < 0)
50         g_warning ("Failed to open log file %s: %s", log_filename, g_strerror (errno));
51
52     return log_fd;
53 }