Changeset 1765 in MondoRescue for branches/2.2.5/mindi-busybox/procps/fuser.c


Ignore:
Timestamp:
Nov 4, 2007, 3:16:40 AM (16 years ago)
Author:
Bruno Cornec
Message:

Update to busybox 1.7.2

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.5/mindi-busybox/procps/fuser.c

    r821 r1765  
    99 */
    1010
    11 #include "busybox.h"
    12 #include <stdio.h>
    13 #include <stdlib.h>
    14 #include <unistd.h>
    15 #include <string.h>
    16 #include <limits.h>
    17 #include <dirent.h>
    18 #include <signal.h>
    19 #include <sys/types.h>
    20 #include <sys/ioctl.h>
    21 #include <sys/stat.h>
    22 #include <sys/socket.h>
    23 #include <sys/sysmacros.h>
     11#include "libbb.h"
    2412
    2513#define FUSER_PROC_DIR "/proc"
     
    4735    int opt = 0;
    4836
    49     if(!(strlen(option))) return 0;
    50     if(option[0] != '-') return 0;
     37    if (!option[0])
     38        return 0;
     39    if (option[0] != '-')
     40        return 0;
    5141    ++option;
    52     while(*option != '\0') {
    53         if(*option == 'm') opt |= FUSER_OPT_MOUNT;
    54         else if(*option == 'k') opt |= FUSER_OPT_KILL;
    55         else if(*option == 's') opt |= FUSER_OPT_SILENT;
    56         else if(*option == '6') opt |= FUSER_OPT_IP6;
    57         else if(*option == '4') opt |= FUSER_OPT_IP4;
    58         else {
    59             bb_error_msg_and_die(
    60                 "Unsupported option '%c'", *option);
    61         }
     42    while (*option != '\0') {
     43        if (*option == 'm') opt |= FUSER_OPT_MOUNT;
     44        else if (*option == 'k') opt |= FUSER_OPT_KILL;
     45        else if (*option == 's') opt |= FUSER_OPT_SILENT;
     46        else if (*option == '6') opt |= FUSER_OPT_IP6;
     47        else if (*option == '4') opt |= FUSER_OPT_IP4;
     48        else
     49            bb_error_msg_and_die("unsupported option '%c'", *option);
    6250        ++option;
    6351    }
     
    6957{
    7058    struct stat f_stat;
    71     if((stat(filename, &f_stat)) < 0) return 0;
     59    if ((stat(filename, &f_stat)) < 0)
     60        return 0;
    7261    *inode = f_stat.st_ino;
    7362    *dev = f_stat.st_dev;
     
    8170
    8271    if (fd >= 0 && (fstat(fd, &buf)) == 0) {
    83         *dev =  buf.st_dev;
     72        *dev = buf.st_dev;
    8473        close(fd);
    8574        return 1;
     
    9382    char path[sizeof(FUSER_PROC_DIR)+12], tproto[5];
    9483
    95     if((sscanf(filename, "%d/%4s", port, tproto)) != 2) return 0;
    96     sprintf(path, "%s/net/%s", FUSER_PROC_DIR, tproto);
    97     if((access(path, R_OK)) != 0) return 0;
    98     *proto = bb_xstrdup(tproto);
     84    if ((sscanf(filename, "%d/%4s", port, tproto)) != 2)
     85        return 0;
     86    sprintf(path, FUSER_PROC_DIR "/net/%s", tproto);
     87    if ((access(path, R_OK)) != 0)
     88        return 0;
     89    *proto = xstrdup(tproto);
    9990    return 1;
    10091}
     
    10495    pid_list *curr = NULL, *last = NULL;
    10596
    106     if(plist->pid == 0) plist->pid = pid;
     97    if (plist->pid == 0)
     98        plist->pid = pid;
    10799    curr = plist;
    108     while(curr != NULL) {
    109         if(curr->pid == pid) return 1;
     100    while (curr != NULL) {
     101        if (curr->pid == pid)
     102            return 1;
    110103        last = curr;
    111104        curr = curr->next;
    112105    }
    113     curr = xmalloc(sizeof(pid_list));
     106    curr = xzalloc(sizeof(pid_list));
    114107    last->next = curr;
    115108    curr->pid = pid;
    116     curr->next = NULL;
     109    /*curr->next = NULL;*/
    117110    return 1;
    118111}
     
    122115    inode_list *curr = NULL, *last = NULL;
    123116
    124     if(!ilist->inode && !ilist->dev) {
     117    if (!ilist->inode && !ilist->dev) {
    125118        ilist->dev = dev;
    126119        ilist->inode = inode;
    127120    }
    128121    curr = ilist;
    129     while(curr != NULL) {
    130         if(curr->inode == inode && curr->dev == dev) return 1;
     122    while (curr != NULL) {
     123        if (curr->inode == inode && curr->dev == dev)
     124            return 1;
    131125        last = curr;
    132126        curr = curr->next;
    133127    }
    134     curr = xmalloc(sizeof(inode_list));
     128    curr = xzalloc(sizeof(inode_list));
    135129    last->next = curr;
    136130    curr->dev = dev;
    137131    curr->inode = inode;
    138     curr->next = NULL;
     132    /*curr->next = NULL;*/
    139133    return 1;
    140134}
     
    147141    ino_t tmp_inode;
    148142    dev_t tmp_dev;
    149     long long  uint64_inode;
     143    long long uint64_inode;
    150144    int tmp_port;
    151145    FILE *f;
    152146
    153     if(!fuser_find_socket_dev(&tmp_dev)) tmp_dev = 0;
    154     sprintf(path, "%s/net/%s", FUSER_PROC_DIR, proto);
    155 
    156     if (!(f = fopen(path, "r"))) return 0;
    157     while(fgets(line, FUSER_MAX_LINE, f)) {
    158         if(sscanf(line,
    159             "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x "
    160             "%*x:%*x %*x %*d %*d %llu",
    161             addr, &tmp_port, &uint64_inode) == 3) {
    162             if((strlen(addr) == 8) &&
    163                 (opts & FUSER_OPT_IP6)) continue;
    164             else if((strlen(addr) > 8) &&
    165                 (opts & FUSER_OPT_IP4)) continue;
    166             if(tmp_port == port) {
     147    if (!fuser_find_socket_dev(&tmp_dev))
     148        tmp_dev = 0;
     149    sprintf(path, FUSER_PROC_DIR "/net/%s", proto);
     150
     151    f = fopen(path, "r");
     152    if (!f)
     153        return 0;
     154    while (fgets(line, FUSER_MAX_LINE, f)) {
     155        if (sscanf(line, "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x "
     156                "%*x:%*x %*x %*d %*d %llu",
     157                addr, &tmp_port, &uint64_inode) == 3
     158        ) {
     159            if (strlen(addr) == 8 && (opts & FUSER_OPT_IP6))
     160                continue;
     161            if (strlen(addr) > 8 && (opts & FUSER_OPT_IP4))
     162                continue;
     163            if (tmp_port == port) {
    167164                tmp_inode = uint64_inode;
    168165                fuser_add_inode(ilist, tmp_dev, tmp_inode);
    169166            }
    170167        }
    171 
    172168    }
    173169    fclose(f);
     
    181177    curr = ilist;
    182178
    183     while(curr) {
    184         if((opts & FUSER_OPT_MOUNT) && curr->dev == dev)
     179    while (curr) {
     180        if ((opts & FUSER_OPT_MOUNT) && curr->dev == dev)
    185181            return 1;
    186         if(curr->inode == inode && curr->dev == dev)
     182        if (curr->inode == inode && curr->dev == dev)
    187183            return 1;
    188184        curr = curr->next;
     
    201197    dev_t dev;
    202198
    203     if (!(file = fopen(fname, "r"))) return 0;
     199    file = fopen(fname, "r");
     200    if (!file)
     201        return 0;
    204202    while (fgets(line, FUSER_MAX_LINE, file)) {
    205         if(sscanf(line, "%*s %*s %*s %x:%x %llu",
    206             &major, &minor, &uint64_inode) != 3) continue;
     203        if (sscanf(line, "%*s %*s %*s %x:%x %llu", &major, &minor, &uint64_inode) != 3)
     204            continue;
    207205        inode = uint64_inode;
    208         if(major == 0 && minor == 0 && inode == 0) continue;
     206        if (major == 0 && minor == 0 && inode == 0)
     207            continue;
    209208        dev = makedev(major, minor);
    210         if(fuser_search_dev_inode(opts, ilist, dev, inode)) {
     209        if (fuser_search_dev_inode(opts, ilist, dev, inode)) {
    211210            fuser_add_pid(plist, pid);
    212211        }
    213 
    214212    }
    215213    fclose(file);
     
    223221    dev_t dev;
    224222
    225     if(!fuser_file_to_dev_inode(lname, &dev, &inode)) return 0;
    226     if(fuser_search_dev_inode(opts, ilist, dev, inode))
     223    if (!fuser_file_to_dev_inode(lname, &dev, &inode))
     224        return 0;
     225    if (fuser_search_dev_inode(opts, ilist, dev, inode))
    227226        fuser_add_pid(plist, pid);
    228227    return 1;
     
    236235    char *lname;
    237236
    238     if((d = opendir(dname))) {
    239         while((de = readdir(d)) != NULL) {
    240             lname = concat_subpath_file(dname, de->d_name);
    241             if(lname == NULL)
    242                 continue;
    243             fuser_scan_link(opts, lname, pid, ilist, plist);
    244             free(lname);
    245         }
    246         closedir(d);
    247     }
    248     else return 0;
    249     return 1;
    250 
     237    d = opendir(dname);
     238    if (!d)
     239        return 0;
     240    while ((de = readdir(d)) != NULL) {
     241        lname = concat_subpath_file(dname, de->d_name);
     242        if (lname == NULL)
     243            continue;
     244        fuser_scan_link(opts, lname, pid, ilist, plist);
     245        free(lname);
     246    }
     247    closedir(d);
     248    return 1;
    251249}
    252250
     
    258256    char *dname;
    259257
    260     if(!(d = opendir(FUSER_PROC_DIR))) return 0;
    261     while((de = readdir(d)) != NULL) {
     258    d = opendir(FUSER_PROC_DIR);
     259    if (!d)
     260        return 0;
     261    while ((de = readdir(d)) != NULL) {
    262262        pid = (pid_t)atoi(de->d_name);
    263         if(!pid) continue;
     263        if (!pid)
     264            continue;
    264265        dname = concat_subpath_file(FUSER_PROC_DIR, de->d_name);
    265         if(chdir(dname) < 0) {
     266        if (chdir(dname) < 0) {
    266267            free(dname);
    267268            continue;
     
    285286    pid_list *curr = plist;
    286287
    287     if(plist == NULL) return 0;
    288     while(curr != NULL) {
    289         if(curr->pid > 0) printf("%d ", curr->pid);
    290         curr = curr->next;
    291     }
    292     printf("\n");
     288    if (plist == NULL)
     289        return 0;
     290    while (curr != NULL) {
     291        if (curr->pid > 0)
     292            printf("%d ", curr->pid);
     293        curr = curr->next;
     294    }
     295    puts("");
    293296    return 1;
    294297}
     
    300303    int success = 1;
    301304
    302     if(plist == NULL) return 0;
    303     while(curr != NULL) {
    304         if(curr->pid > 0 && curr->pid != mypid) {
     305    if (plist == NULL)
     306        return 0;
     307    while (curr != NULL) {
     308        if (curr->pid > 0 && curr->pid != mypid) {
    305309            if (kill(curr->pid, sig) != 0) {
    306                 bb_perror_msg(
    307                     "Could not kill pid '%d'", curr->pid);
     310                bb_perror_msg("kill pid '%d'", curr->pid);
    308311                success = 0;
    309312            }
     
    314317}
    315318
     319int fuser_main(int argc, char **argv);
    316320int fuser_main(int argc, char **argv)
    317321{
     322    /*static -- huh???*/ int opt = 0; /* FUSER_OPT_ */
     323
    318324    int port, i, optn;
    319325    int* fni; /* file name indexes of argv */
    320326    int fnic = 0;  /* file name index count */
    321327    const char *proto;
    322     static int opt = 0; /* FUSER_OPT_ */
    323328    dev_t dev;
    324329    ino_t inode;
     
    332337
    333338    fni = xmalloc(sizeof(int));
    334     for(i=1;i<argc;i++) {
     339    for (i = 1; i < argc; i++) {
    335340        optn = fuser_option(argv[i]);
    336         if(optn) opt |= optn;
    337         else if(argv[i][0] == '-') {
    338             if(!(u_signal_names(argv[i]+1, &killsig, 0)))
     341        if (optn)
     342            opt |= optn;
     343        else if (argv[i][0] == '-') {
     344            killsig = get_signum(argv[i]+1);
     345            if (killsig < 0)
    339346                killsig = SIGTERM;
    340         }
    341         else {
     347        } else {
    342348            fni = xrealloc(fni, sizeof(int) * (fnic+2));
    343349            fni[fnic++] = i;
    344350        }
    345351    }
    346     if(!fnic) return 1;
    347 
    348     pids = xmalloc(sizeof(pid_list));
     352
     353    if (!fnic)
     354        return 1;
     355
    349356    inodes = xmalloc(sizeof(inode_list));
    350     for(i=0;i<fnic;i++) {
    351         if(fuser_parse_net_arg(argv[fni[i]], &proto, &port)) {
     357    for (i = 0; i < fnic; i++) {
     358        if (fuser_parse_net_arg(argv[fni[i]], &proto, &port)) {
    352359            fuser_scan_proc_net(opt, proto, port, inodes);
    353         }
    354         else {
    355             if(!fuser_file_to_dev_inode(
    356                 argv[fni[i]], &dev, &inode)) {
    357                 free(pids);
    358                 free(inodes);
    359                 bb_perror_msg_and_die(
    360                     "Could not open '%s'", argv[fni[i]]);
     360        } else {
     361            if (!fuser_file_to_dev_inode(argv[fni[i]], &dev, &inode)) {
     362                if (ENABLE_FEATURE_CLEAN_UP)
     363                    free(inodes);
     364                bb_perror_msg_and_die("cannot open '%s'", argv[fni[i]]);
    361365            }
    362366            fuser_add_inode(inodes, dev, inode);
    363367        }
    364368    }
     369    pids = xmalloc(sizeof(pid_list));
    365370    success = fuser_scan_proc_pids(opt, inodes, pids);
    366371    /* if the first pid in the list is 0, none have been found */
    367     if(pids->pid == 0) success = 0;
    368     if(success) {
    369         if(opt & FUSER_OPT_KILL) {
     372    if (pids->pid == 0)
     373        success = 0;
     374    if (success) {
     375        if (opt & FUSER_OPT_KILL) {
    370376            success = fuser_kill_pid_list(pids, killsig);
    371         }
    372         else if(!(opt & FUSER_OPT_SILENT)) {
     377        } else if (!(opt & FUSER_OPT_SILENT)) {
    373378            success = fuser_print_pid_list(pids);
    374379        }
    375380    }
    376     free(pids);
    377     free(inodes);
     381    if (ENABLE_FEATURE_CLEAN_UP) {
     382        free(pids);
     383        free(inodes);
     384    }
    378385    /* return 0 on (success == 1) 1 otherwise */
    379386    return (success != 1);
Note: See TracChangeset for help on using the changeset viewer.