summaryrefslogtreecommitdiffstats
path: root/version_library/library/src/ss_ver.cc
blob: 70f120c584d50aaa625764c8eb0ec46c99c263cc (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
 *
 * Licensed 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.
 */

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/file.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>

#include <native_service/ns_logger_if.h>
#include <system_service/ss_templates.h>

#include <system_service/ss_ver.h>

// For DEBUG
#define ZONE_INIT    ZONEMASK(10)
#define ZONE_FUNC    ZONEMASK(11)
#define ZONE_MEM     ZONEMASK(12)
#define ZONE_INFO    ZONEMASK(29)
#define ZONE_WARN    ZONEMASK(30)
#define ZONE_ERR     ZONEMASK(31)

#define ZONE_DEFAULT ZONE_FUNC

#define SSVER_TMPDIR "/tmp/ssver"

// FILE IO
class CPkgInfoIo {
 private:
  int m_fd;
  std::string m_path;
  static std::map<std::string, int> m_dbgCnt;  // Debug information for exclusive leakage check

  void openFD(const int32_t mode) {
    m_fd = open(m_path.c_str(), mode, 0664);  // LCOV_EXCL_BR_LINE 11:except,C++ STL
    if (m_fd == -1) {
      SS_ASERT_ERRNO(0);  // LCOV_EXCL_BR_LINE 15: marco defined in ss_templates.h
      throw eFrameworkunifiedStatusFail;
    }

    // File lock to protect file accesses
    SS_ASERT_ERRNO(0 == flock(m_fd, LOCK_EX));  // LCOV_EXCL_BR_LINE 15: marco defined in ss_templates.h
    // LCOV_EXCL_BR_START 11:Unexpected branch
    m_dbgCnt[m_path]++;
    SS_ASERT_LOG(m_dbgCnt[m_path] == 1, "%s:%d", m_path.c_str(),  // LCOV_EXCL_BR_LINE 15: marco define @ss_templates.h
                 m_dbgCnt[m_path]);
    // LCOV_EXCL_BR_STOP
  }

public:
  CPkgInfoIo(const std::string& pkgName)
    : m_fd(-1),
      m_path(SSVER_TMPDIR) {  // LCOV_EXCL_BR_LINE 11:Unexpected branch
    FRAMEWORKUNIFIEDLOG(ZONE_DEFAULT,__FUNCTION__,"+:%s",pkgName.c_str());
    m_path += "/";
    m_path += pkgName;
 
     FRAMEWORKUNIFIEDLOG(ZONE_DEFAULT,__FUNCTION__,"-");
   }

  ~CPkgInfoIo() {
    FRAMEWORKUNIFIEDLOG(ZONE_DEFAULT, __FUNCTION__, "+");

    // LCOV_EXCL_BR_START 11:Unexpected branch
    m_dbgCnt[m_path]--;
    SS_ASERT_LOG(m_dbgCnt[m_path] == 0, "%s:%d", m_path.c_str(),  // LCOV_EXCL_BR_LINE 15: marco define @ss_templates.h
                 m_dbgCnt[m_path]);
    SS_ASERT_ERRNO(0 == flock(m_fd, LOCK_UN));  // LCOV_EXCL_BR_LINE 15: marco defined in ss_templates.h

    if (m_fd != -1) {
      SS_ASERT_ERRNO(0 == close(m_fd));  // LCOV_EXCL_BR_LINE 15: marco defined in ss_templates.h
    }
    // LCOV_EXCL_BR_STOP
    FRAMEWORKUNIFIEDLOG(ZONE_DEFAULT, __FUNCTION__, "-");
  }

  void putData(SSVER_PkgInfo &info) {  //  NOLINT (readability/nolint)
    FRAMEWORKUNIFIEDLOG(ZONE_DEFAULT, __FUNCTION__, "+");
    openFD(O_RDWR|O_CREAT|O_CLOEXEC);
    ssize_t wsz;
    wsz = write(m_fd, &info, sizeof(info));
    if (wsz != sizeof(info)) {  // LCOV_EXCL_BR_LINE 5:std c lib error process
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed write(). wsz=%d size=%d errno=%d",  // LCOV_EXCL_LINE 5: glibc fail safe
             wsz, sizeof(info), errno);  // LCOV_EXCL_LINE 5:std c lib error process
      throw eFrameworkunifiedStatusFail;  // LCOV_EXCL_LINE 5:std c lib error process
    }
    FRAMEWORKUNIFIEDLOG(ZONE_DEFAULT, __FUNCTION__, "-");
  }

  void getData(SSVER_PkgInfo* p_info) {
    FRAMEWORKUNIFIEDLOG(ZONE_DEFAULT, __FUNCTION__, "+");
    openFD(O_RDONLY|O_CLOEXEC);

    ssize_t rsz;
    rsz = my_read(m_fd, p_info, sizeof(*p_info));
    if (rsz != sizeof(*p_info)) {  // LCOV_EXCL_BR_LINE 5:std c lib error process
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed read(). rsz=%d size=%d", rsz,  // LCOV_EXCL_LINE 5:std c lib error process
             sizeof(*p_info));  // LCOV_EXCL_LINE 5:std c lib error process
      p_info->version[0] = '\0';  // LCOV_EXCL_LINE 5:std c lib error process
      p_info->date[0] = '\0';  // LCOV_EXCL_LINE 5:std c lib error process
      throw eFrameworkunifiedStatusFail;  // LCOV_EXCL_LINE 5:std c lib error process
    }
    FRAMEWORKUNIFIEDLOG(ZONE_DEFAULT, __FUNCTION__, "-");
  }

  int my_read(int fd, void* buf, int size) {
    int done = 0;
    char *buffer = reinterpret_cast<char*>(buf);
    int sz = done;

    while (size > 0) {
      done = read(fd, buffer + done, size);
      if (done < 0) {  // LCOV_EXCL_BR_LINE 5:std c lib error process
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

        SS_ASERT_ERRNO(0);  // LCOV_EXCL_LINE 5:std c lib error process
        if (errno == EINTR) {  // LCOV_EXCL_LINE 5:std c lib error process
          continue;
        } else {
          return done;
        }
      } else if (done == 0) {  // LCOV_EXCL_BR_LINE 5:std c lib error process
        return sz;
      }
      size -= done;
      sz += done;
    }
    return sz;
  }
};

std::map<std::string, int> CPkgInfoIo::m_dbgCnt;  // Actual status

//==================================================================
//                  private
//==================================================================
void CSSVer::dump(void) const {  // LCOV_EXCL_START 7:debugging process
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

  std::string dumpStr;
  dumpStr = "VERINFO DUMP\n";

  for (SSVerPkgList::const_iterator ite = m_verList.begin();
      ite != m_verList.end(); ite++) {
    dumpStr += ite->first;
    dumpStr += "\n";
    dumpStr += ite->second.version;
    dumpStr += "\n";
    dumpStr += ite->second.date;
    dumpStr += "\n";
  }

  FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "%s", dumpStr.c_str());
}
// LCOV_EXCL_STOP

//==================================================================
//                  PUBLIC
//==================================================================
CSSVer::CSSVer() {
  FRAMEWORKUNIFIEDLOG(ZONE_DEFAULT, __FUNCTION__, "+");
  DIR *dir = NULL;
  struct dirent dent, *next;
  struct stat statbuf;

  try {
    if (stat(SSVER_TMPDIR, &statbuf) != 0) {
      mkdir(SSVER_TMPDIR, 0775);
      // Because this process is not locked, if multiple processes process at the same time,
      // although it may have been created and fail, do not ASSERT it because it is harmless.
    }

    // Linux dependency codes
    if ((dir = opendir(SSVER_TMPDIR)) == NULL) {  // LCOV_EXCL_BR_LINE 5:std c lib error process
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

      SS_ASERT_ERRNO(0);  // LCOV_EXCL_LINE 5:std c lib error process
      throw eFrameworkunifiedStatusFail;
    }

    // LCOV_EXCL_BR_START 11:Unexpected branch
    while (0 == readdir_r(dir, &dent, &next) && next) {
      if (DT_REG == dent.d_type) {
        SSVER_PkgInfo info;
        std::string name(dent.d_name);
        std::string verInfo;

        CPkgInfoIo pkgIo(name);
        pkgIo.getData(&info);

        verInfo += "\nPACKAGE :";
        verInfo += name;
        verInfo += "\n";
        verInfo += "  VERSION:";
        verInfo += info.version;
        verInfo += "\n";
        verInfo += "  DATE   :";
        verInfo += info.date;
        verInfo += "\n";
        FRAMEWORKUNIFIEDLOG(ZONE_DEFAULT, __FUNCTION__, "%s", verInfo.c_str());

        m_verList.insert(std::make_pair(name, info));
      }
    }
    SS_ASERT_ERRNO(0 == closedir(dir));  // LCOV_EXCL_BR_LINE 15: marco defined in ss_templates.h
    // LCOV_EXCL_BR_STOP
  } catch (...) {  // LCOV_EXCL_START 6:unable to reach
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

    if (dir) {
      SS_ASERT_ERRNO(0 == closedir(dir));
    }
  }  // LCOV_EXCL_STOP
  FRAMEWORKUNIFIEDLOG(ZONE_DEFAULT, __FUNCTION__, "-");
}

CSSVer::~CSSVer() {
  FRAMEWORKUNIFIEDLOG(ZONE_DEFAULT, __FUNCTION__, "+");
  FRAMEWORKUNIFIEDLOG(ZONE_DEFAULT, __FUNCTION__, "-");
}

EFrameworkunifiedStatus CSSVer::getPkgInfo(const std::string &name, SSVER_PkgInfo* p_info) {
  FRAMEWORKUNIFIEDLOG(ZONE_DEFAULT, __FUNCTION__, "+");
  EFrameworkunifiedStatus versionlibraryRet = eFrameworkunifiedStatusOK;
  try {
    if (p_info == NULL) {
      SS_ASERT(0);  // LCOV_EXCL_BR_LINE 15: marco defined in ss_templates.h
      throw eFrameworkunifiedStatusFail;
    }

    SSVerPkgList::iterator ite = m_verList.find(name);

    if (ite != m_verList.end()) {
      // cache hit
      *p_info = ite->second;
    } else {
      // cache miss
      std::string path(SSVER_TMPDIR);  // LCOV_EXCL_BR_LINE 11:except,C++ STL
      path += "/";
      path += name;

      if (access(path.c_str(), F_OK) == 0) {
        // cache refresh
        CPkgInfoIo pkgIo(name);  // LCOV_EXCL_BR_LINE 11:Unexpected branch
        pkgIo.getData(p_info);  // LCOV_EXCL_BR_LINE 11:except,C++ STL
        m_verList.insert(std::make_pair(name, *p_info));  // LCOV_EXCL_BR_LINE 11:except,C++ STL
      } else {
        throw eFrameworkunifiedStatusFileLoadError;
      }
    }
  } catch (EFrameworkunifiedStatus ee) {
    versionlibraryRet = ee;
  } catch (...) {  // LCOV_EXCL_START 6:unable to reach
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

    versionlibraryRet = eFrameworkunifiedStatusFail;
  }  // LCOV_EXCL_STOP
  FRAMEWORKUNIFIEDLOG(ZONE_DEFAULT, __FUNCTION__, "-");
  return versionlibraryRet;
}

EFrameworkunifiedStatus CSSVer::setPkgInfo(const std::string &name, SSVER_PkgInfo &info) {
  FRAMEWORKUNIFIEDLOG(ZONE_DEFAULT, __FUNCTION__, "+");
  EFrameworkunifiedStatus versionlibraryRet = eFrameworkunifiedStatusOK;
  try {
    // LCOV_EXCL_BR_START 11:Unexpected branch
    SSVerPkgList::iterator ite = m_verList.find(name);

    if (ite == m_verList.end()) {
      m_verList.insert(std::make_pair(name, info));
      CPkgInfoIo pkgIo(name);
      pkgIo.putData(info);
    } else {
      ite->second = info;
      CPkgInfoIo pkgIo(name);
      pkgIo.putData(info);
    }
    // LCOV_EXCL_BR_STOP
  } catch (EFrameworkunifiedStatus ee) {
    versionlibraryRet = ee;
  } catch (...) {  // LCOV_EXCL_START 6:unable to reach
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

    SS_ASERT(0);  // LCOV_EXCL_BR_LINE 15: marco defined in ss_templates.h
    versionlibraryRet = eFrameworkunifiedStatusFail;
  }   // LCOV_EXCL_STOP

  FRAMEWORKUNIFIEDLOG(ZONE_DEFAULT, __FUNCTION__, "-");
  return versionlibraryRet;
}  // LCOV_EXCL_BR_LINE 10:for the last line