开发者

Is there any alternative API for getcwd to get the working direcory of process [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 3 years ago.

Improve this question

In our application we call getcwd(3) to get the current working directory.

When the process starts running, if anyone deletes the directory path, then the process is running but getcwd API fails (returns NULL).

Example: Process name is: a.exe present in /root/appl/a.exe after running a.exe, if the current working directory is deleted the getcwd(3) api fails.

Is there any alternative API for getcwd(3) to know the current working directory of a process, even if the directory pa开发者_运维百科th deleted?


I'm not entirely sure what you will do with the results of the current working directory when the directory will continue to exist only as long as it is held open -- you can't create new files in the directory, and it had to be empty so it could be deleted -- but you can use readlink(2) on /proc/self/cwd to discover the name:

$ mkdir syedsma
$ cd syedsma/
$ /tmp/proccwd 
/proc/self/cwd reports: /tmp/syedsma
$ /tmp/getcwd 
getcwd: /tmp/syedsma
$ rmdir ../syedsma/
$ /tmp/getcwd
getcwd failed: No such file or directory
$ /tmp/proccwd 
/proc/self/cwd reports: /tmp/syedsma (deleted)
$ 

Here's my getcwd.c:

#include <stdio.h>
#include <unistd.h>

int main(int argc, char* argv[]) {
    char p[1000];
    char *r;
    r = getcwd(p, sizeof(p));
    if (!r)
        perror("getcwd failed");
    else
        printf("getcwd: %s\n", p);
    return 0;
}

And here's my proccwd.c:

#include <stdio.h>
#include <unistd.h>
#include <limits.h>

int main(int argc, char* argv[]) {
    char buf[PATH_MAX];

    ssize_t r = readlink("/proc/self/cwd", buf, sizeof(buf));

    if (r < 0) {
        perror("readlink /proc/self/cwd failed");
        return 1;
    } else {
        buf[PATH_MAX-1] = '\0';
        printf("/proc/self/cwd reports: %s\n", buf);
    }
    return 0;
}

mu is too short is correct with his advice to chdir("/"); if it is a daemon -- I can imagine that you might have a good reason for your program to otherwise know its current working directory, and even have an idea of what the pathname might have been if it did still exist -- but in general, you shouldn't care. The pathname "." will work in just about every case where it makes sense to need the current working directory, until you need to implement a pwd shell built-in for the user.


Try this.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

int main() {
  char path[256];
  char buf[BUFSIZ];

  mkdir("/tmp/foo", 0755);
  chdir("/tmp/foo");
  rmdir("/tmp/foo");

  sprintf(path, "/proc/%d/cwd", getpid());
  if (readlink(path, buf, sizeof(buf)) != -1) {
    char* stop = buf+strlen(buf)-10;
    if (!strcmp(stop, " (deleted)")) {
      *stop = 0;
    }
    printf("[%s]\n", buf);
  }
}


get the $PWD environment variable and cache it in your application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜