1 /*
2  * JPty - A small PTY interface for Java.
3  *
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *    http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21 package com.pty4j.unix.linux;
22 
23 import com.pty4j.unix.PtyHelpers;
24 import com.sun.jna.Library;
25 import com.sun.jna.Native;
26 import com.sun.jna.ptr.IntByReference;
27 import jtermios.JTermios;
28 
29 /**
30  * Provides a {@link com.pty4j.unix.PtyHelpers.OSFacade} implementation for Linux.
31  */
32 public class OSFacadeImpl implements PtyHelpers.OSFacade {
33   // INNER TYPES
34 
35   private interface C_lib extends Library {
kill(int pid, int signal)36     int kill(int pid, int signal);
37 
waitpid(int pid, int[] stat, int options)38     int waitpid(int pid, int[] stat, int options);
39 
sigprocmask(int how, IntByReference set, IntByReference oldset)40     int sigprocmask(int how, IntByReference set, IntByReference oldset);
41 
strerror(int errno)42     String strerror(int errno);
43 
grantpt(int fdm)44     int grantpt(int fdm);
45 
unlockpt(int fdm)46     int unlockpt(int fdm);
47 
close(int fd)48     int close(int fd);
49 
ptsname(int fd)50     String ptsname(int fd);
51 
open(String pts_name, int o_rdwr)52     int open(String pts_name, int o_rdwr);
53 
killpg(int pid, int sig)54     int killpg(int pid, int sig);
55 
fork()56     int fork();
57 
setsid()58     int setsid();
59 
getpid()60     int getpid();
61 
setpgid(int pid, int pgid)62     int setpgid(int pid, int pgid);
63 
dup2(int fd, int fileno)64     void dup2(int fd, int fileno);
65 
getppid()66     int getppid();
67 
unsetenv(String s)68     void unsetenv(String s);
69 
chdir(String dirpath)70     void chdir(String dirpath);
71   }
72 
73   public interface Linux_Util_lib extends Library {
login_tty(int fd)74     int login_tty(int fd);
75   }
76 
77   private static final C_lib m_Clib = Native.loadLibrary("c", C_lib.class);
78 
79   private static final Linux_Util_lib m_Utillib = Native.loadLibrary("util", Linux_Util_lib.class);
80 
81   // CONSTUCTORS
82 
83   /**
84    * Creates a new {@link OSFacadeImpl} instance.
85    */
OSFacadeImpl()86   public OSFacadeImpl() {
87     PtyHelpers.ONLCR = 0x04;
88 
89     PtyHelpers.VINTR = 0;
90     PtyHelpers.VQUIT = 1;
91     PtyHelpers.VERASE = 2;
92     PtyHelpers.VKILL = 3;
93     PtyHelpers.VSUSP = 10;
94     PtyHelpers.VREPRINT = 12;
95     PtyHelpers.VWERASE = 14;
96 
97     PtyHelpers.ECHOKE = 0x01;
98     PtyHelpers.ECHOCTL = 0x40;
99   }
100 
101   // METHODS
102 
103   @Override
kill(int pid, int signal)104   public int kill(int pid, int signal) {
105     return m_Clib.kill(pid, signal);
106   }
107 
108   @Override
waitpid(int pid, int[] stat, int options)109   public int waitpid(int pid, int[] stat, int options) {
110     return m_Clib.waitpid(pid, stat, options);
111   }
112 
113   @Override
sigprocmask(int how, IntByReference set, IntByReference oldset)114   public int sigprocmask(int how, IntByReference set, IntByReference oldset) {
115     return m_Clib.sigprocmask(how, set, oldset);
116   }
117 
118   @Override
strerror(int errno)119   public String strerror(int errno) {
120     return m_Clib.strerror(errno);
121   }
122 
123   @Override
getpt()124   public int getpt() {
125     return JTermios.open("/dev/ptmx", JTermios.O_RDWR | JTermios.O_NOCTTY);
126   }
127 
128   @Override
grantpt(int fd)129   public int grantpt(int fd) {
130     return m_Clib.grantpt(fd);
131   }
132 
133   @Override
unlockpt(int fd)134   public int unlockpt(int fd) {
135     return m_Clib.unlockpt(fd);
136   }
137 
138   @Override
close(int fd)139   public int close(int fd) {
140     return m_Clib.close(fd);
141   }
142 
143   @Override
ptsname(int fd)144   public String ptsname(int fd) {
145     return m_Clib.ptsname(fd);
146   }
147 
148   @Override
killpg(int pid, int sig)149   public int killpg(int pid, int sig) {
150     return m_Clib.killpg(pid, sig);
151   }
152 
153   @Override
fork()154   public int fork() {
155     return m_Clib.fork();
156   }
157 
158   @Override
pipe(int[] pipe2)159   public int pipe(int[] pipe2) {
160     return JTermios.pipe(pipe2);
161   }
162 
163   @Override
setsid()164   public int setsid() {
165     return m_Clib.setsid();
166   }
167 
168   @Override
getpid()169   public int getpid() {
170     return m_Clib.getpid();
171   }
172 
173   @Override
setpgid(int pid, int pgid)174   public int setpgid(int pid, int pgid) {
175     return m_Clib.setpgid(pid, pgid);
176   }
177 
178   @Override
dup2(int fds, int fileno)179   public void dup2(int fds, int fileno) {
180     m_Clib.dup2(fds, fileno);
181   }
182 
183   @Override
getppid()184   public int getppid() {
185     return m_Clib.getppid();
186   }
187 
188   @Override
unsetenv(String s)189   public void unsetenv(String s) {
190     m_Clib.unsetenv(s);
191   }
192 
193   @Override
login_tty(int fd)194   public int login_tty(int fd) {
195     return m_Utillib.login_tty(fd);
196   }
197 
198   @Override
chdir(String dirpath)199   public void chdir(String dirpath) {
200     m_Clib.chdir(dirpath);
201   }
202 }
203