/* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * Simple program to rotate Apache logs without having to kill the server. * * Contributed by Ben Laurie * * 12 Mar 1996 */ #include #include #include #include #include #include #include #if defined(WIN32) || defined(OS2) #include #endif #define BUFSIZE 65536 #define ERRMSGSZ 82 #ifndef MAX_PATH #define MAX_PATH 1024 #endif int main (int argc, char **argv) { char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ], startMsgBuf[512]; time_t tLogEnd = 0, tRotation; int startMsgLen; int nLogFD = -1, nLogFDprev = -1, nMessCount = 0, nRead, nWrite; int utc_offset = 0; int use_strftime = 0; time_t now; char *szLogRoot; #ifdef TPF /* set up signal handling to avoid default OPR-I007777 dump */ signal(SIGPIPE, exit); signal(SIGTERM, exit); #endif if (argc < 3) { fprintf(stderr, "Usage: %s " "[offset minutes from UTC]\n\n", argv[0]); #ifdef OS2 fprintf(stderr, "Add this:\n\nTransferLog \"|%s.exe /some/where 86400\"\n\n", argv[0]); #else fprintf(stderr, "Add this:\n\nTransferLog \"|%s /some/where 86400\"\n\n", argv[0]); #endif fprintf(stderr, "to httpd.conf. The generated name will be /some/where.nnnn " "where nnnn is the\nsystem time at which the log nominally " "starts (N.B. this time will always be a\nmultiple of the " "rotation time, so you can synchronize cron scripts with it).\n" "At the end of each rotation time a new log is started.\n"); exit(1); } szLogRoot = argv[1]; if (argc >= 4) { utc_offset = atoi(argv[3]) * 60; } tRotation = atoi(argv[2]); if (tRotation <= 0) { fprintf(stderr, "Rotation time must be > 0\n"); exit(6); } #if defined(WIN32) || defined(OS2) setmode(0, O_BINARY); #endif use_strftime = (strstr(szLogRoot, "%") != NULL); for (;;) { struct tm *tm_now; struct tm *header; time_t tLogStart; time_t headerTime; nRead = read(0, buf, sizeof buf); now = time(NULL) + utc_offset; tLogStart = (now / tRotation) * tRotation; tm_now = gmtime(&tLogStart); headerTime = time(NULL); header = localtime(&headerTime); if (nRead == 0) exit(3); if (nRead < 0) if (errno != EINTR) exit(4); if (nLogFD >= 0 && (now >= tLogEnd || nRead < 0)) { nLogFDprev = nLogFD; nLogFD = -1; } if (nLogFD < 0) { if (use_strftime) { strftime(buf2, sizeof(buf2), szLogRoot, tm_now); } else { sprintf(buf2, "%s.%010d", szLogRoot, (int) tLogStart); } tLogEnd = tLogStart + tRotation; nLogFD = open(buf2, O_WRONLY | O_CREAT | O_APPEND, 0666); if (nLogFD < 0) { /* Uh-oh. Failed to open the new log file. Try to clear * the previous log file, note the lost log entries, * and keep on truckin'. */ if (nLogFDprev == -1) { perror(buf2); exit(2); } else { nLogFD = nLogFDprev; sprintf(errbuf, "Resetting log file due to error opening " "new log file. %10d messages lost.\n", nMessCount); nWrite = strlen(errbuf); #ifdef WIN32 chsize(nLogFD, 0); #else ftruncate(nLogFD, 0); #endif write(nLogFD, errbuf, nWrite); } } else { close(nLogFDprev); } nMessCount = 0; /* Successfully created a new log file, so create a header entry for PCI compliance */ strftime(startMsgBuf, sizeof(startMsgBuf),"============ START OF LOG %c ===========\n", header); startMsgLen = strlen(startMsgBuf); write(nLogFD, startMsgBuf, startMsgLen); } do { nWrite = write(nLogFD, buf, nRead); } while (nWrite < 0 && errno == EINTR); if (nWrite != nRead) { nMessCount++; sprintf(errbuf, "Error writing to log file. " "%10d messages lost.\n", nMessCount); nWrite = strlen(errbuf); #ifdef WIN32 chsize(nLogFD, 0); #else ftruncate(nLogFD, 0); #endif write (nLogFD, errbuf, nWrite); } else { nMessCount++; } } /* We never get here, but suppress the compile warning */ return (0); }