"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-pty.c" (15 Mar 2019, 5808 Bytes) of package /linux/privat/stress-ng-0.09.56.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "stress-pty.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.09.50_vs_0.09.51.

    1 /*
    2  * Copyright (C) 2013-2019 Canonical, Ltd.
    3  *
    4  * This program is free software; you can redistribute it and/or
    5  * modify it under the terms of the GNU General Public License
    6  * as published by the Free Software Foundation; either version 2
    7  * of the License, or (at your option) any later version.
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with this program; if not, write to the Free Software
   16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
   17  *
   18  * This code is a complete clean re-write of the stress tool by
   19  * Colin Ian King <colin.king@canonical.com> and attempts to be
   20  * backwardly compatible with the stress tool by Amos Waterland
   21  * <apw@rossby.metr.ou.edu> but has more stress tests and more
   22  * functionality.
   23  *
   24  */
   25 #include "stress-ng.h"
   26 
   27 #if defined(HAVE_TERMIOS_H) &&  \
   28     defined(HAVE_TERMIO_H) && \
   29     defined(HAVE_PTSNAME)
   30 
   31 typedef struct {
   32     char *slavename;
   33     int master;
   34     int slave;
   35 } pty_info_t;
   36 
   37 #endif
   38 
   39 /*
   40  *  stress_set_pty_max()
   41  *  set ptr maximum
   42  */
   43 int stress_set_pty_max(const char *opt)
   44 {
   45     uint64_t pty_max;
   46 
   47     pty_max = get_uint64(opt);
   48     check_range("pty-max", pty_max,
   49         MIN_PTYS, MAX_PTYS);
   50     return set_setting("pty-max", TYPE_ID_UINT64, &pty_max);
   51 }
   52 
   53 #if defined(HAVE_TERMIOS_H) &&  \
   54     defined(HAVE_TERMIO_H) &&   \
   55     defined(HAVE_PTSNAME)
   56 
   57 /*
   58  *  stress_pty
   59  *  stress pty handling
   60  */
   61 static int stress_pty(const args_t *args)
   62 {
   63     int rc = EXIT_FAILURE;
   64     uint64_t pty_max = DEFAULT_PTYS;
   65 
   66     (void)get_setting("pty-max", &pty_max);
   67 
   68     do {
   69         size_t i, n;
   70         pty_info_t ptys[pty_max];
   71 
   72         (void)memset(ptys, 0, sizeof ptys);
   73 
   74         for (n = 0; n < pty_max; n++) {
   75             ptys[n].slave = -1;
   76             ptys[n].master = open("/dev/ptmx", O_RDWR);
   77             if (ptys[n].master < 0) {
   78                 if ((errno != ENOMEM) &&
   79                     (errno != ENOSPC) &&
   80                     (errno != EIO) &&
   81                     (errno != EMFILE)) {
   82                     pr_fail_err("open /dev/ptmx");
   83                     goto clean;
   84                 }
   85             } else {
   86                 ptys[n].slavename = ptsname(ptys[n].master);
   87                 if (!ptys[n].slavename) {
   88                     pr_fail_err("ptsname");
   89                     goto clean;
   90                 }
   91                 if (grantpt(ptys[n].master) < 0) {
   92                     pr_fail_err("grantpt");
   93                     goto clean;
   94                 }
   95                 if (unlockpt(ptys[n].master) < 0) {
   96                     pr_fail_err("unlockpt");
   97                     goto clean;
   98                 }
   99                 ptys[n].slave = open(ptys[n].slavename, O_RDWR);
  100                 if (ptys[n].slave < 0) {
  101                     if (errno != EMFILE) {
  102                         pr_fail_err("open slave pty");
  103                         goto clean;
  104                     }
  105                 }
  106             }
  107             if (!g_keep_stressing_flag)
  108                 goto clean;
  109         }
  110         /*
  111          *  ... and exercise ioctls ...
  112          */
  113         for (i = 0; i < n; i++) {
  114             if ((ptys[i].master < 0) || (ptys[i].slave < 0))
  115                 continue;
  116 #if defined(TCGETS)
  117             {
  118                 struct termios ios;
  119 
  120                 if (ioctl(ptys[i].slave, TCGETS, &ios) < 0)
  121                     pr_fail_err("ioctl TCGETS on slave pty");   
  122             }
  123 #endif
  124 #if defined(TCSETS)
  125             {
  126                 struct termios ios;
  127 
  128                 if (ioctl(ptys[i].slave, TCSETS, &ios) < 0)
  129                     pr_fail_err("ioctl TCSETS on slave pty");
  130             }
  131 #endif
  132 #if defined(TCSETSW)
  133             {
  134                 struct termios ios;
  135 
  136                 if (ioctl(ptys[i].slave, TCSETSW, &ios) < 0)
  137                     pr_fail_err("ioctl TCSETSW on slave pty");
  138             }
  139 #endif
  140 #if defined(TCSETSF)
  141             {
  142                 struct termios ios;
  143 
  144                 if (ioctl(ptys[i].slave, TCSETSF, &ios) < 0)
  145                     pr_fail_err("ioctl TCSETSF on slave pty");
  146             }
  147 #endif
  148 #if defined(TCGETA)
  149             {
  150                 struct termio io;
  151 
  152                 if (ioctl(ptys[i].slave, TCGETA, &io) < 0)
  153                     pr_fail_err("ioctl TCGETA on slave pty");
  154             }
  155 #endif
  156 #if defined(TCSETA)
  157             {
  158                 struct termio io;
  159 
  160                 if (ioctl(ptys[i].slave, TCSETA, &io) < 0)
  161                     pr_fail_err("ioctl TCSETA on slave pty");
  162             }
  163 #endif
  164 #if defined(TCSETAW)
  165             {
  166                 struct termio io;
  167 
  168                 if (ioctl(ptys[i].slave, TCSETAW, &io) < 0)
  169                     pr_fail_err("ioctl TCSETAW on slave pty");
  170             }
  171 #endif
  172 #if defined(TCSETAF)
  173             {
  174                 struct termio io;
  175 
  176                 if (ioctl(ptys[i].slave, TCSETAF, &io) < 0)
  177                     pr_fail_err("ioctl TCSETAF on slave pty");
  178             }
  179 #endif
  180 #if defined(TIOCGLCKTRMIOS)
  181             {
  182                 struct termios ios;
  183 
  184                 if (ioctl(ptys[i].slave, TIOCGLCKTRMIOS, &ios) < 0)
  185                     pr_fail_err("ioctl TIOCGLCKTRMIOS on slave pty");
  186             }
  187 #endif
  188 #if defined(TIOCGLCKTRMIOS)
  189             {
  190                 struct termios ios;
  191 
  192                 if (ioctl(ptys[i].slave, TIOCGLCKTRMIOS, &ios) < 0)
  193                     pr_fail_err("ioctl TIOCGLCKTRMIOS on slave pty");
  194             }
  195 #endif
  196 #if defined(TIOCGWINSZ) 
  197             {
  198                 struct winsize ws;
  199 
  200                 if (ioctl(ptys[i].slave, TIOCGWINSZ, &ws) < 0)
  201                     pr_fail_err("ioctl TIOCGWINSZ on slave pty");
  202             }
  203 #endif
  204 #if defined(TIOCSWINSZ)
  205             {
  206                 struct winsize ws;
  207 
  208                 if (ioctl(ptys[i].slave, TIOCSWINSZ, &ws) < 0)
  209                     pr_fail_err("ioctl TIOCSWINSZ on slave pty");
  210             }
  211 #endif
  212 #if defined(FIONREAD)
  213             {
  214                 int arg;
  215 
  216                 if (ioctl(ptys[i].slave, FIONREAD, &arg) < 0)
  217                     pr_fail_err("ioctl FIONREAD on slave pty");
  218             }
  219 #endif
  220 #if defined(TIOCINQ)
  221             {
  222                 int arg;
  223 
  224                 if (ioctl(ptys[i].slave, TIOCINQ, &arg) < 0)
  225                     pr_fail_err("ioctl TIOCINQ on slave pty");
  226             }
  227 #endif
  228 #if defined(TIOCOUTQ)
  229             {
  230                 int arg;
  231 
  232                 if (ioctl(ptys[i].slave, TIOCOUTQ, &arg) < 0)
  233                     pr_fail_err("ioctl TIOCOUTQ on slave pty");
  234             }
  235 #endif
  236 
  237             if (!g_keep_stressing_flag)
  238                 goto clean;
  239         }
  240 clean:
  241         /*
  242          *  and close
  243          */
  244         for (i = 0; i < n; i++) {
  245             if (ptys[i].slave != -1)
  246                 (void)close(ptys[i].slave);
  247             if (ptys[i].master != -1)
  248                 (void)close(ptys[i].master);
  249         }
  250         inc_counter(args);
  251     } while (keep_stressing());
  252 
  253     rc = EXIT_SUCCESS;
  254 
  255     return rc;
  256 }
  257 
  258 stressor_info_t stress_pty_info = {
  259     .stressor = stress_pty,
  260     .class = CLASS_OS
  261 };
  262 #else
  263 stressor_info_t stress_pty_info = {
  264     .stressor = stress_not_implemented,
  265     .class = CLASS_OS
  266 };
  267 #endif