105
("Usage: fuser [ -fuv ] [ -a | -s ] [ -4 | -6 ] [ -c | -m | -n SPACE ] [ -k [ -i ] [ -SIGNAL ]] NAME...\n"
105
("Usage: fuser [-fMuv] [-a|-s] [-4|-6] [-c|-m|-n SPACE] [-k [-i] [-SIGNAL]] NAME...\n"
106
106
" fuser -l\n" " fuser -V\n"
107
107
"Show which processes use the named files, sockets, or filesystems.\n\n"
108
" -a display unused files too\n"
109
" -c Same as -m (for POSIX compatibility)\n"
110
" -f silently ignored (for POSIX compatibility)\n"
111
" -i ask before killing (ignored without -k)\n"
112
" -k kill processes accessing the named file\n"
113
" -l list available signal names\n"
114
" -m show all processes using the named filesystems or block device\n"
115
" -n SPACE search in this name space (file, udp, or tcp)\n"
116
" -s silent operation\n"
117
" -SIGNAL send this signal instead of SIGKILL\n"
118
" -u display user IDs\n"
119
" -v verbose output\n"
120
" -V display version information\n"));
108
" -a,--all display unused files too\n"
109
" -i,--interactive ask before killing (ignored without -k)\n"
110
" -k,--kill kill processes accessing the named file\n"
111
" -l,--list-signals list available signal names\n"
112
" -m,--mount show all processes using the named filesystems or block device\n"
113
" -M,--ismountpoint fulfill request only if NAME is a mount point\n"
114
" -n,--namespace SPACE search in this name space (file, udp, or tcp)\n"
115
" -s,--silent silent operation\n"
116
" -SIGNAL send this signal instead of SIGKILL\n"
117
" -u,--user display user IDs\n"
118
" -v,--verbose verbose output\n"
119
" -V,--version display version information\n"));
122
fprintf(stderr, _(" -4 search IPv4 sockets only\n"
123
" -6 search IPv6 sockets only\n"));
122
" -4,--ipv4 search IPv4 sockets only\n"
123
" -6,--ipv6 search IPv6 sockets only\n"));
125
fprintf(stderr, _(" - reset options\n\n"
126
" - reset options\n\n"
126
127
" udp/tcp names: [local_port][,[rmt_host][,[rmt_port]]]\n\n"));
393
394
int parse_file(struct names *this_name, struct inode_list **ino_list)
397
if (stat(this_name->filename, &st) != 0) {
398
fprintf(stderr, _("Cannot stat %s: %s\n"), this_name->filename,
396
if (stat(this_name->filename, &(this_name->st)) != 0) {
398
fprintf(stderr, _("Specified filename %s does not exist.\n"), this_name->filename);
400
fprintf(stderr, _("Cannot stat %s: %s\n"), this_name->filename, strerror(errno));
403
404
printf("adding file %s %lX %lX\n", this_name->filename,
404
(unsigned long)st.st_dev, (unsigned long)st.st_ino);
405
(unsigned long)this_name->st.st_dev, (unsigned long)this_name->st.st_ino);
405
406
#endif /* DEBUG */
406
add_inode(ino_list, this_name, st.st_dev, st.st_ino);
407
add_inode(ino_list, this_name, this_name->st.st_dev, this_name->st.st_ino);
412
413
struct unixsocket_list *sun_head)
414
415
struct unixsocket_list *sun_tmp;
418
if (stat(this_name->filename, &st) != 0) {
419
fprintf(stderr, _("Cannot stat %s: %s\n"), this_name->filename,
423
418
net_dev = find_net_dev();
425
420
for (sun_tmp = sun_head; sun_tmp != NULL; sun_tmp = sun_tmp->next) {
426
if (sun_tmp->dev == st.st_dev && sun_tmp->inode == st.st_ino) {
421
if (sun_tmp->dev == this_name->st.st_dev && sun_tmp->inode == this_name->st.st_ino) {
427
422
add_inode(ino_list, this_name, net_dev,
428
423
sun_tmp->net_inode);
436
431
parse_mounts(struct names *this_name, struct device_list **dev_list,
440
434
dev_t match_device;
442
if (stat(this_name->filename, &st) != 0) {
443
fprintf(stderr, _("Cannot stat %s: %s\n"), this_name->filename,
447
if (S_ISBLK(st.st_mode))
448
match_device = st.st_rdev;
436
if (S_ISBLK(this_name->st.st_mode))
437
match_device = this_name->st.st_rdev;
450
match_device = st.st_dev;
439
match_device = this_name->st.st_dev;
451
440
add_device(dev_list, this_name, match_device);
760
read_proc_mounts(struct mount_list **mnt_list)
766
struct mount_list *mnt_tmp;
768
if ((fp = fopen(PROC_MOUNTS, "r")) == NULL) {
769
fprintf(stderr, "Cannot open %s\n", PROC_MOUNTS);
772
while (fgets(line, BUFSIZ, fp) != NULL) {
773
if ((find_mountp = strchr(line, ' ')) == NULL)
776
if ((find_space = strchr(find_mountp, ' ')) == NULL)
779
if ((mnt_tmp = malloc(sizeof(struct mount_list))) == NULL)
781
if ((mnt_tmp->mountpoint = strdup(find_mountp)) == NULL)
783
mnt_tmp->next = *mnt_list;
790
is_mountpoint(struct mount_list **mnt_list, char *arg)
793
struct mount_list *mnt_tmp;
797
/* Remove trailing slashes. */
798
for (p = arg; *p != '\0'; p++)
800
while (*(--p) == '/' && p > arg)
803
for (mnt_tmp = *mnt_list; mnt_tmp != NULL; mnt_tmp = mnt_tmp->next)
804
if (!strcmp(mnt_tmp->mountpoint, arg))
770
809
int main(int argc, char *argv[])
798
840
{"interactive", 0, NULL, 'i'},
799
841
{"list-signals", 0, NULL, 'l'},
800
842
{"mount", 0, NULL, 'm'},
843
{"ismountpoint", 0, NULL, 'M'},
801
844
{"namespace", 1, NULL, 'n'},
802
845
{"silent", 0, NULL, 's'},
803
846
{"user", 0, NULL, 'u'},
829
870
netdev = find_net_dev();
830
871
fill_unix_cache(&unixsockets);
836
getopt_long(argc, argv, "46acfhikmn:sS:uvV", options,NULL)
838
getopt_long(argc, argv, "acfhikmn:sS:uvV", options,NULL)
841
if (ignore_options > 0) {
873
for (argc_cnt = 1; argc_cnt < argc; argc_cnt++) {
874
current_argv = argv[argc_cnt];
875
if (current_argv[0] == '-') { /* its an option */
876
if (current_argv[1] == '-') { /* its a long option */
877
if (current_argv[2] == '\0') /* -- */
879
/* Parse the long options */
881
for (optr = options; optr->name != NULL; optr++) {
882
if (strcmp(current_argv+2,optr->name) == 0) {
883
sprintf(option_buf, "-%c", (char)optr->val);
887
if (optr->name == NULL) {
888
fprintf(stderr, _("%s: Invalid option %s\n"), argv[0],
893
option = current_argv;
896
while (*(++option) != '\0' && !skip_argv) { /* skips over the - */
905
#endif /* WITH_IPV6 */
855
907
opts |= OPT_ALLFILES;
858
opts |= OPT_MOUNTPOINT;
867
919
opts |= OPT_INTERACTIVE;
870
922
opts |= OPT_KILL;
876
928
opts |= OPT_MOUNTS;
879
if (strcmp(optarg, "tcp") == 0)
931
opts |= OPT_ISMOUNTPOINT;
932
read_proc_mounts(&mounts);
936
if (argc_cnt >= argc) {
937
usage(_ ("Namespace option requires an argument."));
941
//while(option != '\0') option++;
942
if (strcmp(argv[argc_cnt], "tcp") == 0)
880
943
default_namespace = NAMESPACE_TCP;
881
else if (strcmp(optarg, "udp") == 0)
944
else if (strcmp(argv[argc_cnt], "udp") == 0)
882
945
default_namespace = NAMESPACE_UDP;
883
else if (strcmp(optarg, "file") == 0)
946
else if (strcmp(argv[argc_cnt], "file") == 0)
884
947
default_namespace = NAMESPACE_FILE;
886
949
usage(_("Invalid namespace name"));
889
952
opts |= OPT_SILENT;
894
if (asprintf(&signame, "S%s",optarg) > 0) {
895
sig_number = get_signal(signame,argv[0]);
901
955
opts |= OPT_USER;
904
958
opts |= OPT_VERBOSE;
912
("Namespace option requires an argument."));
914
if (isdigit(optopt)) {
915
if (strlen(argv[optind-1]) == 2)
916
sig_aname =strdup(argv[optind-1]+1);
918
sig_aname = strdup(argv[optind]+1);
919
ignore_options = strlen(argv[optind])-2;
921
sig_number = get_signal(sig_aname,argv[0]);
964
if (isupper(*option) || isdigit(*option)) {
965
sig_number = get_signal(current_argv+1, argv[0]);
925
969
fprintf(stderr, "%s: Invalid option %c\n", argv[0],
932
printf("unknown optc %c\n", optc);
978
/* Not an option, must be a file specification */
937
for (optc = optind; optc < argc; optc++) {
938
/* File specifications */
939
980
if ((this_name = malloc(sizeof(struct names))) == NULL)
941
982
this_name->next = NULL;
942
if (names_head == NULL)
943
names_head = this_name;
944
if (names_tail != NULL)
945
names_tail->next = this_name;
946
names_tail = this_name;
947
983
/* try to find namespace spec */
948
984
this_name->name_space = default_namespace;
949
if (((nsptr = strchr(argv[optc], '/')) != NULL)
950
&& (nsptr != argv[optc])) {
985
if (((nsptr = strchr(current_argv, '/')) != NULL)
986
&& (nsptr != current_argv)) {
951
987
if (strcmp(nsptr + 1, "tcp") == 0) {
952
988
this_name->name_space = NAMESPACE_TCP;
962
998
this_name->matched_procs = NULL;
963
if ((opts & OPT_MOUNTS || opts & OPT_MOUNTPOINT)
999
if (opts & (OPT_MOUNTS|OPT_ISMOUNTPOINT)
964
1000
&& this_name->name_space != NAMESPACE_FILE)
966
("You can only use files with mountpoint option"));
1002
("You can only use files with mountpoint options"));
1003
if (opts & OPT_ISMOUNTPOINT &&
1004
!is_mountpoint(&mounts, current_argv)) {
967
1008
switch (this_name->name_space) {
968
1009
case NAMESPACE_TCP:
969
if (asprintf(&(this_name->filename), "%s/tcp", argv[optc]) > 0) {
1010
if (asprintf(&(this_name->filename), "%s/tcp", current_argv) > 0) {
970
1011
#ifdef WITH_IPV6
971
1012
parse_inet(this_name, ipv4_only, ipv6_only,
972
1013
&tcp_connection_list, &tcp6_connection_list);
988
1029
default: /* FILE */
989
this_name->filename = strdup(argv[optc]);
990
parse_file(this_name, &match_inodes);
991
parse_unixsockets(this_name, &match_inodes,
993
if (opts & OPT_MOUNTPOINT || opts & OPT_MOUNTS)
1030
this_name->filename = strdup(current_argv);
1031
if (parse_file(this_name, &match_inodes) == 0) {
1032
parse_unixsockets(this_name, &match_inodes, unixsockets);
1033
if (opts & OPT_MOUNTS)
994
1034
parse_mounts(this_name, &match_devices, opts);
1000
if (names_head == NULL) {
1039
if (names_head == NULL)
1040
names_head = this_name;
1041
if (names_tail != NULL)
1042
names_tail->next = this_name;
1043
names_tail = this_name;
1044
} /* for across the argvs */
1045
if (names_head == NULL)
1001
1046
usage(_("No process specification given"));
1003
/* Check conflicting operations */
1004
if (opts & OPT_MOUNTPOINT) {
1005
if (opts & OPT_MOUNTS)
1007
("You cannot use the mounted and mountpoint flags together"));
1009
1048
if (opts & OPT_SILENT) {
1010
1049
opts &= ~OPT_VERBOSE;
1011
1050
opts &= ~OPT_USER;
1450
1489
const int sig_number)
1452
1491
struct procs *pptr;
1454
1496
for (pptr = proc_head; pptr != NULL; pptr = pptr->next) {
1497
if ( pptr->pid == mypid)
1498
continue; /* dont kill myself */
1499
if ( pptr->proc_type != PTYPE_NORMAL )
1455
1501
if ((opts & OPT_INTERACTIVE) && (ask(pptr->pid) == 0))
1457
if (kill(pptr->pid, sig_number) < 0) {
1503
if ( kill(pptr->pid, sig_number) < 0) {
1458
1504
fprintf(stderr, _("Could not kill process %d: %s\n"),
1459
pptr->pid, strerror(errno));
1505
pptr->pid, strerror(errno));