View | Details | Raw Unified | Return to bug 27257
Collapse All | Expand All

(-)rotatelogs.c.orig (-48 / +81 lines)
Lines 69-74 Link Here
69
#include "apr_lib.h"
69
#include "apr_lib.h"
70
#include "apr_strings.h"
70
#include "apr_strings.h"
71
#include "apr_errno.h"
71
#include "apr_errno.h"
72
#include "apr_getopt.h"
72
#include "apr_file_io.h"
73
#include "apr_file_io.h"
73
#include "apr_file_info.h"
74
#include "apr_file_info.h"
74
#include "apr_general.h"
75
#include "apr_general.h"
Lines 83-88 Link Here
83
#if APR_HAVE_STRINGS_H
84
#if APR_HAVE_STRINGS_H
84
#include <strings.h>
85
#include <strings.h>
85
#endif
86
#endif
87
#ifdef HAVE_PWD_H
88
#include <pwd.h>
89
#endif
90
86
91
87
#define BUFSIZE         65536
92
#define BUFSIZE         65536
88
#define ERRMSGSZ        82
93
#define ERRMSGSZ        82
Lines 91-96 Link Here
91
#define MAX_PATH        1024
96
#define MAX_PATH        1024
92
#endif
97
#endif
93
98
99
static void usage(const char *progname)
100
{
101
fprintf(stderr,
102
"Usage: %s -f <logfile> -u [username] -t <rotation time in seconds> ",progname);
103
fprintf(stderr,
104
"-o [offset minutes from UTC] or -m <rotation size in megabytes>\n\n");
105
fprintf(stderr,
106
"E.g. add this:\n\nTransferLog \"|%s -f /some/where -t 86400\"\n\n",progname);
107
fprintf(stderr,
108
"or\n\nTransferLog \"|%s -f /some/where -m 5\"\n\n",progname);
109
fprintf(stderr,
110
"to httpd.conf. The generated name will be /some/where.nnnn "
111
"where nnnn is the\nsystem time at which the log nominally "
112
"starts (N.B. if using a rotation time,\nthe time will always "
113
"be a multiple of the rotation time, so you can synchronize\n"
114
"cron scripts with it). At the end of each rotation time or "
115
"when the file size\nis reached a new log is started.\n");
116
exit(1);
117
}
118
94
int main (int argc, const char * const argv[])
119
int main (int argc, const char * const argv[])
95
{
120
{
96
    char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ];
121
    char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ];
Lines 98-162 Link Here
98
    unsigned int sRotation = 0;
123
    unsigned int sRotation = 0;
99
    int nMessCount = 0;
124
    int nMessCount = 0;
100
    apr_size_t nRead, nWrite;
125
    apr_size_t nRead, nWrite;
126
	apr_status_t rv;
101
    int use_strftime = 0;
127
    int use_strftime = 0;
102
    int now = 0;
128
    int now = 0;
129
	int c;
103
    const char *szLogRoot;
130
    const char *szLogRoot;
131
    char *user = NULL;
104
    apr_file_t *f_stdin, *nLogFD = NULL, *nLogFDprev = NULL;
132
    apr_file_t *f_stdin, *nLogFD = NULL, *nLogFDprev = NULL;
105
    apr_pool_t *pool;
133
    apr_pool_t *pool;
106
    char *ptr = NULL;
134
	apr_getopt_t *opt;
135
	const char *optarg;
136
	struct passwd *pwd;
137
	apr_uid_t retreived_uid=0;
138
	apr_gid_t retreived_gid=0;
107
139
108
    apr_app_initialize(&argc, &argv, NULL);
140
    apr_app_initialize(&argc, &argv, NULL);
109
    atexit(apr_terminate);
141
    atexit(apr_terminate);
110
142
111
    apr_pool_create(&pool, NULL);
143
    apr_pool_create(&pool, NULL);
112
    if (argc < 3 || argc > 4) {
113
        fprintf(stderr,
114
                "Usage: %s <logfile> <rotation time in seconds> "
115
                "[offset minutes from UTC] or <rotation size in megabytes>\n\n",
116
                argv[0]);
117
#ifdef OS2
118
        fprintf(stderr,
119
                "Add this:\n\nTransferLog \"|%s.exe /some/where 86400\"\n\n",
120
                argv[0]);
121
#else
122
        fprintf(stderr,
123
                "Add this:\n\nTransferLog \"|%s /some/where 86400\"\n\n",
124
                argv[0]);
125
        fprintf(stderr,
126
                "or \n\nTransferLog \"|%s /some/where 5M\"\n\n", argv[0]);
127
#endif
128
        fprintf(stderr,
129
                "to httpd.conf. The generated name will be /some/where.nnnn "
130
                "where nnnn is the\nsystem time at which the log nominally "
131
                "starts (N.B. if using a rotation time,\nthe time will always "
132
                "be a multiple of the rotation time, so you can synchronize\n"
133
                "cron scripts with it). At the end of each rotation time or "
134
                "when the file size\nis reached a new log is started.\n");
135
        exit(1);
136
    }
137
144
138
    szLogRoot = argv[1];
145
	apr_getopt_init(&opt, pool, argc, argv);
139
146
	while ((rv=apr_getopt(opt,"u:f:t:o:m:",(char *)&c,&optarg)) == APR_SUCCESS)
140
    ptr = strchr (argv[2], 'M');
147
	{
141
    if (ptr) {
148
	 switch (c)
142
        if (*(ptr+1) == '\0') {
149
	 {
143
            sRotation = atoi(argv[2]) * 1048576;
150
	  case 'f':
144
        }
151
       szLogRoot = strdup(optarg);
145
        if (sRotation == 0) {
152
	  break;
146
            fprintf(stderr, "Invalid rotation size parameter\n");
153
	  case 'o':
147
            exit(1);
154
       utc_offset = atoi(optarg) * 60;
148
        }
155
	  break;
149
    }
156
	  case 'm':
150
    else {
157
	   sRotation = atoi(optarg) * 1048576;
151
        if (argc >= 4) {
158
	   if (sRotation == 0) {
152
            utc_offset = atoi(argv[3]) * 60;
159
	    fprintf(stderr, "Invalid rotation size parameter\n");
153
        }
160
	    exit(1);
154
        tRotation = atoi(argv[2]);
161
	   }
155
        if (tRotation <= 0) {
162
	  break;
156
            fprintf(stderr, "Rotation time must be > 0\n");
163
	  case 't':
157
            exit(6);
164
	   tRotation = atoi(optarg);
158
        }
165
	   if (tRotation <= 0) {
159
    }
166
	    fprintf(stderr, "Rotation time must be > 0\n");
167
	    exit(6);
168
	   }
169
	  break;
170
	  case 'u':
171
       user = strdup(optarg);
172
	  break;
173
	  default:
174
	   usage(argv[0]);
175
	 }
176
	}
177
	if (rv == APR_BADCH) {
178
	 usage(argv[0]);
179
	}
180
181
	if (user != NULL) {
182
#if APR_HAS_USER
183
	 rv = apr_uid_get(&retreived_uid, &retreived_gid, user, pool);
184
	 if (setuid(retreived_uid) != 0) {
185
      fprintf(stderr,"Failed to setuid %s\n",user);
186
	  exit(1);
187
	 }
188
#else
189
      fprintf(stderr,"setuid not supported by your OS\n");
190
	  exit(1);
191
#endif
192
	}
160
193
161
    use_strftime = (strchr(szLogRoot, '%') != NULL);
194
    use_strftime = (strchr(szLogRoot, '%') != NULL);
162
    if (apr_file_open_stdin(&f_stdin, pool) != APR_SUCCESS) {
195
    if (apr_file_open_stdin(&f_stdin, pool) != APR_SUCCESS) {

Return to bug 27257