1 /*
2 * Copyright (c) 2025 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 "vpe_sa_utils.h"
17
18 #include <fcntl.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22
23 #include "vpe_log.h"
24
25 using namespace OHOS::Media::VideoProcessingEngine;
26
27 namespace {
28 constexpr uint32_t DEV_VALUE_SIZE = 256;
29 }
30
GetProcessName()31 std::string VpeSaUtils::GetProcessName()
32 {
33 std::string pid = std::to_string(getpid());
34 std::string devPath = "/proc/" + pid + "/cmdline";
35 pid = "pid:" + pid;
36 int fd = open(devPath.c_str(), O_RDONLY);
37 if (fd < 0) [[unlikely]] {
38 VPE_LOGW("Failed to open %{public}s! %{public}s", devPath.c_str(), strerror(errno));
39 return pid;
40 }
41 std::string name;
42 char text[DEV_VALUE_SIZE]{};
43 if (read(fd, text, sizeof(text) - 1) <= 0) [[unlikely]] {
44 VPE_LOGW("Failed to read %{public}s! %{public}s", devPath.c_str(), strerror(errno));
45 name = pid;
46 } else [[likely]] {
47 // Video app may have several process like below, and we use "tv.danmaku.bili" as the app name,
48 // and trim the suffix after ":" such as ":ijkservice" and etc.
49 // tv.danmaku.bili
50 // tv.danmaku.bili:pushservice
51 // tv.danmaku.bili:ijkservice
52 // tv.danmaku.bili:download
53 name = text;
54 auto pos = name.find_first_of(':');
55 if (pos != std::string::npos) {
56 name = name.substr(0, pos);
57 }
58 }
59 close(fd);
60 return name;
61 }
62