1 /*
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>
20 #include <sys/stat.h>
21 #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
32 char *__real_getenv(const char *name);
33
34 static char modified_gcov_prefix[128];
35
__wrap_getenv(const char * name)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());
42 mkdir(modified_gcov_prefix, 0777);
43 return modified_gcov_prefix;
44 }
45