1  /* Emulate sigstack function using sigaltstack.
       2     Copyright (C) 1998-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <signal.h>
      20  #include <stddef.h>
      21  #include <sys/syscall.h>
      22  
      23  
      24  int
      25  sigstack (struct sigstack *ss, struct sigstack *oss)
      26  {
      27    stack_t sas;
      28    stack_t *sasp = NULL;
      29    stack_t osas;
      30    stack_t *osasp = oss == NULL ? NULL : &osas;
      31    int result;
      32  
      33    if (ss != NULL)
      34      {
      35        /* We have to convert the information.  */
      36        sas.ss_sp = ss->ss_sp;
      37        sas.ss_flags = ss->ss_onstack ? SS_ONSTACK : 0;
      38  
      39        /* For the size of the stack we have no value we can pass to the
      40  	 kernel.  This is why this function should not be used.  We simply
      41  	 assume that all the memory down to address zero (in case the stack
      42  	 grows down) is available.  */
      43        sas.ss_size = ss->ss_sp - NULL;
      44  
      45        sasp = &sas;
      46      }
      47  
      48    /* Call the kernel.  */
      49    result = __sigaltstack (sasp, osasp);
      50  
      51    /* Convert the result, if wanted and possible.  */
      52    if (result == 0 && oss != NULL)
      53      {
      54        oss->ss_sp = osas.ss_sp;
      55        oss->ss_onstack = (osas.ss_flags & SS_ONSTACK) != 0;
      56      }
      57  
      58    return result;
      59  }
      60  
      61  link_warning (sigstack, "the `sigstack' function is dangerous.  `sigaltstack' should be used instead.")