blob: 309f0606ba2e8f6b4501d7decee68d29e205fa2f [file] [log] [blame]
Pirama Arumuga Nainara277ce92019-07-01 22:13:10 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdlib.h>
18#include <string.h>
19#include <unistd.h>
Pirama Arumuga Nainarb00f2fc2019-07-26 14:49:58 -070020#include <sys/stat.h>
Pirama Arumuga Nainara277ce92019-07-01 22:13:10 -070021#include <sys/types.h>
22
23// This file provides a wrapper for getenv that appends the userid (geteuid())
24// of the current process to GCOV_PREFIX. This avoids conflicts and permissions
25// issues when different processes try to create/access the same directories and
26// files under $GCOV_PREFIX.
27//
28// When this file is linked to a binary, the -Wl,--wrap,getenv flag must be
29// used. The linker redirects calls to getenv to __wrap_getenv and sets
30// __real_getenv to point to libc's getenv.
31
32char *__real_getenv(const char *name);
33
34static char modified_gcov_prefix[128];
35
36__attribute__((weak)) char *__wrap_getenv(const char *name) {
37 if (strcmp(name, "GCOV_PREFIX") != 0) {
38 return __real_getenv(name);
39 }
40
41 sprintf(modified_gcov_prefix, "%s/%u", __real_getenv(name), geteuid());
Pirama Arumuga Nainarb00f2fc2019-07-26 14:49:58 -070042 mkdir(modified_gcov_prefix, 0777);
Pirama Arumuga Nainara277ce92019-07-01 22:13:10 -070043 return modified_gcov_prefix;
44}