Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!ncar!boulder!stan!number6!kucharsk From: kucharsk@number6.Solbourne.COM (William Kucharski) Newsgroups: comp.unix.wizards Subject: Re: Another "why won't this work" program Message-ID: <1990Aug16.002638.17756@Solbourne.COM> Date: 16 Aug 90 00:26:38 GMT References: <24183@adm.BRL.MIL> Sender: news@Solbourne.COM Organization: Solbourne Computer, Inc., Longmont CO Lines: 74 In article <24183@adm.BRL.MIL> BKEHOE@widener writes: >/* > * This is being run on a Sun SS1 under 4.0.3. > * Theoretically, according to the Design & Implementation of 4.3BSD Unix, > * this should print out the ascii version of each process's current > * directory...instead, it chokes on u->u_cwd->cw_dir, which is in the > * u struct in sys/user.h .. any help, suggestions, etc would be greatly > * appreciated. > > */ > >/* > * cc -o cwd cwd.c -lkvm > */ > >#include >#include >#include >#include >#include >#include >#include >#include >#include ... > (void) printf("Name\t\tDir\n"); > kvm_setproc (kd); > while ((proc = kvm_nextproc (kd))) > if (proc->p_stat != SZOMB && proc->p_uid) { > if (!(user = kvm_getu(kd, proc))) > continue; > (void) printf("%s\n", (getpwuid(proc->p_uid))->pw_name); >/* Curtains & Slow Music */ > (void) printf("%s\n", user->u_cwd->cw_dir); >/* It dies, but the user structure's fine (printing user->u_comm works); I > stepped thru it with gdb & discovered that the pointer user->u_cwd is off in > never-never-land; is it a valid entry in >the user structure? */ > } >} That's because the user->u_cwd pointer is really a pointer into the kernel's memory space, rather than user memory space. You need to do something like the following to read the contents of the kernel memory in question into user space: ... char dir[MAXPATHLEN]; char rootdir[MAXPATHLEN]; struct ucwd cwd; if (kvm_read(kd, (unsigned long)userp->u_cwd, (char *)&cwd, sizeof(struct ucwd)) == sizeof(struct ucwd)) { if ((kvm_read(kd, (unsigned long)cwd.cw_dir, dir, MAXPATHLEN) == MAXPATHLEN) && (kvm_read(kd, (unsigned long)cwd.cw_root, rootdir, MAXPATHLEN) == MAXPATHLEN)) { if ((*rootdir) || (*dir)) printf("cwd: %s%s\n", rootdir, dir); else printf("cwd: /\n"); } } ... -- =============================================================================== | Internet: kucharsk@Solbourne.COM | William Kucharski | | uucp: ...!{boulder,sun,uunet}!stan!kucharsk | Solbourne Computer, Inc. | === The sentiments expressed above are MY opinions, NOT those of Solbourne. ===