File: conmen

package info (click to toggle)
conman 0.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,600 kB
  • sloc: ansic: 11,863; sh: 2,174; exp: 2,132; makefile: 270; perl: 120
file content (174 lines) | stat: -rwxr-xr-x 5,678 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/perl

###############################################################################
# ConMen: a wrapper to aid in ConMan broadcast sessions.
###############################################################################
# Written by Chris Dunlap <cdunlap@llnl.gov>.
# Copyright (C) 2007-2022 Lawrence Livermore National Security, LLC.
# Copyright (C) 2001-2007 The Regents of the University of California.
# UCRL-CODE-2002-009.
#
# This file is part of ConMan: The Console Manager.
# For details, see <https://dun.github.io/conman/>.
#
# ConMan is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# ConMan is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with ConMan.  If not, see <http://www.gnu.org/licenses/>.
###############################################################################
# ConMen spawns a terminal emulator for each of the selected consoles
#   and uses the current tty as the broadcast window.
# All of the spawned client windows are terminated when the
#   broadcast window session is closed.
###############################################################################

use Getopt::Std;


$default_xterm = "xterm";

$SIG{TERM} = exit_handler;

$ENV{DISPLAY}
    or die("ERROR: DISPLAY not set.\n");

$have_genders = !system("command -v nodeattr 1>/dev/null 2>&1");

print_usage() if (!@ARGV || !getopts('d:e:fg:G:hjLmqrT:V') || $opt_h);
@patterns = @ARGV;
exec("conman", "-L") or die("ERROR: Cannot exec conman: $!\n") if ($opt_L);
exec("conman", "-V") or die("ERROR: Cannot exec conman: $!\n") if ($opt_V);

($opt_f + $opt_j + $opt_m <= 1)
    or die("ERROR: Only one mode [fjm] can be specified.\n");

if ($opt_f) {
    $mode = "-f";
} elsif ($opt_j) {
    $mode = "-j";
} elsif ($opt_m) {
    $mode = "-m";
} else {
    $mode = "-m";
}

@conman = ("conman", "-Q");
push(@conman, ("-d", $opt_d)) if ($opt_d);
push(@conman, ("-e", $opt_e)) if ($opt_e);
push(@conman, ("-r")) if ($opt_r);

@xterm = ($opt_T || $default_xterm);
!system("command -v $xterm[0] 1>/dev/null 2>&1")
    or die("ERROR: Cannot exec $xterm[0]\n");
push(@xterm, ("-geometry", $opt_G)) if ($opt_G);

query_consoles();
spawn_consoles();
exit_handler();
kill(-9, $$);
exit(0);


sub exit_handler
{
    kill('TERM', @pids);
    select(undef, undef, undef, 0.5);
    kill('KILL', @pids);
    exit(0);
}


sub print_usage
{
    $help = `conman -h` or die("ERROR: Cannot exec conman: $!\n");
    ($default_dst) = ($help =~ /-d .* \[([^\]]*)\]/);
    ($default_esc) = ($help =~ /-e .* \[([^\]]*)\]/);

    ($prog) = ($0 =~ m|(?:.*/)?([^/]*)|);
    print("Usage: $prog [OPTIONS] [CONSOLES]\n");
    print("\n");
    print("  -d HOST   Specify server destination. [$default_dst]\n");
    print("  -e CHAR   Specify escape character. [$default_esc]\n");
    print("  -f        Force connections (console-stealing).\n");
    print("  -g QUERY  Specify nodes matching a genders nodeattr query.\n")
        if ($have_genders);
    print("  -G SIZE   Specify terminal emulator geometry (\"80x24\").\n");
    print("  -h        Display this help.\n");
    print("  -j        Join connections (console-sharing).\n");
    print("  -L        Display license information.\n");
    print("  -m        Monitor connections (read-only).\n");
    print("  -q        Query server about specified console(s).\n");
    print("  -r        Match console names via regex instead of globbing.\n");
    print("  -T PROG   Specify terminal emulator. [$default_xterm]\n");
    print("  -V        Display version information.\n");
    print("\n");
    exit(0);
}


sub query_consoles
{
    if ($opt_g) {
        if ($have_genders) {
            chomp($nodes = `nodeattr -c $opt_g`);
            push(@patterns, split(/,/, $nodes));
        }
        else {
            die("ERROR: Cannot query genders: nodeattr not found\n");
        }
    }

    die("ERROR: Cannot fork process for console query: $!\n")
        unless defined($pid = open(QUERY, "-|"));
    if ($pid == 0) {
        exec(@conman, "-q", @patterns)
            or die("ERROR: Cannot exec conman for console query: $!\n");
    } else {
        chomp(@consoles = <QUERY>);
        close(QUERY);
    }

    if ($opt_q) {
        foreach (@consoles) { print("$_\n"); }
        exit(0);
    }
    if (@consoles == 0) {
        exit(1);
    } elsif (@consoles == 1) {
        exec(@conman, $mode, $consoles[0])
            or die("ERROR: Cannot exec conman for console $consoles[0]: $!\n");
    }
}


sub spawn_consoles
{
    foreach $console (@consoles) {
        $name = "ConMan: $console";
        if (!defined($pid = fork)) {
            die("ERROR: Cannot fork $xterm[0] for console $console: $!\n");
        } elsif ($pid == 0) {
            local $SIG{TERM} = 'DEFAULT';
            exec(@xterm, "-T", $name, "-e", @conman, $mode, $console)
                or die("ERROR: Cannot exec $xterm[0] for console $console: $!\n");
        } else {
            push(@pids, $pid);
        }
    }
    if (!defined($pid = fork)) {
        die("ERROR: Cannot fork $xterm[0] for console broadcast: $!\n");
    } elsif ($pid == 0) {
        exec(@conman, "-b", "-j", @patterns)
            or die("ERROR: Cannot exec $xterm[0] for console broadcast: $!\n");
    } else {
        waitpid($pid, 0);
    }
}