init_service.h 3.2 KB
Newer Older
1
/*
2
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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.
 */
#ifndef BASE_STARTUP_INITLITE_SERVICE_H
#define BASE_STARTUP_INITLITE_SERVICE_H
#include <sys/types.h>

#include "cJSON.h"
#include "init_cmds.h"
X
xionglei6 已提交
21
#include "init_service_file.h"
22
#include "init_service_socket.h"
Q
Qin Fandong 已提交
23 24 25
#ifdef WITH_SELINUX
#   include "init_selinux_param.h"
#endif // WITH_SELINUX
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include "list.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif

// return value
#define SERVICE_FAILURE (-1)
#define SERVICE_SUCCESS 0

// service attributes
#define SERVICE_ATTR_INVALID 0x001      // option invalid
#define SERVICE_ATTR_ONCE 0x002         // do not restart when it exits
#define SERVICE_ATTR_NEED_RESTART 0x004 // will restart in the near future
#define SERVICE_ATTR_NEED_STOP 0x008    // will stop in reap
#define SERVICE_ATTR_IMPORTANT 0x010    // will reboot if it crash
#define SERVICE_ATTR_CRITICAL 0x020     // critical, will reboot if it crash 4 times in 4 minutes
#define SERVICE_ATTR_DISABLED 0x040     // disabled
#define SERVICE_ATTR_CONSOLE 0x080      // console
#define SERVICE_ATTR_DYNAMIC 0x100      // dynamic service
X
xionglei6 已提交
47
#define SERVICE_ATTR_ONDEMAND 0x200     // ondemand, manage socket by init
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

#define MAX_SERVICE_NAME 32
#define MAX_WRITEPID_FILES 100

#define FULL_CAP 0xFFFFFFFF
// init
#define DEFAULT_UMASK_INIT 022

#define CAP_NUM 2

#define SERVICES_ARR_NAME_IN_JSON "services"

typedef struct {
    uid_t uID;
    gid_t *gIDArray;
    int gIDCnt;
    unsigned int *caps;
    unsigned int capsCnt;
} Perms;

typedef struct {
    int count;
    char **argv;
} ServiceArgs;

typedef struct {
    ListNode node;
    char name[MAX_SERVICE_NAME + 1];
Q
Qin Fandong 已提交
76 77 78
#ifdef WITH_SELINUX
    char secon[MAX_SECON_LEN];
#endif // WITH_SELINUX
79 80 81
    int pid;
    int crashCnt;
    time_t firstCrashTime;
X
xionglei6 已提交
82 83
    int crashCount;
    int crashTime;
84 85 86 87 88 89 90
    unsigned int attribute;
    int importance;
    Perms servPerm;
    ServiceArgs pathArgs;
    ServiceArgs writePidArgs;
    CmdLines *restartArg;
    ServiceSocket *socketCfg;
X
xionglei6 已提交
91
    ServiceFile *fileCfg;
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
} Service;

int ServiceStart(Service *service);
int ServiceStop(Service *service);
void ServiceReap(Service *service);
void ReapService(Service *service);

void NotifyServiceChange(const char *serviceName, const char *change);
int IsForbidden(const char *fieldStr);
int SetImportantValue(Service *curServ, const char *attrName, int value, int flag);
int GetServiceCaps(const cJSON *curArrItem, Service *curServ);
int ServiceExec(const Service *service);

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif

#endif // BASE_STARTUP_INITLITE_SERVICE_H