1 /*
2  * Copyright (c) 2024 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 <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/stat.h>
20 #include <sys/resource.h>
21 #include <unistd.h>
22 #include <errno.h>
23 
24 #include "hnp_base.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
HnpProcessRunCheck(const char * runPath)30 int HnpProcessRunCheck(const char *runPath)
31 {
32     char cmdBuffer[BUFFER_SIZE];
33     FILE *cmdOutput;
34 
35     HNP_LOGI("runPath[%{public}s] running check", runPath);
36 
37     /* 判断进程是否运行 */
38     cmdOutput = popen("lsof", "rb");
39     if (cmdOutput == NULL) {
40         HNP_LOGE("hnp uninstall lsof command unsuccess");
41         return HNP_ERRNO_BASE_FILE_OPEN_FAILED;
42     }
43 
44     while (fgets(cmdBuffer, sizeof(cmdBuffer), cmdOutput) != NULL) {
45         if (strstr(cmdBuffer, runPath) != NULL) {
46             HNP_LOGE("hnp install path is running now, path[%{public}s]", cmdBuffer);
47             pclose(cmdOutput);
48             return HNP_ERRNO_PROCESS_RUNNING;
49         }
50     }
51 
52     pclose(cmdOutput);
53     return 0;
54 }
55 
HnpRelPath(const char * fromPath,const char * toPath,char * relPath)56 APPSPAWN_STATIC void HnpRelPath(const char *fromPath, const char *toPath, char *relPath)
57 {
58     char *from = strdup(fromPath);
59     if (from == NULL) {
60         return;
61     }
62     char *to = strdup(toPath);
63     if (to == NULL) {
64         free(from);
65         return;
66     }
67     int numDirs = 0;
68     int ret;
69 
70     char *fromHead = from;
71     char *toHead = to;
72 
73     while ((*from != '\0') && (*to != '\0') && (*from == *to)) {
74         from++;
75         to++;
76     }
77 
78     while (from > fromHead && *(from - 1) != DIR_SPLIT_SYMBOL) {
79         from--;
80         to--;
81     }
82 
83     char *p = from;
84     while (*p) {
85         if (*p == DIR_SPLIT_SYMBOL) {
86             numDirs++;
87         }
88         p++;
89     }
90 
91     for (int i = 0; i < numDirs; i++) {
92         ret = strcat_s(relPath, MAX_FILE_PATH_LEN, "../");
93         if (ret != 0) {
94             HNP_LOGE("Failed to strcat rel path");
95             free(fromHead);
96             free(toHead);
97             return;
98         }
99     }
100     ret = strcat_s(relPath, MAX_FILE_PATH_LEN, to);
101     if (ret != 0) {
102         HNP_LOGE("Failed to strcat rel path");
103         free(fromHead);
104         free(toHead);
105         return;
106     }
107 
108     free(fromHead);
109     free(toHead);
110 
111     return;
112 }
113 
HnpSymlink(const char * srcFile,const char * dstFile)114 int HnpSymlink(const char *srcFile, const char *dstFile)
115 {
116     int ret;
117     char relpath[MAX_FILE_PATH_LEN];
118 
119     (void)unlink(dstFile);
120 
121     HnpRelPath(dstFile, srcFile, relpath);
122     ret = symlink(relpath, dstFile);
123     if (ret < 0) {
124         HNP_LOGE("hnp install generate soft link unsuccess, src:%{public}s, dst:%{public}s, errno:%{public}d", srcFile,
125             dstFile, errno);
126         return HNP_ERRNO_GENERATE_SOFT_LINK_FAILED;
127     }
128 
129     return 0;
130 }
131 
132 #ifdef __cplusplus
133 }
134 #endif
135