1
#if defined(__MWERKS__)//dima
2
#include "CommPortIdentifier.h"
4
#include "gnu_io_CommPortIdentifier.h"
7
JNIEXPORT jstring JNICALL Java_gnu_io_CommPortIdentifier_native_1psmisc_1report_1owner (JNIEnv *env, jobject obj, jstring arg)
9
return (*env)->NewStringUTF(env, "Unknown Application");
10
//return("Unknown Application\n");
13
/* loosly based on fuser.c by Werner Almesberger. */
15
/* Copyright 1993-1998 Werner Almesberger. See file COPYING for details.
16
psmisc (fuser, killall and pstree) program code, documentation and
17
auxiliary programs are
18
Copyright 1993-1998 Werner Almesberger.
21
Redistribution and use in source and binary forms of parts of or the
22
whole original or derived work are permitted provided that the
23
original work is properly attributed to the author. The name of the
24
author may not be used to endorse or promote products derived from
25
this software without specific prior written permission. This work
26
is provided "as is" and without any express or implied warranties.
34
#include <linux/major.h>
36
#include <sys/types.h>
40
#define PROC_BASE "/proc"
41
#define UID_UNKNOWN -1
42
#define NAME_FIELD 20 /* space reserved for file name */
43
#define REF_FILE 1 /* an open file */
44
#define REF_ROOT 2 /* current root */
45
#define REF_CWD 4 /* current directory */
46
#define REF_EXE 8 /* executable */
47
#define REF_MMAP 16 /* mmap'ed file or library */
48
#define FLAG_UID 2 /* show uid */
49
#define FLAG_VERB 4 /* show verbose output */
50
#define FLAG_DEV 8 /* show all processes using this device */
53
char returnstring[256];
54
void parse_args(const char *);
59
typedef enum { it_proc,it_mount,it_loop,it_swap } ITEM_TYPE;
61
typedef struct item_dsc {
66
int uid; /* must also accept UID_UNKNOWN */
73
struct item_dsc *next;
76
typedef struct file_dsc {
77
const char *name; /* NULL if previous entry has name */
81
SPACE_DSC *name_space; /* or NULL if no indication */
83
struct file_dsc *named,*next;
86
static SPACE_DSC name_spaces[] = { };
87
static FILE_DSC *files = NULL;
88
static FILE_DSC *last_named = NULL;
89
static int all = 0,found_item = 0;
91
static void add_file(const char *path,unsigned long device,unsigned long inode, pid_t pid,int ref)
95
ITEM_DSC **item,*this;
96
unsigned long mount_dev = 0;
98
if (device) mount_dev = device;
99
for (file = files; file; file = next) {
101
if (file->flags & FLAG_DEV ? mount_dev && mount_dev == file->dev :
102
device == file->dev && inode == file->ino) {
103
if (!file->name) file = file->named;
104
for (item = &file->items; *item; item = &(*item)->next)
105
if ((*item)->type == it_proc && (*item)->u.proc.pid >= pid)
107
if (*item && (*item)->u.proc.pid == pid) this = *item;
109
if (!(this = malloc(sizeof(ITEM_DSC)))) {
113
this->type = it_proc;
114
this->u.proc.pid = pid;
115
this->u.proc.uid = UID_UNKNOWN;
116
this->u.proc.ref_set = 0;
121
this->u.proc.ref_set |= ref;
122
if ((file->flags & (FLAG_UID | FLAG_VERB)) && this->u.proc.uid == UID_UNKNOWN && lstat(path,&st) >= 0)
123
this->u.proc.uid = st.st_uid;
128
static void check_dir(const char *rel,pid_t pid,int type)
132
char path[PATH_MAX+1];
135
if (!(dir = opendir(rel))) return;
136
while ( (de = readdir(dir) ) )
137
if (strcmp(de->d_name,".") && strcmp(de->d_name,"..")) {
138
sprintf(path,"%s/%s",rel,de->d_name);
139
if (stat(path,&st) >= 0)
140
add_file(path,st.st_dev,st.st_ino,pid,type);
142
(void) closedir(dir);
146
extern void scan_fd(void)
150
char path[PATH_MAX+1];
154
if (!(dir = opendir(PROC_BASE))) {
159
while ( ( de = readdir(dir) ) )
160
if ( ( pid = atoi(de->d_name) ) ) {
162
sprintf(path,"%s/%d",PROC_BASE,pid);
163
if (chdir(path) >= 0) {
164
check_dir("fd",pid,REF_FILE);
167
(void) closedir(dir);
169
fprintf(stderr,PROC_BASE " is empty (not mounted ?)\n");
174
extern void show_user(const char tstring[],char *rs)
176
const ITEM_DSC *item;
178
const struct passwd *pw;
179
const char *user,*scan;
180
char tmp[10],path[PATH_MAX+1],comm[COMM_LEN+1];
190
if (seteuid(getuid()) < 0) {
191
sprintf(rs, "%s", "Unknown Linux Application");
195
if (!files->name || !(files->items || all))
197
sprintf(rs, "%s", "Unknown Linux Application");
201
strcat(returnstring," ");
203
sprintf(path,PROC_BASE "/%d/stat",item->u.proc.pid);
205
if ( ( f = fopen(path,"r") ) ) {
206
(void) fscanf(f,"%d (%[^)]",&dummy,comm);
210
uid = item->u.proc.uid;
211
if (uid == UID_UNKNOWN) user = "???";
212
else if ( ( pw = getpwuid(uid) ) ) user = pw->pw_name;
214
sprintf(tmp,"%d",uid);
217
strcat(returnstring,user);
218
strcat(returnstring," PID = ");
219
sprintf(temp,"%6d ",item->u.proc.pid);
220
strcat(returnstring,temp);
221
strcat(returnstring,"Program = ");
224
for (scan = name; *scan; scan++)
228
sprintf(temp,"\\\\");
229
strcat(returnstring,temp);
231
else if (*scan > ' ' && *scan <= '~')
233
keeper=strlen(returnstring);
234
returnstring[keeper]= *scan;
235
returnstring[keeper+1]= '\0';
238
sprintf(temp,"\\%03o", (int) scan);
239
strcat(returnstring,temp);
243
strcpy(rs,returnstring);
245
static void enter_item(const char *name,int flags,int sig_number,dev_t dev, ino_t ino,SPACE_DSC *name_space)
247
static FILE_DSC *last = NULL;
250
if (!(new = malloc(sizeof(FILE_DSC)))) {
254
if (last_named && !strcmp(last_named->name,name) &&
255
last_named->name_space == name_space) new->name = NULL;
256
else if (!(new->name = strdup(name))) {
261
new->sig_num = sig_number;
266
new->name_space = name_space;
267
if (last) last->next = new;
270
new->named = last_named;
271
if (new->name) last_named = new;
273
void parse_args(const char *argv)
275
SPACE_DSC *name_space;
276
int flags,sig_number,no_files;
277
SPACE_DSC *this_name_space;
281
sig_number = SIGKILL;
282
name_space = name_spaces;
287
this_name_space = name_space;
288
if (this_name_space == name_spaces) {
289
if (stat(argv,&st) < 0) {
293
if (!S_ISSOCK(st.st_mode) || (flags & FLAG_DEV))
294
enter_item(argv,flags,sig_number,st.st_dev,st.st_ino,NULL);
299
JNIEXPORT jstring JNICALL Java_gnu_io_CommPortIdentifier_native_1psmisc_1report_1owner (JNIEnv *env, jobject obj, jstring arg)
301
char returnstring[256];
302
const char *str = (*env)->GetStringUTFChars(env, arg, 0);
303
show_user(str,returnstring);
304
(*env)->ReleaseStringUTFChars(env, arg, str);
305
return (*env)->NewStringUTF(env, returnstring);
307
#endif /* __linux__ */