0
# Unix/Linux Platform APIs
1
2
POSIX-compliant system calls and Linux-specific functionality including X11 graphics, device management, file system operations, and platform-specific utilities for AIX and Solaris systems.
3
4
## Capabilities
5
6
### Core Unix System Calls (LibC)
7
8
Standard C library functions providing essential POSIX system calls for file operations, process management, and system interaction.
9
10
```java { .api }
11
/**
12
* Standard C library functions (POSIX)
13
*/
14
public interface LibC extends Library {
15
LibC INSTANCE = Native.load("c", LibC.class);
16
17
/**
18
* Open file or device
19
* @param path File path to open
20
* @param flags Open flags (O_RDONLY, O_WRONLY, O_RDWR, etc.)
21
* @return File descriptor, or -1 if failed
22
*/
23
int open(String path, int flags);
24
25
/**
26
* Open file with mode specification
27
* @param path File path to open
28
* @param flags Open flags
29
* @param mode File permission mode (for O_CREAT)
30
* @return File descriptor, or -1 if failed
31
*/
32
int open(String path, int flags, int mode);
33
34
/**
35
* Close file descriptor
36
* @param fd File descriptor to close
37
* @return 0 if successful, -1 if failed
38
*/
39
int close(int fd);
40
41
/**
42
* Read from file descriptor
43
* @param fd File descriptor to read from
44
* @param buf Buffer to read into
45
* @param count Maximum bytes to read
46
* @return Number of bytes read, or -1 if failed
47
*/
48
NativeLong read(int fd, Pointer buf, NativeLong count);
49
50
/**
51
* Write to file descriptor
52
* @param fd File descriptor to write to
53
* @param buf Buffer containing data to write
54
* @param count Number of bytes to write
55
* @return Number of bytes written, or -1 if failed
56
*/
57
NativeLong write(int fd, Pointer buf, NativeLong count);
58
59
/**
60
* Change file permissions
61
* @param path File path
62
* @param mode New permission mode
63
* @return 0 if successful, -1 if failed
64
*/
65
int chmod(String path, int mode);
66
67
/**
68
* Change file ownership
69
* @param path File path
70
* @param owner New owner user ID
71
* @param group New group ID
72
* @return 0 if successful, -1 if failed
73
*/
74
int chown(String path, int owner, int group);
75
76
/**
77
* Get process ID
78
* @return Current process ID
79
*/
80
int getpid();
81
82
/**
83
* Get parent process ID
84
* @return Parent process ID
85
*/
86
int getppid();
87
88
/**
89
* Get user ID
90
* @return Current user ID
91
*/
92
int getuid();
93
94
/**
95
* Get effective user ID
96
* @return Effective user ID
97
*/
98
int geteuid();
99
}
100
```
101
102
**Usage Examples:**
103
104
```java
105
import com.sun.jna.platform.unix.LibC;
106
import com.sun.jna.Memory;
107
import com.sun.jna.NativeLong;
108
109
// Open a file for reading
110
int fd = LibC.INSTANCE.open("/etc/passwd", LibC.O_RDONLY);
111
if (fd != -1) {
112
// Read from file
113
Memory buffer = new Memory(1024);
114
NativeLong bytesRead = LibC.INSTANCE.read(fd, buffer, new NativeLong(1024));
115
116
if (bytesRead.longValue() > 0) {
117
String content = buffer.getString(0);
118
System.out.println("Read " + bytesRead.longValue() + " bytes:");
119
System.out.println(content);
120
}
121
122
// Close file
123
LibC.INSTANCE.close(fd);
124
} else {
125
System.err.println("Failed to open file");
126
}
127
128
// Get process information
129
System.out.println("PID: " + LibC.INSTANCE.getpid());
130
System.out.println("Parent PID: " + LibC.INSTANCE.getppid());
131
System.out.println("UID: " + LibC.INSTANCE.getuid());
132
```
133
134
### X Window System (X11)
135
136
X11 protocol bindings for graphics operations, window management, and event handling in Unix/Linux desktop environments.
137
138
```java { .api }
139
/**
140
* X Window System functions
141
*/
142
public interface X11 extends Library {
143
X11 INSTANCE = Native.load("X11", X11.class);
144
145
/**
146
* Open connection to X server
147
* @param display_name Display name (can be null for default)
148
* @return Display pointer, or null if failed
149
*/
150
Display XOpenDisplay(String display_name);
151
152
/**
153
* Close connection to X server
154
* @param display Display to close
155
* @return 0 if successful
156
*/
157
int XCloseDisplay(Display display);
158
159
/**
160
* Create simple window
161
* @param display X display
162
* @param parent Parent window
163
* @param x X coordinate
164
* @param y Y coordinate
165
* @param width Window width
166
* @param height Window height
167
* @param border_width Border width
168
* @param border Border color
169
* @param background Background color
170
* @return Window ID
171
*/
172
Window XCreateSimpleWindow(Display display, Window parent, int x, int y,
173
int width, int height, int border_width,
174
NativeLong border, NativeLong background);
175
176
/**
177
* Map window (make visible)
178
* @param display X display
179
* @param window Window to map
180
* @return status code
181
*/
182
int XMapWindow(Display display, Window window);
183
184
/**
185
* Unmap window (hide)
186
* @param display X display
187
* @param window Window to unmap
188
* @return status code
189
*/
190
int XUnmapWindow(Display display, Window window);
191
192
/**
193
* Destroy window
194
* @param display X display
195
* @param window Window to destroy
196
* @return status code
197
*/
198
int XDestroyWindow(Display display, Window window);
199
200
/**
201
* Get next event from queue
202
* @param display X display
203
* @param event_return Event structure to fill
204
* @return status code
205
*/
206
int XNextEvent(Display display, XEvent event_return);
207
208
/**
209
* Check if events are pending
210
* @param display X display
211
* @return number of pending events
212
*/
213
int XPending(Display display);
214
}
215
216
/**
217
* X11 Display structure
218
*/
219
public static class Display extends PointerType {
220
public Display() { }
221
public Display(Pointer p) { super(p); }
222
}
223
224
/**
225
* X11 Window ID
226
*/
227
public static class Window extends NativeLong {
228
public Window() { }
229
public Window(long value) { super(value); }
230
}
231
232
/**
233
* X11 Event structure
234
*/
235
public static class XEvent extends Union {
236
public int type;
237
public XKeyEvent xkey;
238
public XButtonEvent xbutton;
239
public XMotionEvent xmotion;
240
// ... other event types
241
}
242
```
243
244
### Linux-Specific APIs
245
246
Linux-specific system functions and interfaces not available on other Unix systems.
247
248
#### Device Management (Udev)
249
250
```java { .api }
251
/**
252
* Linux udev device management
253
*/
254
public interface Udev extends Library {
255
Udev INSTANCE = Native.load("udev", Udev.class);
256
257
/**
258
* Create udev context
259
* @return udev context, or null if failed
260
*/
261
UdevContext udev_new();
262
263
/**
264
* Free udev context
265
* @param udev udev context to free
266
*/
267
void udev_unref(UdevContext udev);
268
269
/**
270
* Create device enumerator
271
* @param udev udev context
272
* @return device enumerator, or null if failed
273
*/
274
UdevEnumerate udev_enumerate_new(UdevContext udev);
275
276
/**
277
* Scan devices matching current filter
278
* @param enumerate device enumerator
279
* @return 0 if successful
280
*/
281
int udev_enumerate_scan_devices(UdevEnumerate enumerate);
282
283
/**
284
* Get list of device paths
285
* @param enumerate device enumerator
286
* @return linked list of device entries
287
*/
288
UdevListEntry udev_enumerate_get_list_entry(UdevEnumerate enumerate);
289
}
290
```
291
292
#### Extended File Attributes (XAttr)
293
294
```java { .api }
295
/**
296
* Linux extended file attributes
297
*/
298
public interface XAttr extends Library {
299
XAttr INSTANCE = Native.load("c", XAttr.class);
300
301
/**
302
* Set extended attribute
303
* @param path File path
304
* @param name Attribute name
305
* @param value Attribute value
306
* @param size Value size
307
* @param flags Operation flags
308
* @return 0 if successful, -1 if failed
309
*/
310
int setxattr(String path, String name, Pointer value, NativeLong size, int flags);
311
312
/**
313
* Get extended attribute
314
* @param path File path
315
* @param name Attribute name
316
* @param value Buffer for attribute value
317
* @param size Buffer size
318
* @return Size of attribute value, or -1 if failed
319
*/
320
NativeLong getxattr(String path, String name, Pointer value, NativeLong size);
321
322
/**
323
* Remove extended attribute
324
* @param path File path
325
* @param name Attribute name to remove
326
* @return 0 if successful, -1 if failed
327
*/
328
int removexattr(String path, String name);
329
330
/**
331
* List extended attributes
332
* @param path File path
333
* @param list Buffer for attribute names
334
* @param size Buffer size
335
* @return Size of attribute list, or -1 if failed
336
*/
337
NativeLong listxattr(String path, Pointer list, NativeLong size);
338
}
339
340
/**
341
* Extended attribute utilities
342
*/
343
public class XAttrUtil {
344
/**
345
* Set string extended attribute
346
* @param path File path
347
* @param name Attribute name
348
* @param value String value to set
349
* @throws IOException if operation fails
350
*/
351
public static void setXAttr(String path, String name, String value) throws IOException;
352
353
/**
354
* Get string extended attribute
355
* @param path File path
356
* @param name Attribute name
357
* @return String value, or null if not found
358
* @throws IOException if operation fails
359
*/
360
public static String getXAttr(String path, String name) throws IOException;
361
362
/**
363
* List all extended attributes
364
* @param path File path
365
* @return Array of attribute names
366
* @throws IOException if operation fails
367
*/
368
public static String[] listXAttrs(String path) throws IOException;
369
}
370
```
371
372
#### Real-Time Extensions (LibRT)
373
374
```java { .api }
375
/**
376
* POSIX real-time extensions
377
*/
378
public interface LibRT extends Library {
379
LibRT INSTANCE = Native.load("rt", LibRT.class);
380
381
/**
382
* Create shared memory object
383
* @param name Shared memory name
384
* @param oflag Open flags
385
* @param mode Permission mode
386
* @return File descriptor, or -1 if failed
387
*/
388
int shm_open(String name, int oflag, int mode);
389
390
/**
391
* Remove shared memory object
392
* @param name Shared memory name to remove
393
* @return 0 if successful, -1 if failed
394
*/
395
int shm_unlink(String name);
396
397
/**
398
* Get current time with nanosecond precision
399
* @param clk_id Clock identifier
400
* @param tp Timespec structure to fill
401
* @return 0 if successful, -1 if failed
402
*/
403
int clock_gettime(int clk_id, TimeSpec tp);
404
405
/**
406
* Sleep for specified time with nanosecond precision
407
* @param req Requested sleep time
408
* @param rem Remaining time if interrupted
409
* @return 0 if successful, -1 if failed
410
*/
411
int nanosleep(TimeSpec req, TimeSpec rem);
412
}
413
414
/**
415
* Time specification structure
416
*/
417
public static class TimeSpec extends Structure {
418
public NativeLong tv_sec; // seconds
419
public NativeLong tv_nsec; // nanoseconds
420
421
@Override
422
protected List<String> getFieldOrder() {
423
return Arrays.asList("tv_sec", "tv_nsec");
424
}
425
}
426
```
427
428
### Memory Mapping (Mman)
429
430
```java { .api }
431
/**
432
* Memory mapping operations
433
*/
434
public interface Mman extends Library {
435
Mman INSTANCE = Native.load("c", Mman.class);
436
437
// Memory protection flags
438
int PROT_READ = 0x1;
439
int PROT_WRITE = 0x2;
440
int PROT_EXEC = 0x4;
441
int PROT_NONE = 0x0;
442
443
// Memory mapping flags
444
int MAP_SHARED = 0x01;
445
int MAP_PRIVATE = 0x02;
446
int MAP_ANONYMOUS = 0x20;
447
448
/**
449
* Map memory
450
* @param addr Preferred address (can be null)
451
* @param len Length to map
452
* @param prot Protection flags
453
* @param flags Mapping flags
454
* @param fd File descriptor (-1 for anonymous)
455
* @param offset File offset
456
* @return Mapped address, or MAP_FAILED if failed
457
*/
458
Pointer mmap(Pointer addr, NativeLong len, int prot, int flags, int fd, NativeLong offset);
459
460
/**
461
* Unmap memory
462
* @param addr Address to unmap
463
* @param len Length to unmap
464
* @return 0 if successful, -1 if failed
465
*/
466
int munmap(Pointer addr, NativeLong len);
467
468
/**
469
* Change memory protection
470
* @param addr Address to change
471
* @param len Length to change
472
* @param prot New protection flags
473
* @return 0 if successful, -1 if failed
474
*/
475
int mprotect(Pointer addr, NativeLong len, int prot);
476
477
/**
478
* Synchronize memory mapping with file
479
* @param addr Address to sync
480
* @param len Length to sync
481
* @param flags Sync flags
482
* @return 0 if successful, -1 if failed
483
*/
484
int msync(Pointer addr, NativeLong len, int flags);
485
}
486
```
487
488
### Platform-Specific Extensions
489
490
#### AIX Platform Support
491
492
```java { .api }
493
/**
494
* AIX performance statistics
495
*/
496
public interface Perfstat extends Library {
497
Perfstat INSTANCE = Native.load("perfstat", Perfstat.class);
498
499
/**
500
* Get CPU statistics
501
* @param name CPU identifier (can be null for total)
502
* @param userbuff Buffer for CPU stats
503
* @param sizeof_userbuff Buffer size
504
* @param desired_number Number of entries requested
505
* @return Number of entries returned, or -1 if failed
506
*/
507
int perfstat_cpu_total(PerfstatId name, PerfstatCpuTotal userbuff,
508
int sizeof_userbuff, int desired_number);
509
510
/**
511
* Get memory statistics
512
* @param name Memory identifier (can be null for total)
513
* @param userbuff Buffer for memory stats
514
* @param sizeof_userbuff Buffer size
515
* @param desired_number Number of entries requested
516
* @return Number of entries returned, or -1 if failed
517
*/
518
int perfstat_memory_total(PerfstatId name, PerfstatMemoryTotal userbuff,
519
int sizeof_userbuff, int desired_number);
520
}
521
```
522
523
#### Solaris Platform Support
524
525
```java { .api }
526
/**
527
* Solaris kernel statistics
528
*/
529
public interface LibKstat extends Library {
530
LibKstat INSTANCE = Native.load("kstat", LibKstat.class);
531
532
/**
533
* Open kstat library
534
* @return kstat control structure, or null if failed
535
*/
536
KstatCtl kstat_open();
537
538
/**
539
* Close kstat library
540
* @param kc kstat control structure
541
* @return 0 if successful
542
*/
543
int kstat_close(KstatCtl kc);
544
545
/**
546
* Look up kstat by name
547
* @param kc kstat control structure
548
* @param module Module name (can be null)
549
* @param instance Instance number (-1 for any)
550
* @param name Statistic name (can be null)
551
* @return kstat structure, or null if not found
552
*/
553
Kstat kstat_lookup(KstatCtl kc, String module, int instance, String name);
554
555
/**
556
* Read kstat data
557
* @param kc kstat control structure
558
* @param ksp kstat structure
559
* @param buf Buffer to read into (can be null)
560
* @return 0 if successful, -1 if failed
561
*/
562
int kstat_read(KstatCtl kc, Kstat ksp, Pointer buf);
563
}
564
```
565
566
### High-Level Utilities
567
568
```java { .api }
569
/**
570
* High-level C library utilities
571
*/
572
public class LibCUtil {
573
/**
574
* Get last system error message
575
* @return Error message string
576
*/
577
public static String getLastErrorMessage();
578
579
/**
580
* Get current working directory
581
* @return Current directory path
582
* @throws IOException if operation fails
583
*/
584
public static String getCwd() throws IOException;
585
586
/**
587
* Change current working directory
588
* @param path New directory path
589
* @throws IOException if operation fails
590
*/
591
public static void chdir(String path) throws IOException;
592
593
/**
594
* Get environment variable
595
* @param name Variable name
596
* @return Variable value, or null if not found
597
*/
598
public static String getenv(String name);
599
600
/**
601
* Set environment variable
602
* @param name Variable name
603
* @param value Variable value
604
* @param overwrite Whether to overwrite existing value
605
* @throws IOException if operation fails
606
*/
607
public static void setenv(String name, String value, boolean overwrite) throws IOException;
608
}
609
```
610
611
**Usage Examples:**
612
613
```java
614
import com.sun.jna.platform.unix.X11;
615
import com.sun.jna.platform.unix.LibCUtil;
616
import com.sun.jna.platform.linux.XAttrUtil;
617
618
// X11 window creation
619
X11.Display display = X11.INSTANCE.XOpenDisplay(null);
620
if (display != null) {
621
X11.Window rootWindow = new X11.Window(1); // Root window is typically 1
622
623
X11.Window window = X11.INSTANCE.XCreateSimpleWindow(
624
display, rootWindow,
625
100, 100, // x, y
626
640, 480, // width, height
627
1, // border width
628
new NativeLong(0), // border color
629
new NativeLong(0) // background color
630
);
631
632
X11.INSTANCE.XMapWindow(display, window);
633
634
// Event loop would go here...
635
636
X11.INSTANCE.XDestroyWindow(display, window);
637
X11.INSTANCE.XCloseDisplay(display);
638
}
639
640
// Extended attributes (Linux)
641
try {
642
XAttrUtil.setXAttr("/tmp/testfile", "user.comment", "My test file");
643
String comment = XAttrUtil.getXAttr("/tmp/testfile", "user.comment");
644
System.out.println("File comment: " + comment);
645
} catch (IOException e) {
646
System.err.println("Extended attribute operation failed: " + e.getMessage());
647
}
648
```
649
650
### Linux-Specific System Information
651
652
Advanced Linux system information gathering beyond basic POSIX functionality.
653
654
```java { .api }
655
/**
656
* Linux-specific LibC extensions
657
*/
658
public interface LibC extends com.sun.jna.platform.unix.LibC {
659
LibC INSTANCE = Native.load("c", LibC.class);
660
661
/**
662
* Get system information (Linux-specific)
663
* @param info Structure to fill with system information
664
* @return 0 if successful, -1 if failed
665
*/
666
int sysinfo(Sysinfo info);
667
668
/**
669
* Get file system statistics
670
* @param path Path to file system
671
* @param statvfs Structure to fill with file system information
672
* @return 0 if successful, -1 if failed
673
*/
674
int statvfs(String path, Statvfs statvfs);
675
}
676
677
/**
678
* System information structure (Linux)
679
*/
680
public static class Sysinfo extends Structure {
681
public NativeLong uptime; // Seconds since boot
682
public NativeLong[] loads = new NativeLong[3]; // 1, 5, and 15 minute load averages
683
public NativeLong totalram; // Total usable main memory size
684
public NativeLong freeram; // Available memory size
685
public NativeLong sharedram; // Amount of shared memory
686
public NativeLong bufferram; // Memory used by buffers
687
public NativeLong totalswap; // Total swap space size
688
public NativeLong freeswap; // Available swap space
689
public short procs; // Number of current processes
690
public NativeLong totalhigh; // Total high memory size
691
public NativeLong freehigh; // Available high memory size
692
public int mem_unit; // Memory unit size in bytes
693
}
694
695
/**
696
* File system statistics structure
697
*/
698
public static class Statvfs extends Structure {
699
public NativeLong f_bsize; // File system block size
700
public NativeLong f_frsize; // Fragment size
701
public NativeLong f_blocks; // Size of file system in f_frsize units
702
public NativeLong f_bfree; // Number of free blocks
703
public NativeLong f_bavail; // Number of free blocks for unprivileged users
704
public NativeLong f_files; // Number of inodes
705
public NativeLong f_ffree; // Number of free inodes
706
public NativeLong f_favail; // Number of free inodes for unprivileged users
707
public NativeLong f_fsid; // File system ID
708
public NativeLong f_flag; // Mount flags
709
public NativeLong f_namemax; // Maximum filename length
710
}
711
```
712
713
### Linux Error Codes
714
715
Comprehensive error code definitions for Linux system calls.
716
717
```java { .api }
718
/**
719
* Linux errno constants
720
*/
721
public interface ErrNo {
722
int EPERM = 1; // Operation not permitted
723
int ENOENT = 2; // No such file or directory
724
int ESRCH = 3; // No such process
725
int EINTR = 4; // Interrupted system call
726
int EIO = 5; // Input/output error
727
int ENXIO = 6; // No such device or address
728
int E2BIG = 7; // Argument list too long
729
int ENOEXEC = 8; // Exec format error
730
int EBADF = 9; // Bad file descriptor
731
int ECHILD = 10; // No child processes
732
int EAGAIN = 11; // Resource temporarily unavailable
733
int ENOMEM = 12; // Cannot allocate memory
734
int EACCES = 13; // Permission denied
735
int EFAULT = 14; // Bad address
736
int ENOTBLK = 15; // Block device required
737
int EBUSY = 16; // Device or resource busy
738
int EEXIST = 17; // File exists
739
int EXDEV = 18; // Invalid cross-device link
740
int ENODEV = 19; // No such device
741
int ENOTDIR = 20; // Not a directory
742
int EISDIR = 21; // Is a directory
743
int EINVAL = 22; // Invalid argument
744
int ENFILE = 23; // Too many open files in system
745
int EMFILE = 24; // Too many open files
746
int ENOTTY = 25; // Inappropriate ioctl for device
747
int ETXTBSY = 26; // Text file busy
748
int EFBIG = 27; // File too large
749
int ENOSPC = 28; // No space left on device
750
int ESPIPE = 29; // Illegal seek
751
int EROFS = 30; // Read-only file system
752
int EMLINK = 31; // Too many links
753
int EPIPE = 32; // Broken pipe
754
int EDOM = 33; // Numerical argument out of domain
755
int ERANGE = 34; // Numerical result out of range
756
// ... many more error codes available
757
}
758
```
759
760
### Resource Management (Unix/Linux)
761
762
System resource limit management and control.
763
764
```java { .api }
765
/**
766
* Resource limit management
767
*/
768
public interface Resource extends Library {
769
Resource INSTANCE = Native.load("c", Resource.class);
770
771
// Resource limit constants
772
int RLIMIT_CPU = 0; // CPU time in seconds
773
int RLIMIT_FSIZE = 1; // Maximum file size
774
int RLIMIT_DATA = 2; // Data segment size
775
int RLIMIT_STACK = 3; // Stack size
776
int RLIMIT_CORE = 4; // Core file size
777
int RLIMIT_RSS = 5; // Resident set size
778
int RLIMIT_NPROC = 6; // Number of processes
779
int RLIMIT_NOFILE = 7; // Number of open files
780
int RLIMIT_MEMLOCK = 8; // Locked memory
781
int RLIMIT_AS = 9; // Address space size
782
783
/**
784
* Get resource limits
785
* @param resource Resource type (RLIMIT_*)
786
* @param rlim Structure to fill with current limits
787
* @return 0 if successful, -1 if failed
788
*/
789
int getrlimit(int resource, Rlimit rlim);
790
791
/**
792
* Set resource limits
793
* @param resource Resource type (RLIMIT_*)
794
* @param rlim New resource limits
795
* @return 0 if successful, -1 if failed
796
*/
797
int setrlimit(int resource, Rlimit rlim);
798
}
799
800
/**
801
* Resource limit structure
802
*/
803
public static class Rlimit extends Structure {
804
public NativeLong rlim_cur; // Current (soft) limit
805
public NativeLong rlim_max; // Maximum (hard) limit
806
}
807
```
808
809
### BSD Platform APIs
810
811
BSD-specific functionality including extended attributes support.
812
813
```java { .api }
814
/**
815
* BSD extended attributes interface
816
*/
817
public interface ExtAttr extends Library {
818
ExtAttr INSTANCE = Native.load("c", ExtAttr.class);
819
820
// Namespace constants
821
int EXTATTR_NAMESPACE_USER = 1;
822
int EXTATTR_NAMESPACE_SYSTEM = 2;
823
824
/**
825
* Get extended attribute value
826
* @param path File path
827
* @param attrnamespace Attribute namespace
828
* @param attrname Attribute name
829
* @param data Buffer to store attribute value
830
* @param nbytes Buffer size
831
* @return Number of bytes retrieved, or -1 if failed
832
*/
833
NativeLong extattr_get_file(String path, int attrnamespace, String attrname,
834
Pointer data, NativeLong nbytes);
835
836
/**
837
* Set extended attribute value
838
* @param path File path
839
* @param attrnamespace Attribute namespace
840
* @param attrname Attribute name
841
* @param data Attribute value data
842
* @param nbytes Size of attribute value
843
* @return Number of bytes stored, or -1 if failed
844
*/
845
NativeLong extattr_set_file(String path, int attrnamespace, String attrname,
846
Pointer data, NativeLong nbytes);
847
848
/**
849
* Delete extended attribute
850
* @param path File path
851
* @param attrnamespace Attribute namespace
852
* @param attrname Attribute name
853
* @return 0 if successful, -1 if failed
854
*/
855
int extattr_delete_file(String path, int attrnamespace, String attrname);
856
857
/**
858
* List extended attributes
859
* @param path File path
860
* @param attrnamespace Attribute namespace
861
* @param data Buffer to store attribute list
862
* @param nbytes Buffer size
863
* @return Number of bytes retrieved, or -1 if failed
864
*/
865
NativeLong extattr_list_file(String path, int attrnamespace,
866
Pointer data, NativeLong nbytes);
867
}
868
869
/**
870
* High-level BSD extended attributes utilities
871
*/
872
public class ExtAttrUtil {
873
/**
874
* List all extended attributes for a file
875
* @param path File path
876
* @param namespace Attribute namespace
877
* @return List of attribute names
878
* @throws IOException if operation fails
879
*/
880
public static List<String> list(String path, int namespace) throws IOException;
881
882
/**
883
* Get extended attribute value as string
884
* @param path File path
885
* @param namespace Attribute namespace
886
* @param name Attribute name
887
* @return Attribute value, or null if not found
888
* @throws IOException if operation fails
889
*/
890
public static String get(String path, int namespace, String name) throws IOException;
891
892
/**
893
* Set extended attribute value from string
894
* @param path File path
895
* @param namespace Attribute namespace
896
* @param name Attribute name
897
* @param value Attribute value
898
* @throws IOException if operation fails
899
*/
900
public static void set(String path, int namespace, String name, String value) throws IOException;
901
902
/**
903
* Delete extended attribute
904
* @param path File path
905
* @param namespace Attribute namespace
906
* @param name Attribute name
907
* @throws IOException if operation fails
908
*/
909
public static void delete(String path, int namespace, String name) throws IOException;
910
}
911
```
912
913
### System Control (Unix/Linux)
914
915
System reboot and shutdown control functionality.
916
917
```java { .api }
918
/**
919
* System control interface for reboot/shutdown operations
920
*/
921
public interface Reboot extends Library {
922
Reboot INSTANCE = Native.load("c", Reboot.class);
923
924
// Reboot command constants
925
int RB_AUTOBOOT = 0x01234567; // Restart system
926
int RB_HALT_SYSTEM = 0xcdef0123; // Halt system
927
int RB_ENABLE_CAD = 0x89abcdef; // Enable Ctrl-Alt-Del
928
int RB_DISABLE_CAD = 0x00000000; // Disable Ctrl-Alt-Del
929
int RB_POWER_OFF = 0x4321fedc; // Power off system
930
int RB_SW_SUSPEND = 0xd000fce2; // Suspend system
931
int RB_KEXEC = 0x45584543; // Restart with kexec
932
933
/**
934
* Reboot or control system
935
* @param cmd Reboot command (RB_*)
936
* @return 0 if successful, -1 if failed
937
*/
938
int reboot(int cmd);
939
}
940
```
941
942
## Platform-Specific Extensions
943
944
### AIX Performance Statistics (Enhanced)
945
946
Beyond the basic examples, AIX Perfstat provides comprehensive system statistics including CPU, memory, disk, network, and partition information.
947
948
### Solaris Kstat2 (Modern API)
949
950
Solaris 11.4+ includes the modern Kstat2 API for system statistics, providing improved performance and functionality over the legacy Kstat interface.
951
952
## Advanced Usage Examples
953
954
```java
955
// Linux system information
956
LibC.Sysinfo sysinfo = new LibC.Sysinfo();
957
if (LibC.INSTANCE.sysinfo(sysinfo) == 0) {
958
System.out.println("Uptime: " + sysinfo.uptime + " seconds");
959
System.out.println("Total RAM: " + sysinfo.totalram.longValue() * sysinfo.mem_unit + " bytes");
960
System.out.println("Free RAM: " + sysinfo.freeram.longValue() * sysinfo.mem_unit + " bytes");
961
}
962
963
// BSD extended attributes
964
try {
965
ExtAttrUtil.set("/tmp/testfile", ExtAttr.EXTATTR_NAMESPACE_USER, "comment", "My test file");
966
String comment = ExtAttrUtil.get("/tmp/testfile", ExtAttr.EXTATTR_NAMESPACE_USER, "comment");
967
System.out.println("File comment: " + comment);
968
} catch (IOException e) {
969
System.err.println("Extended attribute operation failed: " + e.getMessage());
970
}
971
972
// Resource limits
973
Resource.Rlimit limits = new Resource.Rlimit();
974
if (Resource.INSTANCE.getrlimit(Resource.RLIMIT_NOFILE, limits) == 0) {
975
System.out.println("Current file descriptor limit: " + limits.rlim_cur);
976
System.out.println("Maximum file descriptor limit: " + limits.rlim_max);
977
}
978
```