1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "selinux_error.h"
17 #include <cmath>
18 #include <cstdlib>
19
20 namespace selinux {
21 static const char *g_selinuxErrMsg[SELINUX_ERROR_MAX] = {
22 [SELINUX_SUCC] = "success",
23 [SELINUX_ARG_INVALID] = "argument is invalid",
24 [SELINUX_PATH_INVAILD] = "path is invalid",
25 [SELINUX_STAT_INVAILD] = "stat failed",
26 [SELINUX_PTR_NULL] = "ptr is null",
27 [SELINUX_KEY_NOT_FOUND] = "cannot find key in contexts file",
28 [SELINUX_GET_CONTEXT_ERROR] = "get context failed",
29 [SELINUX_SET_CONTEXT_ERROR] = "set context failed",
30 [SELINUX_SET_CONTEXT_TYPE_ERROR] = "set context type failed",
31 [SELINUX_CHECK_CONTEXT_ERROR] = "check context failed, context may not define",
32 [SELINUX_CONTEXTS_FILE_LOAD_ERROR] = "load contexts file failed",
33 [SELINUX_FTS_OPEN_ERROR] = "fts_open failed",
34 [SELINUX_FTS_ELOOP] = "fts ELOOP",
35 [SELINUX_RESTORECON_ERROR] = "hap path restorecon error",
36 [SELINUX_UNKNOWN_ERROR] = "unknown error",
37 [SELINUX_PERMISSION_DENY] = "permission denied",
38 };
39
GetErrStr(int errNo)40 const char *GetErrStr(int errNo)
41 {
42 int errIndex = std::abs(errNo);
43 errIndex = errIndex >= SELINUX_ERROR_MAX ? SELINUX_UNKNOWN_ERROR : errIndex;
44 return g_selinuxErrMsg[errIndex];
45 }
46 } // namespace selinux
47