2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2014 Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
16 #include "hphp/util/cronolog.h"
18 #include <boost/filesystem/path.hpp>
22 /* Default permissions for files and directories that are created */
25 #define FILE_MODE ( S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH )
30 ///////////////////////////////////////////////////////////////////////////////
33 namespace fs
= boost::filesystem
;
35 /* Open a new log file: determine the start of the current
36 * period, generate the log file name from the fileTemplate,
37 * determine the end of the period and open the new log file.
39 * Returns the file descriptor of the new log file and also sets the
40 * name of the file and the start time of the next period via pointers
43 static FILE *new_log_file(const char *fileTemplate
, const char *linkname
,
44 mode_t linktype
, const char *prevlinkname
,
45 PERIODICITY periodicity
, int period_multiple
,
46 int period_delay
, char *pfilename
,
47 size_t pfilename_len
, time_t time_now
,
48 time_t *pnext_period
) {
49 time_t start_of_period
;
53 start_of_period
= start_of_this_period(time_now
, periodicity
,
55 tm
= localtime(&start_of_period
);
56 strftime(pfilename
, pfilename_len
, fileTemplate
, tm
);
57 *pnext_period
= start_of_next_period(start_of_period
, periodicity
,
58 period_multiple
) + period_delay
;
60 CRONO_DEBUG(("%s (%d): using log file \"%s\" from %s (%d) until %s (%d) "
62 timestamp(time_now
), time_now
, pfilename
,
63 timestamp(start_of_period
), start_of_period
,
64 timestamp(*pnext_period
), *pnext_period
,
65 *pnext_period
- time_now
));
67 log_fd
= open(pfilename
, O_WRONLY
|O_CREAT
|O_APPEND
, FILE_MODE
);
69 #ifndef DONT_CREATE_SUBDIRS
70 if ((log_fd
< 0) && (errno
== ENOENT
)) {
71 create_subdirs(pfilename
);
72 log_fd
= open(pfilename
, O_WRONLY
|O_CREAT
|O_APPEND
, FILE_MODE
);
82 /* Create a relative symlink to logs under linkname's directory */
83 std::string dir
= fs::path(linkname
).parent_path().native();
88 if (!strncmp(pfilename
, dir
.c_str(), dir
.length())) {
89 filename
= pfilename
+ dir
.length();
94 create_link(filename
.c_str(), linkname
, linktype
, prevlinkname
);
96 return fdopen(log_fd
, "a");
99 void Cronolog::setPeriodicity() {
100 if (m_periodicity
== UNKNOWN
) {
101 m_periodicity
= determine_periodicity((char *)m_template
.c_str());
105 FILE *Cronolog::getOutputFile() {
106 if (m_template
.empty()) return m_file
;
108 time_t time_now
= time(nullptr) + m_timeOffset
;
109 /* If the current period has not finished and there is a log file, use it */
110 if ((time_now
< m_nextPeriod
) && (m_file
)) return m_file
;
112 /* We need to open a new file under a mutex. */
115 if ((time_now
>= m_nextPeriod
)) {
116 /* the current period has finished */
118 /* We cannot close m_file because there may be other threads still
119 * writing to it. We save m_file in m_prevFile and leave it open for
120 * an entire period. We simply assume that by the end of the delay
121 * no threads should be still referencing m_prevFile and we can safely
124 if (m_prevFile
) fclose(m_prevFile
);
129 /* If there is no log file open then open a new one. */
130 if (m_file
== nullptr) {
131 const char *linkname
= m_linkName
.empty() ? nullptr : m_linkName
.c_str();
132 m_file
= new_log_file(m_template
.c_str(), linkname
, S_IFLNK
,
133 m_prevLinkName
, m_periodicity
, m_periodMultiple
,
134 m_periodDelay
, m_fileName
, sizeof(m_fileName
),
135 time_now
, &m_nextPeriod
);
141 void Cronolog::changeOwner(const string
&username
, const string
&symlink
) {
142 if (username
.empty() || symlink
.empty()) {
146 int username_length
= sysconf(_SC_GETPW_R_SIZE_MAX
);
147 if (username_length
== -1) {
148 username_length
= 512;
151 struct passwd user_info
, *user_infop
;
152 std::vector
<char> username_buf(username_length
);
154 if (getpwnam_r(username
.c_str(), &user_info
, &username_buf
[0],
155 username_length
, &user_infop
)) {
160 if (lchown(symlink
.c_str(), user_info
.pw_uid
, -1) < 0) {
161 fprintf(stderr
, "Unable to chmod %s\n", symlink
.c_str());
164 // using chown() isn't portable if it is a symlink
165 int fd
= open(symlink
.c_str(), O_RDONLY
| O_NONBLOCK
| O_NOCTTY
);
166 int success
= (fd
>= 0 ? fchown(fd
, user_info
.pw_uid
, -1) : -1);
173 fprintf(stderr
, "Unable to chmod %s\n", symlink
.c_str());
177 ///////////////////////////////////////////////////////////////////////////////