xref: /trafficserver/mgmt/ConfigManager.cc (revision cd148543)
1 /** @file
2 
3   This file contains code for class to allow management of configuration files
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 #include "tscore/ink_platform.h"
25 #include "tscore/ink_memory.h"
26 #include "tscore/ink_time.h"
27 #include "Alarms.h"
28 #include "LocalManager.h"
29 #include "ConfigManager.h"
30 #include "WebMgmtUtils.h"
31 #include "ExpandingArray.h"
32 #include "MgmtSocket.h"
33 #include "tscore/I_Layout.h"
34 #include "FileManager.h"
35 #include "ProxyConfig.h"
36 
37 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
38 #define TS_ARCHIVE_STAT_MTIME(t) ((t).st_mtime * 1000000000 + (t).st_mtimespec.tv_nsec)
39 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
40 #define TS_ARCHIVE_STAT_MTIME(t) ((t).st_mtime * 1000000000 + (t).st_mtim.tv_nsec)
41 #else
42 #define TS_ARCHIVE_STAT_MTIME(t) ((t).st_mtime * 1000000000)
43 #endif
44 
ConfigManager(const char * fileName_,const char * configName_,bool root_access_needed_,ConfigManager * parentConfig_)45 ConfigManager::ConfigManager(const char *fileName_, const char *configName_, bool root_access_needed_, ConfigManager *parentConfig_)
46   : root_access_needed(root_access_needed_), parentConfig(parentConfig_)
47 {
48   ExpandingArray existVer(25, true); // Existing versions
49   struct stat fileInfo;
50   ink_assert(fileName_ != nullptr);
51 
52   // parent must not also have a parent
53   if (parentConfig) {
54     ink_assert(parentConfig->parentConfig == nullptr);
55   }
56 
57   // Copy the file name.
58   fileName   = ats_strdup(fileName_);
59   configName = ats_strdup(configName_);
60 
61   ink_mutex_init(&fileAccessLock);
62 
63   // Check to make sure that our configuration file exists
64   //
65   if (statFile(&fileInfo) < 0) {
66     // If we can't find an active version because there is none we have a hard failure.
67     mgmt_fatal(0, "[ConfigManager::ConfigManager] Unable to find configuration file %s.\n\tStat failed : %s\n", fileName,
68                strerror(errno));
69 
70   } else {
71     fileLastModified = TS_ARCHIVE_STAT_MTIME(fileInfo);
72   }
73 }
74 
~ConfigManager()75 ConfigManager::~ConfigManager()
76 {
77   ats_free(fileName);
78 }
79 
80 //
81 //
82 // int ConfigManager::statFile()
83 //
84 //  A wrapper for stat()
85 //
86 int
statFile(struct stat * buf)87 ConfigManager::statFile(struct stat *buf)
88 {
89   int statResult;
90   std::string sysconfdir(RecConfigReadConfigDir());
91   std::string filePath = Layout::get()->relative_to(sysconfdir, fileName);
92 
93   statResult = root_access_needed ? elevating_stat(filePath.c_str(), buf) : stat(filePath.c_str(), buf);
94 
95   return statResult;
96 }
97 
98 // bool ConfigManager::checkForUserUpdate(RollBackCheckType how)
99 //
100 //  Called to check if the file has been changed  by the user.
101 //  Timestamps are compared to see if a change occurred
102 bool
checkForUserUpdate(RollBackCheckType how)103 ConfigManager::checkForUserUpdate(RollBackCheckType how)
104 {
105   struct stat fileInfo;
106   bool result;
107 
108   ink_mutex_acquire(&fileAccessLock);
109 
110   if (this->statFile(&fileInfo) < 0) {
111     ink_mutex_release(&fileAccessLock);
112     return false;
113   }
114 
115   if (fileLastModified < TS_ARCHIVE_STAT_MTIME(fileInfo)) {
116     if (how == ROLLBACK_CHECK_AND_UPDATE) {
117       fileLastModified = TS_ARCHIVE_STAT_MTIME(fileInfo);
118       configFiles->fileChanged(fileName, configName);
119       mgmt_log("User has changed config file %s\n", fileName);
120     }
121     result = true;
122   } else {
123     result = false;
124   }
125 
126   ink_mutex_release(&fileAccessLock);
127   return result;
128 }
129