summaryrefslogtreecommitdiffstats
path: root/logfile.c
blob: c1ffb15216148d78440b7078c43375647a50a907 (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
/* Copyright (c) 2008, 2009
 *      Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
 *      Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
 *      Micah Cowan (micah@cowan.name)
 *      Sadrul Habib Chowdhury (sadrul@users.sourceforge.net)
 * Copyright (c) 1993-2002, 2003, 2005, 2006, 2007
 *      Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
 *      Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
 * Copyright (c) 1987 Oliver Laumann
 *
 * 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 3, 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 (see the file COPYING); if not, see
 * http://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 *
 ****************************************************************
 */

#include <sys/types.h>		/* dev_t, ino_t, off_t, ... */
#include <sys/stat.h>		/* struct stat */
#include <fcntl.h>		/* O_WRONLY for logfile_reopen */

#include "config.h"
#include "screen.h"
#include "extern.h"
#include "logfile.h"

static void changed_logfile __P((struct logfile *));
static struct logfile *lookup_logfile __P((char *));
static int stolen_logfile __P((struct logfile *));

static struct logfile *logroot = NULL;

static void changed_logfile(struct logfile *l) {
  struct stat o, *s = l->st;

  if (fstat(fileno(l->fp), &o) < 0)  /* get trouble later */
    return;
  if (o.st_size > s->st_size) {      /* aha, appended text */
    s->st_size = o.st_size;          /* this should have changed */
    s->st_mtime = o.st_mtime;        /* only size and mtime */
  }
}

/*
 * Requires fd to be open and need_fd to be closed.
 * If possible, need_fd will be open afterwards and refer to 
 * the object originally reffered by fd. fd will be closed then.
 * Works just like ``fcntl(fd, DUPFD, need_fd); close(fd);''
 * 
 * need_fd is returned on success, else -1 is returned.
 */
int lf_move_fd(int fd, int need_fd) {
  int r = -1;
  
  if (fd == need_fd)
    return fd;
  if (fd >=0 && fd < need_fd)
    r = lf_move_fd(dup(fd), need_fd);
  close(fd);
  return r;
}

static int logfile_reopen(char *name, int wantfd, struct logfile *l) {
  int got_fd;

  close(wantfd);
  if (((got_fd = open(name, O_WRONLY | O_CREAT | O_APPEND, 0666)) < 0) || lf_move_fd(got_fd, wantfd) < 0) {
    logfclose(l);
    debug1("logfile_reopen: failed for %s\n", name);
    return -1;
  }
  changed_logfile(l);
  debug2("logfile_reopen: %d = %s\n", wantfd, name);
  return 0;
}

static int (* lf_reopen_fn)() = logfile_reopen;

/* 
 * Whenever logfwrite discoveres that it is required to close and
 * reopen the logfile, the function registered here is called.
 * If you do not register anything here, the above logfile_reopen()
 * will be used instead.
 * Your function should perform the same steps as logfile_reopen():
 * a) close the original filedescriptor without flushing any output
 * b) open a new logfile for future output on the same filedescriptor number.
 * c) zero out st_dev, st_ino to tell the stolen_logfile() indcator to 
 *    reinitialise itself.
 * d) return 0 on success.
 */
void logreopen_register(fn)
int (*fn) __P((char *, int, struct logfile *));
{
  lf_reopen_fn = fn ? fn : logfile_reopen;
}

/*
 * If the logfile has been removed, truncated, unlinked or the like,
 * return nonzero.
 * The l->st structure initialised by logfopen is updated
 * on every call.
 */
static int stolen_logfile(struct logfile *l) {
  struct stat o, *s = l->st;

  o = *s;
  if (fstat(fileno(l->fp), s) < 0)              /* remember that stat failed */
    s->st_ino = s->st_dev = 0;

  ASSERT(s == l->st);
  if (!o.st_dev && !o.st_ino)                   /* nothing to compare with */
    return 0;

  if ((!s->st_dev && !s->st_ino) ||             /* stat failed, that's new! */
      !s->st_nlink ||                           /* red alert: file unlinked */
      (s->st_size < o.st_size) ||               /*           file truncated */
      (s->st_mtime != o.st_mtime) ||            /*            file modified */
      ((s->st_ctime != o.st_ctime) &&           /*     file changed (moved) */
       !(s->st_mtime == s->st_ctime &&          /*  and it was not a change */
         o.st_ctime < s->st_ctime)))            /* due to delayed nfs write */
  {
    debug1("stolen_logfile: %s stolen!\n", l->name);
    debug3("st_dev %d, st_ino %d, st_nlink %d\n", (int)s->st_dev, (int)s->st_ino, (int)s->st_nlink);
    debug2("s->st_size %d, o.st_size %d\n", (int)s->st_size, (int)o.st_size);
    debug2("s->st_mtime %d, o.st_mtime %d\n", (int)s->st_mtime, (int)o.st_mtime);
    debug2("s->st_ctime %d, o.st_ctime %d\n", (int)s->st_ctime, (int)o.st_ctime);
    return -1;
  }
  debug1("stolen_logfile: %s o.k.\n", l->name);
  return 0;
}

static struct logfile *lookup_logfile(char *name) {
  struct logfile *l;

  for (l = logroot; l; l = l->next)
    if (!strcmp(name, l->name))
      return l;
  return NULL;
}

struct logfile *logfopen(char *name, FILE *fp) {
  struct logfile *l;

  if (!fp) {
    if (!(l = lookup_logfile(name)))
      return NULL;
    l->opencount++;
    return l;
  }

  if (!(l = (struct logfile *)malloc(sizeof(struct logfile))))
    return NULL;
  if (!(l->st = (struct stat *)malloc(sizeof(struct stat)))) {
    free((char *)l);
    return NULL;
  }

  if (!(l->name = SaveStr(name))) {
    free((char *)l->st);
    free((char *)l);
    return NULL;
  }
  l->fp = fp;
  l->opencount = 1;
  l->writecount = 0;
  l->flushcount = 0;
  changed_logfile(l);

  l->next = logroot;
  logroot = l;
  return l;
}

int islogfile(char *name) {
  if (!name)
    return logroot ? 1 : 0;
  return lookup_logfile(name) ? 1 : 0;
}

int logfclose(struct logfile *l) {
  struct logfile **lp;

  for (lp = &logroot; *lp; lp = &(*lp)->next)
    if (*lp == l)
      break;

  if (!*lp)
    return -1;

  if ((--l->opencount) > 0)
    return 0;
  if (l->opencount < 0)
    abort();

  *lp = l->next;
  fclose(l->fp);
  free(l->name);
  free((char *)l);
  return 0;
}

/* 
 * XXX
 * write and flush both *should* check the file's stat, if it disappeared
 * or changed, re-open it.
 */
int logfwrite(struct logfile *l, char *buf, int n) {
  int r;

  if (stolen_logfile(l) && lf_reopen_fn(l->name, fileno(l->fp), l))
    return -1;
  r = fwrite(buf, n, 1, l->fp);
  l->writecount += l->flushcount + 1;
  l->flushcount = 0;
  changed_logfile(l); 
  return r;
}

int logfflush(struct logfile *l) {
  int r = 0;

  if (!l)
    for (l = logroot; l; l = l->next) {
      if (stolen_logfile(l) && lf_reopen_fn(l->name, fileno(l->fp), l))
        return -1;
      r |= fflush(l->fp);
      l->flushcount++;
      changed_logfile(l); 
    }
  else {
    if (stolen_logfile(l) && lf_reopen_fn(l->name, fileno(l->fp), l))
      return -1;
    r = fflush(l->fp);
    l->flushcount++;
    changed_logfile(l);
  }
  return r;
}