~mmach/netext73/sysvinit

« back to all changes in this revision

Viewing changes to src/killall5.c

  • Committer: mmach
  • Date: 2023-11-26 16:47:13 UTC
  • Revision ID: netbit73@gmail.com-20231126164713-wzy9kmqvr8q6ydml
308

Show diffs side-by-side

added added

removed removed

Lines of Context:
120
120
} NFS;
121
121
 
122
122
/* List of processes. */
123
 
PROC *plist;
 
123
PROC *plist = NULL;
124
124
 
125
125
/* List of processes to omit. */
126
 
OMIT *omit;
 
126
OMIT *omit = NULL;
127
127
 
128
128
/* List of NFS mountes partitions. */
129
 
NFS *nlist;
 
129
NFS *nlist = NULL;
130
130
 
131
131
/* Did we stop all processes ? */
132
 
int sent_sigstop;
 
132
int sent_sigstop = 0;
133
133
int scripts_too = 0;
134
134
 
135
135
/* Should pidof try to list processes in I/O wait (D) and zombie (Z) states? */
662
662
                /* Try to stat the executable. */
663
663
                snprintf(path, sizeof(path), "/proc/%s/exe", d->d_name);
664
664
                p->pathname = (char *)xmalloc(PATH_MAX);
 
665
                memset(p->pathname, 0, PATH_MAX);
665
666
                if (readlink(path, p->pathname, PATH_MAX) == -1) {
666
667
                        p->pathname = NULL;
667
668
                }
739
740
                return NULL;
740
741
 
741
742
        /* Try to stat the executable. */
 
743
        memset(real_path, 0, sizeof(real_path));
742
744
        if ( (prog[0] == '/') && ( realpath(prog, real_path) ) ) {
743
 
                memset(&real_path[0], 0, sizeof(real_path));
744
745
                dostat++;
745
746
        }
746
747
 
763
764
                                add_pid_to_q(q, p);
764
765
                                foundone++;
765
766
                        }
 
767
                        else if ( (p->argv0) && (! strcmp(p->argv0, prog) ) )
 
768
                        {
 
769
                            add_pid_to_q(q, p);
 
770
                            foundone++;
 
771
                        }
766
772
                }
767
773
        }
768
774
 
826
832
                 * as was done in earlier versions of this program, since this
827
833
                 * allows /aaa/foo to match /bbb/foo .
828
834
                 */
 
835
 
829
836
                ok |=
830
837
                        (p->argv0 && strcmp(p->argv0, prog) == 0)
831
838
                        || (p->argv0 && s != prog && strcmp(p->argv0, s) == 0)
873
880
/* Give usage message and exit. */
874
881
void usage(void)
875
882
{
876
 
        nsyslog(LOG_ERR, "only one argument, a signal number, allowed");
 
883
        nsyslog(LOG_ERR, "usage: killall5 -signum [-o omitpid] [-o omitpid] ...");
877
884
        closelog();
878
885
        exit(1);
879
886
}
1152
1159
        /* lock us into memory */
1153
1160
        mlockall(MCL_CURRENT | MCL_FUTURE);
1154
1161
 
1155
 
        /* Now stop all processes. */
1156
 
        kill(-1, SIGSTOP);
1157
 
        sent_sigstop = 1;
 
1162
        /* Get our session ID and PID to make sure we do not kill ourselves or our session. */
 
1163
        sid = (int)getsid(0);
 
1164
        pid = (int)getpid();
 
1165
 
 
1166
        /* Now stop all processes, unless there are some we should omit. */
 
1167
        if (! omit)
 
1168
        {
 
1169
            kill(-1, SIGSTOP);
 
1170
            sent_sigstop = 1;
 
1171
        }
1158
1172
 
1159
1173
        /* Read /proc filesystem */
1160
1174
        if (readproc() < 0) {
 
1175
             if (sent_sigstop)
1161
1176
                kill(-1, SIGCONT);
1162
 
                return(1);
 
1177
             return(1);
1163
1178
        }
1164
1179
 
1165
1180
        /* Now kill all processes except init (pid 1) and our session. */
1166
 
        sid = (int)getsid(0);
1167
 
        pid = (int)getpid();
1168
1181
        for (p = plist; p; p = p->next) {
1169
1182
                if (p->pid == 1 || p->pid == pid || p->sid == sid || p->kernel)
1170
1183
                        continue;
1179
1192
                        /* On a match, continue with the for loop above. */
1180
1193
                        if (optr)
1181
1194
                                continue;
1182
 
                }
 
1195
                }      /* end of skipping PIDs to omit */
1183
1196
 
1184
1197
                kill(p->pid, sig);
1185
1198
                retval = 0;
1186
1199
        }
1187
1200
 
1188
1201
        /* And let them continue. */
1189
 
        kill(-1, SIGCONT);
 
1202
        if (sent_sigstop)
 
1203
            kill(-1, SIGCONT);
1190
1204
 
1191
1205
        /* Done. */
1192
1206
        closelog();