aboutsummaryrefslogtreecommitdiffstats
path: root/src/framework/common/base/liblinuxapi/nsfw_lock_file.c
blob: 0ec196ffd6ff7014e56c3e3fa703139a351e6fc3 (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
/*
*
* Copyright (c) 2018 Huawei Technologies Co.,Ltd.
* 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 <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#include "types.h"
#include "nstack_securec.h"
#include "nsfw_init.h"
#include "common_mem_api.h"

#include "nstack_log.h"
#include "nsfw_maintain_api.h"
#include "nsfw_mgr_com_api.h"

#include "nsfw_base_linux_api.h"
#ifdef __cplusplus
/* *INDENT-OFF* */
extern "C"{
/* *INDENT-ON* */
#endif /* __cplusplus */

#define NSFW_FILE_PATH_LEN 128
#define LOCK_FOLDER "/ip_module/"
#define LOCK_SUFFIX ".pid"

#define read_lock(fd, offset, whence, len) nsfw_lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
#define readw_lock(fd, offset, whence, len) nsfw_lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))
#define write_lock(fd, offset, whence, len) nsfw_lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
#define writew_lock(fd, offset, whence, len) nsfw_lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))
#define un_lock(fd, offset, whence, len) nsfw_lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))

i32
nsfw_lock_reg (i32 fd, i32 cmd, i32 type, off_t offset, i32 whence, off_t len)
{
  struct flock lock_file;
  lock_file.l_type = type;
  lock_file.l_start = offset;
  lock_file.l_whence = whence;
  lock_file.l_len = len;
  return (fcntl (fd, cmd, &lock_file));
}

/*****************************************************************************
*   Prototype    : nsfw_proc_start_with_lock
*   Description  : lock file start
*   Input        : u8 proc_type
*   Output       : None
*   Return Value : i32
*   Calls        :
*   Called By    :
*****************************************************************************/
i32
nsfw_proc_start_with_lock (u8 proc_type)
{
  NSFW_LOGINF ("lock_file init]type=%u", proc_type);
  char *module_name = nsfw_get_proc_name (proc_type);
  if (NULL == module_name)
    {
      NSFW_LOGERR ("proc type error]proc_type=%u", proc_type);
      return 0;
    }

  const char *directory = NSFW_DOMAIN_DIR;
  const char *home_dir = getenv ("HOME");

  if (getuid () != 0 && home_dir != NULL)
    {
      directory = home_dir;
    }

  int ret;
  char lock_fpath[NSFW_FILE_PATH_LEN] = { 0 };
  ret = STRCPY_S (lock_fpath, NSFW_FILE_PATH_LEN, directory);
  if (EOK != ret)
    {
      NSFW_LOGERR ("lock init STRCPY_S failed]ret=%d", ret);
      return -1;
    }

  ret = STRCAT_S (lock_fpath, NSFW_FILE_PATH_LEN, LOCK_FOLDER);
  if (EOK != ret)
    {
      NSFW_LOGERR ("lock init STRCAT_S failed]ret=%d", ret);
      return -1;
    }

  ret = STRCAT_S (lock_fpath, NSFW_FILE_PATH_LEN, module_name);
  if (EOK != ret)
    {
      NSFW_LOGERR ("lock init STRCAT_S failed]ret=%d", ret);
      return -1;
    }

  ret = STRCAT_S (lock_fpath, NSFW_FILE_PATH_LEN, LOCK_SUFFIX);
  if (EOK != ret)
    {
      NSFW_LOGERR ("lock init STRCAT_S failed]ret=%d", ret);
      return -1;
    }

  i32 fd;
  if ((fd = open (lock_fpath, O_RDWR | O_CREAT, 0640)) == -1)
    {                           /* file permission no large than 0640 */
      NSFW_LOGERR ("open lock file error!]path=%s,error = %d", lock_fpath,
                   errno);
      return -1;
    }

  int rc = nsfw_set_close_on_exec (fd);
  if (rc == -1)
    {
      (void) nsfw_base_close (fd);
      NSFW_LOGERR ("set exec err]fd=%d, errno=%d", fd, errno);
      return -1;
    }

  if (write_lock (fd, 0, SEEK_SET, 0) < 0)
    {
      (void) nsfw_base_close (fd);
      NSFW_LOGERR ("get lock file error!]path=%s,error = %d", lock_fpath,
                   errno);
      return -1;
    }

  char buf[32] = { 0 };
  if (ftruncate (fd, 0) < 0)
    {
      (void) nsfw_base_close (fd);
      NSFW_LOGERR ("ftruncate file error!]path=%s,error = %d", lock_fpath,
                   errno);
      return -1;
    }

  ret =
    SNPRINTF_S (buf, sizeof (buf), sizeof (buf) - 1, "%ld", (long) getpid ());
  if (-1 == ret)
    {
      NSTCP_LOGERR ("SNPRINTF_S failed]ret=%d", ret);
      (void) nsfw_base_close (fd);
      return -1;
    }

  if (write (fd, buf, strlen (buf) + 1) < 0)
    {
      (void) nsfw_base_close (fd);
      NSFW_LOGERR ("write file error!]path=%s,error = %d", lock_fpath, errno);
      return -1;
    }

  return 0;
}

#ifdef __cplusplus
/* *INDENT-OFF* */
}
/* *INDENT-ON* */
#endif /* __cplusplus */