2
#if defined(__MWERKS__)//dima
3
#include "CommPortIdentifier.h"
5
#include "gnu/io/CommPortIdentifier.h"
8
/* Yikes... how do we do this? */
9
jstring gnu::io::CommPortIdentifier::native_psmisc_report_owner (jstring arg)
11
return ( JvNewStringUTF("Unknown Application") );
12
//return("Unknown Application\n");
14
/* just kille this for now... trying to get gcj working */
17
/* loosly based on fuser.c by Werner Almesberger. */
19
/* Copyright 1993-1998 Werner Almesberger. See file COPYING for details.
20
psmisc (fuser, killall and pstree) program code, documentation and
21
auxiliary programs are
22
Copyright 1993-1998 Werner Almesberger.
25
Redistribution and use in source and binary forms of parts of or the
26
whole original or derived work are permitted provided that the
27
original work is properly attributed to the author. The name of the
28
author may not be used to endorse or promote products derived from
29
this software without specific prior written permission. This work
30
is provided "as is" and without any express or implied warranties.
38
#include <linux/major.h>
40
#include <sys/types.h>
44
#define PROC_BASE "/proc"
45
#define UID_UNKNOWN -1
46
#define NAME_FIELD 20 /* space reserved for file name */
47
#define REF_FILE 1 /* an open file */
48
#define REF_ROOT 2 /* current root */
49
#define REF_CWD 4 /* current directory */
50
#define REF_EXE 8 /* executable */
51
#define REF_MMAP 16 /* mmap'ed file or library */
52
#define FLAG_UID 2 /* show uid */
53
#define FLAG_VERB 4 /* show verbose output */
54
#define FLAG_DEV 8 /* show all processes using this device */
57
char returnstring[256];
58
void parse_args(const char *);
63
typedef enum { it_proc,it_mount,it_loop,it_swap } ITEM_TYPE;
65
typedef struct item_dsc {
70
int uid; /* must also accept UID_UNKNOWN */
77
struct item_dsc *next;
80
typedef struct file_dsc {
81
const char *name; /* NULL if previous entry has name */
85
SPACE_DSC *name_space; /* or NULL if no indication */
87
struct file_dsc *named,*next;
90
static SPACE_DSC name_spaces[] = { };
91
static FILE_DSC *files = NULL;
92
static FILE_DSC *last_named = NULL;
93
static int all = 0,found_item = 0;
95
static void add_file(const char *path,unsigned long device,unsigned long inode, pid_t pid,int ref)
99
ITEM_DSC **item,*here;
100
unsigned long mount_dev = 0;
102
if (device) mount_dev = device;
103
for (file = files; file; file = next) {
105
if (file->flags & FLAG_DEV ? mount_dev && mount_dev == file->dev :
106
device == file->dev && inode == file->ino) {
107
if (!file->name) file = file->named;
108
for (item = &file->items; *item; item = &(*item)->next)
109
if ((*item)->type == it_proc && (*item)->u.proc.pid >= pid)
111
if (*item && (*item)->u.proc.pid == pid) here = *item;
113
if (!(here = malloc(sizeof(ITEM_DSC)))) {
117
here->type = it_proc;
118
here->u.proc.pid = pid;
119
here->u.proc.uid = UID_UNKNOWN;
120
here->u.proc.ref_set = 0;
125
here->u.proc.ref_set |= ref;
126
if ((file->flags & (FLAG_UID | FLAG_VERB)) && here->u.proc.uid == UID_UNKNOWN && lstat(path,&st) >= 0)
127
here->u.proc.uid = st.st_uid;
132
static void check_dir(const char *rel,pid_t pid,int type)
136
char path[PATH_MAX+1];
139
if (!(dir = opendir(rel))) return;
140
while ( (de = readdir(dir) ) )
141
if (strcmp(de->d_name,".") && strcmp(de->d_name,"..")) {
142
sprintf(path,"%s/%s",rel,de->d_name);
143
if (stat(path,&st) >= 0)
144
add_file(path,st.st_dev,st.st_ino,pid,type);
146
(void) closedir(dir);
150
extern void scan_fd(void)
154
char path[PATH_MAX+1];
158
if (!(dir = opendir(PROC_BASE))) {
163
while ( ( de = readdir(dir) ) )
164
if ( ( pid = atoi(de->d_name) ) ) {
166
sprintf(path,"%s/%d",PROC_BASE,pid);
167
if (chdir(path) >= 0) {
168
check_dir("fd",pid,REF_FILE);
171
(void) closedir(dir);
173
fprintf(stderr,PROC_BASE " is empty (not mounted ?)\n");
178
extern void show_user(const char tstring[],char *rs)
180
const ITEM_DSC *item;
182
const struct passwd *pw;
183
const char *user,*scan;
184
char tmp[10],path[PATH_MAX+1],comm[COMM_LEN+1];
194
if (seteuid(getuid()) < 0) {
195
sprintf(rs, "%s", "Unknown Linux Application");
199
if (!files->name || !(files->items || all))
201
sprintf(rs, "%s", "Unknown Linux Application");
205
strcat(returnstring," ");
207
sprintf(path,PROC_BASE "/%d/stat",item->u.proc.pid);
209
if ( ( f = fopen(path,"r") ) ) {
210
(void) fscanf(f,"%d (%[^)]",&dummy,comm);
214
uid = item->u.proc.uid;
215
if (uid == UID_UNKNOWN) user = "???";
216
else if ( ( pw = getpwuid(uid) ) ) user = pw->pw_name;
218
sprintf(tmp,"%d",uid);
221
strcat(returnstring,user);
222
strcat(returnstring," PID = ");
223
sprintf(temp,"%6d ",item->u.proc.pid);
224
strcat(returnstring,temp);
225
strcat(returnstring,"Program = ");
228
for (scan = name; *scan; scan++)
232
sprintf(temp,"\\\\");
233
strcat(returnstring,temp);
235
else if (*scan > ' ' && *scan <= '~')
237
keeper=strlen(returnstring);
238
returnstring[keeper]= *scan;
239
returnstring[keeper+1]= '\0';
242
sprintf(temp,"\\%03o", (int) scan);
243
strcat(returnstring,temp);
247
strcpy(rs,returnstring);
249
static void enter_item(const char *name,int flags,int sig_number,dev_t dev, ino_t ino,SPACE_DSC *name_space)
251
static FILE_DSC *last = NULL;
254
if (!(new = malloc(sizeof(FILE_DSC)))) {
258
if (last_named && !strcmp(last_named->name,name) &&
259
last_named->name_space == name_space) new->name = NULL;
260
else if (!(new->name = strdup(name))) {
265
new->sig_num = sig_number;
270
new->name_space = name_space;
271
if (last) last->next = new;
274
new->named = last_named;
275
if (new->name) last_named = new;
277
void parse_args(const char *argv)
279
SPACE_DSC *name_space;
280
int flags,sig_number,no_files;
285
sig_number = SIGKILL;
286
name_space = name_spaces;
291
this_name_space = name_space;
292
if (this_name_space == name_spaces) {
293
if (stat(argv,&st) < 0) {
297
if (!S_ISSOCK(st.st_mode) || (flags & FLAG_DEV))
298
enter_item(argv,flags,sig_number,st.st_dev,st.st_ino,NULL);
303
jstring Java::gnu::io::CommPortIdentifier::native_1psmisc_1report_1owner (
307
char returnstring[256];
308
const char *filename = JvGetStringChars(arg);
309
show_user(str,returnstring);
310
return (*env)->NewStringUTF(env, returnstring);
312
#endif /* __linux__ */