1 /*
2 * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library 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 Lesser General Public
15 * License along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Additionally licensed with:
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License");
21 * you may not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
23 *
24 * http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS,
28 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
31 */
32 package org.spf4j.unix;
33
34 import com.sun.jna.Library;
35 import com.sun.jna.Memory;
36 import com.sun.jna.NativeLong;
37 import com.sun.jna.StringArray;
38 import com.sun.jna.Native;
39 import com.sun.jna.Pointer;
40 import com.sun.jna.PointerType;
41 import com.sun.jna.platform.unix.LibCAPI;
42 import com.sun.jna.ptr.IntByReference;
43
44 /**
45 * GNU C library.
46 */
47 public interface CLibrary extends LibCAPI, Library {
48
49 CLibrary INSTANCE = (CLibrary) Native.loadLibrary("c", CLibrary.class);
50
51 // obtained from Linux. Needs to be checked if these values are portable.
52 int F_GETFD = 1;
53
54 int F_SETFD = 2;
55
56 int FD_CLOEXEC = 1;
57
58 int fork();
59
60 int gethostname(byte[] name, int len);
61
62 /**
63 * returns signal name from a signal number.
64 * @param sigNumber the signal number.
65 * @return the signal name.
66 */
67 String strsignal(int sigNumber);
68
69 int kill(int pid, int signum);
70
71 int setsid();
72
73 int setuid(short newuid);
74
75 int setgid(short newgid);
76
77 int umask(int mask);
78
79 /**
80 * Get current process id.
81 * https://www.systutorials.com/docs/linux/man/2-getppid/
82 * @return current process id.
83 */
84 int getpid();
85
86 /**
87 * get parent process id.
88 * https://www.systutorials.com/docs/linux/man/2-getppid/
89 * @return parent process id.
90 */
91 int getppid();
92
93 int chdir(String dir);
94
95 int execv(String file, StringArray args);
96
97 int execvp(String file, StringArray args);
98
99 int setenv(String name, String value);
100
101 void perror(String msg);
102
103 String strerror(int errno);
104
105 // this is listed in http://developer.apple.com/DOCUMENTATION/Darwin/Reference/ManPages/man3/sysctlbyname.3.html
106 // but not in http://www.gnu.org/software/libc/manual/html_node/System-Parameters.html#index-sysctl-3493
107 // perhaps it is only supported on BSD?
108 int sysctlbyname(String name, Pointer oldp, IntByReference oldlenp, Pointer newp, IntByReference newlen);
109
110 int sysctl(int[] mib, int nameLen, Pointer oldp, IntByReference oldlenp, Pointer newp, IntByReference newlen);
111
112 int sysctlnametomib(String name, Pointer mibp, IntByReference size);
113
114 class FILE extends PointerType {
115
116 public FILE() {
117 }
118
119 public FILE(final Pointer pointer) {
120 super(pointer);
121 }
122 }
123
124 FILE fopen(String fileName, String mode);
125
126 //FILE *freopen(const char *filename, const char *mode, FILE *stream)
127 FILE freopen(String fileName, String mode, FILE stream);
128
129 int fseek(FILE file, long offset, int whence);
130
131 long ftell(FILE file);
132
133 int fread(Pointer buf, int size, int count, FILE file);
134
135 int fclose(FILE file);
136
137 int getdtablesize();
138
139 int fcntl(int fd, int command);
140
141 int fcntl(int fd, int command, int flags);
142
143 /**
144 * Read a symlink. The name will be copied into the specified memory, and returns the number of bytes copied. The
145 * string is not null-terminated.
146 *
147 * @return if the return value equals size, the caller needs to retry with a bigger buffer. If -1, error.
148 */
149 int readlink(String filename, Memory buffer, NativeLong size);
150
151 }