0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PROTECT_ERRNO

Posted at

systemdにはPROTECT_ERRNOというマクロがある。使い方はこんな感じ。

src/shared/util.c
int unlink_noerrno(const char *path) {
        PROTECT_ERRNO;
        int r;

        r = unlink(path);
        if (r < 0)
                return -errno;

        return 0;
}

定義を見てみると、なるほどと思った。

src/shared/util.h
static inline void _reset_errno_(int *saved_errno) {
        errno = *saved_errno;
}       
        
# define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno

errnoを一旦保存して、関数を抜けるときに cleanup属性 を使って保存したerrnoを元に戻している。

上記の使用例だと、unlink(2)でerrnoが上書きされるのを防いでいるようだ。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?