github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/priority_execve.cc (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     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  #include <errno.h>
    16  #include <stdio.h>
    17  #include <stdlib.h>
    18  #include <sys/resource.h>
    19  #include <sys/time.h>
    20  #include <sys/types.h>
    21  #include <unistd.h>
    22  
    23  int main(int argc, char** argv, char** envp) {
    24    errno = 0;
    25    int prio = getpriority(PRIO_PROCESS, getpid());
    26  
    27    // NOTE: getpriority() can legitimately return negative values
    28    // in the range [-20, 0). If errno is set, exit with a value that
    29    // could not be reached by a valid priority. Valid exit values
    30    // for the test are in the range [1, 40], so we'll use 0.
    31    if (errno != 0) {
    32      printf("getpriority() failed with errno = %d\n", errno);
    33      exit(0);
    34    }
    35  
    36    // Used by test to verify priority is being maintained through
    37    // calls to execve(). Since prio should always be in the range
    38    // [-20, 19], we offset by 20 so as not to have negative exit codes.
    39    exit(20 - prio);
    40  
    41    return 0;
    42  }