2 * LTszoff.c -- Lsof Test small file (< 32 bits) size and offset tests
10 * Copyright 2002 Purdue Research Foundation, West Lafayette, Indiana
11 * 47907. All rights reserved.
13 * Written by V. Abell.
15 * This software is not subject to any license of the American Telephone
16 * and Telegraph Company or the Regents of the University of California.
18 * Permission is granted to anyone to use this software for any purpose on
19 * any computer system, and to alter it and redistribute it freely, subject
20 * to the following restrictions:
22 * 1. Neither the authors nor Purdue University are responsible for any
23 * consequences of the use of this software.
25 * 2. The origin of this software must not be misrepresented, either by
26 * explicit claim or by omission. Credit to the authors and Purdue
27 * University must appear in documentation and sources.
29 * 3. Altered versions must be plainly marked as such, and must not be
30 * misrepresented as being the original software.
32 * 4. This notice may not be removed or altered.
36 static char copyright[] =
37 "@(#) Copyright 2002 Purdue Research Foundation.\nAll rights reserved.\n";
41 #include "lsof_fields.h"
45 * Pre-definitions that might be undefined by dialects
48 #define OFFTST_STAT 1 /* offset tests status */
51 #if defined(LT_DIAL_linux)
53 * Linux-specific items
57 #define OFFTST_STAT 0 /* Linux lsof may not be able to report
58 * offsets -- see the function
59 * ck_Linux_offset_support() */
61 _PROTOTYPE(static int ck_Linux_offset_support,(void));
62 #endif /* defined(LT_DIAL_linux) */
69 #define TYTST_SZ 0 /* size test type */
70 #define TYTST_0to 1 /* 0t offset test type */
71 #define TYTST_0xo 2 /* 0x offset test type */
72 #define TSTFSZ 32768 /* test file size */
79 int Fd = -1; /* test file descriptor; open if >= 0 */
80 pid_t MyPid = (pid_t)0; /* PID of this process */
81 char *Path = (char *)NULL; /* test file path; none if NULL */
82 char *Pn = (char *)NULL; /* program name */
86 * Local function prototypes
89 _PROTOTYPE(static void cleanup,(void));
90 _PROTOTYPE(static char *testlsof,(int tt, char *opt, char *xval));
99 int argc; /* argument count */
100 char *argv[]; /* arguments */
102 char buf[2048]; /* temporary buffer */
103 int do_offt = OFFTST_STAT; /* do offset tests if == 1 */
104 char *em; /* error message pointer */
105 int ti; /* temporary index */
106 char *tcp; /* temporary character pointer */
107 char *tstsz = (char *)NULL; /* size test status */
108 char *tst0to = (char *)NULL; /* offset 0t form test */
109 char *tst0xo = (char *)NULL; /* offset 0x form test */
110 int xv = 0; /* exit value */
111 char xbuf[64]; /* expected value buffer */
113 * Get program name and PID, issue start message, and build space prefix.
115 if ((Pn = strrchr(argv[0], '/')))
120 (void) printf("%s ... ", Pn);
121 (void) fflush(stdout);
122 PrtMsg((char *)NULL, Pn);
126 if (ScanArg(argc, argv, "hp:", Pn))
129 (void) PrtMsg("usage: [-h] [-p path]", Pn);
130 PrtMsg (" -h print help (this panel)", Pn);
131 PrtMsgX (" -p path define test file path", Pn, cleanup, xv);
134 #if defined(LT_DIAL_linux)
136 * If this is Linux, see if lsof can report file offsets.
138 do_offt = ck_Linux_offset_support();
139 #endif /* defined(LT_DIAL_linux) */
142 * See if lsof can be executed and can access kernel memory.
144 if ((em = IsLsofExec()))
145 (void) PrtMsgX(em, Pn, cleanup, 1);
146 if ((em = CanRdKmem()))
147 (void) PrtMsgX(em, Pn, cleanup, 1);
149 * If a path was supplied in an "-p path" option, use it. Otherwise construct
152 if (!(Path = LTopt_p)) {
153 (void) snprintf(buf, sizeof(buf) - 1, "./config.LTszoff%ld",
155 buf[sizeof(buf) - 1] = '\0';
156 Path = MkStrCpy(buf, &ti);
159 * Open a new test file at the specified path.
162 if ((Fd = open(Path, O_RDWR|O_CREAT, 0600)) < 0) {
163 (void) fprintf(stderr, "ERROR!!! can't open %s\n", Path);
168 (void) snprintf(buf, sizeof(buf) - 1, " Errno %d: %s",
169 errno, strerror(errno));
170 buf[sizeof(buf) - 1] = '\0';
171 (void) PrtMsgX(buf, Pn, cleanup, 1);
174 * Write the test file to its expected size.
176 for (ti = 0; ti < sizeof(buf); ti++) {
177 buf[ti] = (char)(ti & 0xff);
179 for (ti = 0; ti < TSTFSZ; ti += sizeof(buf)) {
180 if (write(Fd, buf, sizeof(buf)) != sizeof(buf)) {
181 (void) fprintf(stderr, "ERROR!!! can't write %d bytes to %s\n",
182 (int)sizeof(buf), Path);
183 goto print_file_error;
190 (void) fprintf(stderr, "ERROR!!! can't fsync %s\n", Path);
191 goto print_file_error;
194 * Do the tests. Skip offset tests as indicated.
196 (void) snprintf(xbuf, sizeof(xbuf) - 1, "%d", TSTFSZ);
197 xbuf[sizeof(xbuf) - 1] = '\0';
198 if ((tstsz = testlsof(TYTST_SZ, "-s", xbuf)))
199 (void) PrtMsg(tstsz, Pn);
201 (void) snprintf(xbuf, sizeof(xbuf) - 1, "0t%d", TSTFSZ);
202 xbuf[sizeof(xbuf) - 1] = '\0';
203 if ((tst0to = testlsof(TYTST_0to, "-o", xbuf)))
204 (void) PrtMsg(tst0to, Pn);
205 (void) snprintf(xbuf, sizeof(xbuf) - 1, "0x%x", TSTFSZ);
206 xbuf[sizeof(xbuf) - 1] = '\0';
207 if ((tst0xo = testlsof(TYTST_0xo, "-oo2", xbuf)))
208 (void) PrtMsg(tst0to, Pn);
210 PrtMsg("WARNING!!! lsof can't return file offsets for this dialect,",
212 PrtMsg(" so offset tests have been disabled.", Pn);
215 * Compute exit value and exit.
217 if (tstsz || tst0to || tst0xo) {
224 (void) PrtMsgX(tcp, Pn, cleanup, xv);
229 #if defined(LT_DIAL_linux)
231 * ck_Linux_offset_support() -- see if lsof can report offsets for this
232 * Linux implementation
236 ck_Linux_offset_support()
238 char buf[1024]; /* lsof output line buffer */
239 int bufl = sizeof(buf); /* size of buf[] */
240 char *opv[5]; /* option vector for lsof */
241 int rv = 1; /* return value:
242 * 0 == no lsof offset support
243 * 1 == lsof offset support */
245 * Ask lsof to report the test's FD zero offset.
250 snprintf(buf, bufl - 1, "-p%d", (int)getpid());
254 opv[4] = (char *)NULL;
258 * Read the lsof output. Look for a line with "WARNING: can't report offset"
259 * in it. If it is found, then this Linux lsof can't report offsets.
261 while(fgets(buf, bufl - 1, LsofFs)) {
262 if (strstr(buf, "WARNING: can't report offset")) {
270 #endif /* defined(LT_DIAL_linux) */
274 * cleanup() -- release resources
292 * testlsof() -- test the open file with lsof
296 testlsof(tt, opt, xval)
297 int tt; /* test type -- TYTST_* symbol */
298 char *opt; /* extra lsof options */
299 char *xval; /* expected value */
301 char buf[2048]; /* temporary buffer */
302 char *cem; /* current error message pointer */
303 LTfldo_t *cmdp; /* command pointer */
304 LTfldo_t *devp; /* device pointer */
305 int ff = 0; /* file found status */
306 LTfldo_t *fop; /* field output pointer */
307 char ibuf[64]; /* inode number buffer */
308 LTfldo_t *inop; /* inode number pointer */
309 LTdev_t lsofdc; /* lsof device components */
310 int nf; /* number of fields */
311 LTfldo_t *offp; /* offset pointer */
312 char *opv[4]; /* option vector for ExecLsof() */
313 char *pem = (char *)NULL; /* previous error message pointer */
315 int pids = 0; /* PID found status */
316 struct stat sb; /* stat(2) buffer */
317 LTdev_t stdc; /* stat(2) device components */
318 LTfldo_t *szp; /* size pointer */
319 char *tcp; /* temporary character pointer */
320 int ti; /* temporary integer */
321 char *tnm1, *tnm2; /* test names */
322 int ts = 0; /* test status flag */
323 LTfldo_t *typ; /* file type pointer */
325 * Check the test type.
341 (void) snprintf(buf, sizeof(buf) - 1,
342 "ERROR!!! illegal test type: %d", tt);
343 buf[sizeof(buf) - 1] = '\0';
344 (void) PrtMsgX(buf, Pn, cleanup, 1);
347 * Get test file's information.
349 if (stat(Path, &sb)) {
350 (void) snprintf(buf, sizeof(buf) - 1,
351 "ERROR!!! can't stat(2) %s: %s", Path, strerror(errno));
352 buf[sizeof(buf) - 1] = '\0';
353 PrtMsgX(buf, Pn, cleanup, 1);
356 * Extract components from test file's stat buffer.
358 if ((cem = ConvStatDev(&sb.st_dev, &stdc)))
359 PrtMsgX(buf, Pn, cleanup, 1);
360 (void) snprintf(ibuf, sizeof(ibuf) - 1, "%u", (unsigned int)sb.st_ino);
361 ibuf[sizeof(ibuf) - 1] = '\0';
363 * Complete the option vector and start lsof execution.
369 #if defined(USE_LSOF_C_OPT)
371 #else /* !defined(USE_LSOF_C_OPT) */
373 #endif /* defined(USE_LSOF_C_OPT) */
376 opv[ti] = (char *)NULL;
377 if ((cem = ExecLsof(opv)))
382 while (!ff && !cem && (fop = RdFrLsof(&nf, &cem))) {
385 (void) PrtMsg(pem, Pn);
392 * This is a process information line.
394 pid = (pid_t)atoi(fop->v);
396 cmdp = (LTfldo_t *)NULL;
397 for (fop++, ti = 1; ti < nf; fop++, ti++) {
404 if (!cmdp || (pid != MyPid))
410 * This is a file descriptor line. Make sure its number matches the
411 * test file's descriptor number.
415 for (ti = 0, tcp = fop->v; *tcp; tcp++) {
418 * Convert file descriptor to a number.
422 if (((int)*tcp < (int)'0') || ((int)*tcp > (int)'9')) {
426 ti = (ti * 10) + (int)*tcp - (int)'0';
431 * Scan for device, inode, offset, size and type fields.
433 devp = inop = offp = szp = typ = (LTfldo_t *)NULL;
434 for (fop++, ti = 1; ti < nf; fop++, ti++) {
442 case LSOF_FID_OFFSET:
454 * Check the results of the file descriptor field scan.
456 if (!devp || !inop || !typ)
458 if (strcasecmp(typ->v, "reg") && strcasecmp(typ->v, "vreg"))
460 if ((cem = ConvLsofDev(devp->v, &lsofdc))) {
462 (void) PrtMsg(pem, Pn);
466 if ((stdc.maj != lsofdc.maj)
467 || (stdc.min != lsofdc.min)
468 || (stdc.unit != lsofdc.unit)
469 || strcmp(inop->v, ibuf)
474 * The specified file has been located. Do the specified test.
477 fop = (tt == TYTST_SZ) ? szp : offp;
479 (void) snprintf(buf, sizeof(buf) - 1,
480 "ERROR!!! %s%s test, but no lsof%s", tnm1, tnm2, tnm2);
482 } else if (strcmp(fop->v, xval)) {
483 (void) snprintf(buf, sizeof(buf) - 1,
484 "ERROR!!! %s%s mismatch: expected %s, got %s",
485 tnm1, tnm2, xval, fop->v);
489 buf[sizeof(buf) - 1] = '\0';
490 cem = MkStrCpy(buf, &ti);
492 (void) PrtMsg(pem, Pn);
500 (void) snprintf(buf, sizeof(buf) - 1,
501 "ERROR!!! test file %s not found by lsof", Path);
502 buf[sizeof(buf) - 1] = '\0';
503 cem = MkStrCpy(buf, &ti);
505 (void) PrtMsg(pem, Pn);