Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <pthread.h> |
| 30 | |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 31 | #include <inttypes.h> |
| 32 | #include <stdio.h> |
| 33 | #include <sys/resource.h> |
| 34 | |
| 35 | #include "private/bionic_string_utils.h" |
| 36 | #include "private/ErrnoRestorer.h" |
| 37 | #include "private/libc_logging.h" |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 38 | #include "pthread_internal.h" |
| 39 | |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 40 | int pthread_attr_init(pthread_attr_t* attr) { |
Elliott Hughes | 6d33918 | 2013-02-12 16:36:04 -0800 | [diff] [blame] | 41 | attr->flags = 0; |
| 42 | attr->stack_base = NULL; |
Brian Carlstrom | 50af69e | 2013-09-13 16:34:43 -0700 | [diff] [blame] | 43 | attr->stack_size = PTHREAD_STACK_SIZE_DEFAULT; |
Elliott Hughes | 6d33918 | 2013-02-12 16:36:04 -0800 | [diff] [blame] | 44 | attr->guard_size = PAGE_SIZE; |
| 45 | attr->sched_policy = SCHED_NORMAL; |
| 46 | attr->sched_priority = 0; |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | int pthread_attr_destroy(pthread_attr_t* attr) { |
| 51 | memset(attr, 0x42, sizeof(pthread_attr_t)); |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | int pthread_attr_setdetachstate(pthread_attr_t* attr, int state) { |
| 56 | if (state == PTHREAD_CREATE_DETACHED) { |
| 57 | attr->flags |= PTHREAD_ATTR_FLAG_DETACHED; |
| 58 | } else if (state == PTHREAD_CREATE_JOINABLE) { |
| 59 | attr->flags &= ~PTHREAD_ATTR_FLAG_DETACHED; |
| 60 | } else { |
| 61 | return EINVAL; |
| 62 | } |
| 63 | return 0; |
| 64 | } |
| 65 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 66 | int pthread_attr_getdetachstate(const pthread_attr_t* attr, int* state) { |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 67 | *state = (attr->flags & PTHREAD_ATTR_FLAG_DETACHED) ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE; |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | int pthread_attr_setschedpolicy(pthread_attr_t* attr, int policy) { |
| 72 | attr->sched_policy = policy; |
| 73 | return 0; |
| 74 | } |
| 75 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 76 | int pthread_attr_getschedpolicy(const pthread_attr_t* attr, int* policy) { |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 77 | *policy = attr->sched_policy; |
| 78 | return 0; |
| 79 | } |
| 80 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 81 | int pthread_attr_setschedparam(pthread_attr_t* attr, const sched_param* param) { |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 82 | attr->sched_priority = param->sched_priority; |
| 83 | return 0; |
| 84 | } |
| 85 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 86 | int pthread_attr_getschedparam(const pthread_attr_t* attr, sched_param* param) { |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 87 | param->sched_priority = attr->sched_priority; |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | int pthread_attr_setstacksize(pthread_attr_t* attr, size_t stack_size) { |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 92 | if (stack_size < PTHREAD_STACK_MIN) { |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 93 | return EINVAL; |
| 94 | } |
| 95 | attr->stack_size = stack_size; |
| 96 | return 0; |
| 97 | } |
| 98 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 99 | int pthread_attr_getstacksize(const pthread_attr_t* attr, size_t* stack_size) { |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 100 | void* unused; |
| 101 | return pthread_attr_getstack(attr, &unused, stack_size); |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 104 | int pthread_attr_setstack(pthread_attr_t* attr, void* stack_base, size_t stack_size) { |
| 105 | if ((stack_size & (PAGE_SIZE - 1) || stack_size < PTHREAD_STACK_MIN)) { |
| 106 | return EINVAL; |
| 107 | } |
Elliott Hughes | 405f855 | 2013-10-01 17:25:28 -0700 | [diff] [blame] | 108 | if (reinterpret_cast<uintptr_t>(stack_base) & (PAGE_SIZE - 1)) { |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 109 | return EINVAL; |
| 110 | } |
| 111 | attr->stack_base = stack_base; |
| 112 | attr->stack_size = stack_size; |
| 113 | return 0; |
| 114 | } |
| 115 | |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 116 | static int __pthread_attr_getstack_main_thread(void** stack_base, size_t* stack_size) { |
| 117 | ErrnoRestorer errno_restorer; |
| 118 | |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame^] | 119 | rlimit stack_limit; |
| 120 | if (getrlimit(RLIMIT_STACK, &stack_limit) == -1) { |
| 121 | return errno; |
| 122 | } |
| 123 | |
| 124 | // If the current RLIMIT_STACK is RLIM_INFINITY, only admit to an 8MiB stack for sanity's sake. |
| 125 | if (stack_limit.rlim_cur == RLIM_INFINITY) { |
| 126 | stack_limit.rlim_cur = 8 * 1024 * 1024; |
| 127 | } |
| 128 | |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 129 | // It doesn't matter which thread we are; we're just looking for "[stack]". |
| 130 | FILE* fp = fopen("/proc/self/maps", "re"); |
| 131 | if (fp == NULL) { |
| 132 | return errno; |
| 133 | } |
| 134 | char line[BUFSIZ]; |
| 135 | while (fgets(line, sizeof(line), fp) != NULL) { |
| 136 | if (ends_with(line, " [stack]\n")) { |
| 137 | uintptr_t lo, hi; |
| 138 | if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR, &lo, &hi) == 2) { |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame^] | 139 | *stack_size = stack_limit.rlim_cur; |
| 140 | *stack_base = reinterpret_cast<void*>(hi - *stack_size); |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 141 | fclose(fp); |
| 142 | return 0; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | __libc_fatal("No [stack] line found in /proc/self/maps!"); |
| 147 | } |
| 148 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 149 | int pthread_attr_getstack(const pthread_attr_t* attr, void** stack_base, size_t* stack_size) { |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 150 | if ((attr->flags & PTHREAD_ATTR_FLAG_MAIN_THREAD) != 0) { |
| 151 | return __pthread_attr_getstack_main_thread(stack_base, stack_size); |
| 152 | } |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 153 | *stack_base = attr->stack_base; |
| 154 | *stack_size = attr->stack_size; |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | int pthread_attr_setguardsize(pthread_attr_t* attr, size_t guard_size) { |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 159 | attr->guard_size = guard_size; |
| 160 | return 0; |
| 161 | } |
| 162 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 163 | int pthread_attr_getguardsize(const pthread_attr_t* attr, size_t* guard_size) { |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 164 | *guard_size = attr->guard_size; |
| 165 | return 0; |
| 166 | } |
| 167 | |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 168 | int pthread_getattr_np(pthread_t t, pthread_attr_t* attr) { |
| 169 | *attr = reinterpret_cast<pthread_internal_t*>(t)->attr; |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 173 | int pthread_attr_setscope(pthread_attr_t*, int scope) { |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 174 | if (scope == PTHREAD_SCOPE_SYSTEM) { |
| 175 | return 0; |
| 176 | } |
| 177 | if (scope == PTHREAD_SCOPE_PROCESS) { |
| 178 | return ENOTSUP; |
| 179 | } |
| 180 | return EINVAL; |
| 181 | } |
| 182 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 183 | int pthread_attr_getscope(const pthread_attr_t*, int* scope) { |
| 184 | *scope = PTHREAD_SCOPE_SYSTEM; |
| 185 | return 0; |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 186 | } |