fcntl couldn't lock /dev/null and hang





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















A minimal working example, but only for my machine:



int main(int argc, char* argv) {


int fd = open ("/dev/null", O_RDWR|O_CREAT);
if (fd < 0) {
printf("Failed to open filen");
}

struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;

int res = fcntl(fd, F_SETLKW,&lock); // this hangs
if (res < 0) {
printf("Failed to lockn");
}
close (fd);
return (0);
}


The program above hangs only on my machine, and completed instantly on 7 other machines. Is there anything that I can look into to investigate this problem?



strace shows that fcntl was getting stuck when the program (./t) was killed by ^C.



5249  execve("./t", ["./t"], [/* 23 vars */]) = 0
5249 brk(NULL) = 0x1cf6000
5249 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
5249 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
5249 open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
5249 fstat(3, {st_mode=S_IFREG|0644, st_size=98358, ...}) = 0
5249 mmap(NULL, 98358, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f31a1118000
5249 close(3) = 0
5249 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
5249 open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
5249 read(3, "177ELF21133>1Pt2"..., 832) = 832
5249 fstat(3, {st_mode=S_IFREG|0755, st_size=1868984, ...}) = 0
5249 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f31a1117000
5249 mmap(NULL, 3971488, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f31a0b42000
5249 mprotect(0x7f31a0d02000, 2097152, PROT_NONE) = 0
5249 mmap(0x7f31a0f02000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c0000) = 0x7f31a0f02000
5249 mmap(0x7f31a0f08000, 14752, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f31a0f08000
5249 close(3) = 0
5249 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f31a1116000
5249 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f31a1115000
5249 arch_prctl(ARCH_SET_FS, 0x7f31a1116700) = 0
5249 mprotect(0x7f31a0f02000, 16384, PROT_READ) = 0
5249 mprotect(0x600000, 4096, PROT_READ) = 0
5249 mprotect(0x7f31a1131000, 4096, PROT_READ) = 0
5249 munmap(0x7f31a1118000, 98358) = 0
5249 open("/dev/null", O_RDWR|O_CREAT, 03777762203636510) = 3
5249 fcntl(3, F_SETLKW, {l_type=F_WRLCK, l_whence=SEEK_SET, l_start=0, l_len=0}) = ? ERESTARTSYS (To be restarted if SA_RESTART is set)
5249 --- SIGINT {si_signo=SIGINT, si_code=SI_KERNEL} ---
5249 +++ killed by SIGINT +++









share|improve this question

























  • First thing to look into would be strace.

    – Jos
    yesterday











  • @Jos Thanks, I added strace output, what else I can look into?

    – qsp
    yesterday






  • 1





    Why do you want a lock on /dev/null?

    – vidarlo
    yesterday











  • You are asking fnctl to obtain an exclusive (write) lock l_type=F_WRLCK, where (according to man fnctl) F_SETLKW means "... but if a conflicting lock is held on the file, then wait for that lock to be released". You may be able to see why there is a conflicting lock using lslocks - see How to list processes locking file?. If you don't want fnctl to wait in the case of conflict, then try F_SETLK in place of F_SETLKW. Regardless, @vidarlo 's point applies - it doesn't seem like a good idea to be write-locking /dev/null.

    – steeldriver
    13 hours ago




















1















A minimal working example, but only for my machine:



int main(int argc, char* argv) {


int fd = open ("/dev/null", O_RDWR|O_CREAT);
if (fd < 0) {
printf("Failed to open filen");
}

struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;

int res = fcntl(fd, F_SETLKW,&lock); // this hangs
if (res < 0) {
printf("Failed to lockn");
}
close (fd);
return (0);
}


The program above hangs only on my machine, and completed instantly on 7 other machines. Is there anything that I can look into to investigate this problem?



strace shows that fcntl was getting stuck when the program (./t) was killed by ^C.



5249  execve("./t", ["./t"], [/* 23 vars */]) = 0
5249 brk(NULL) = 0x1cf6000
5249 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
5249 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
5249 open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
5249 fstat(3, {st_mode=S_IFREG|0644, st_size=98358, ...}) = 0
5249 mmap(NULL, 98358, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f31a1118000
5249 close(3) = 0
5249 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
5249 open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
5249 read(3, "177ELF21133>1Pt2"..., 832) = 832
5249 fstat(3, {st_mode=S_IFREG|0755, st_size=1868984, ...}) = 0
5249 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f31a1117000
5249 mmap(NULL, 3971488, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f31a0b42000
5249 mprotect(0x7f31a0d02000, 2097152, PROT_NONE) = 0
5249 mmap(0x7f31a0f02000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c0000) = 0x7f31a0f02000
5249 mmap(0x7f31a0f08000, 14752, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f31a0f08000
5249 close(3) = 0
5249 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f31a1116000
5249 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f31a1115000
5249 arch_prctl(ARCH_SET_FS, 0x7f31a1116700) = 0
5249 mprotect(0x7f31a0f02000, 16384, PROT_READ) = 0
5249 mprotect(0x600000, 4096, PROT_READ) = 0
5249 mprotect(0x7f31a1131000, 4096, PROT_READ) = 0
5249 munmap(0x7f31a1118000, 98358) = 0
5249 open("/dev/null", O_RDWR|O_CREAT, 03777762203636510) = 3
5249 fcntl(3, F_SETLKW, {l_type=F_WRLCK, l_whence=SEEK_SET, l_start=0, l_len=0}) = ? ERESTARTSYS (To be restarted if SA_RESTART is set)
5249 --- SIGINT {si_signo=SIGINT, si_code=SI_KERNEL} ---
5249 +++ killed by SIGINT +++









share|improve this question

























  • First thing to look into would be strace.

    – Jos
    yesterday











  • @Jos Thanks, I added strace output, what else I can look into?

    – qsp
    yesterday






  • 1





    Why do you want a lock on /dev/null?

    – vidarlo
    yesterday











  • You are asking fnctl to obtain an exclusive (write) lock l_type=F_WRLCK, where (according to man fnctl) F_SETLKW means "... but if a conflicting lock is held on the file, then wait for that lock to be released". You may be able to see why there is a conflicting lock using lslocks - see How to list processes locking file?. If you don't want fnctl to wait in the case of conflict, then try F_SETLK in place of F_SETLKW. Regardless, @vidarlo 's point applies - it doesn't seem like a good idea to be write-locking /dev/null.

    – steeldriver
    13 hours ago
















1












1








1








A minimal working example, but only for my machine:



int main(int argc, char* argv) {


int fd = open ("/dev/null", O_RDWR|O_CREAT);
if (fd < 0) {
printf("Failed to open filen");
}

struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;

int res = fcntl(fd, F_SETLKW,&lock); // this hangs
if (res < 0) {
printf("Failed to lockn");
}
close (fd);
return (0);
}


The program above hangs only on my machine, and completed instantly on 7 other machines. Is there anything that I can look into to investigate this problem?



strace shows that fcntl was getting stuck when the program (./t) was killed by ^C.



5249  execve("./t", ["./t"], [/* 23 vars */]) = 0
5249 brk(NULL) = 0x1cf6000
5249 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
5249 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
5249 open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
5249 fstat(3, {st_mode=S_IFREG|0644, st_size=98358, ...}) = 0
5249 mmap(NULL, 98358, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f31a1118000
5249 close(3) = 0
5249 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
5249 open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
5249 read(3, "177ELF21133>1Pt2"..., 832) = 832
5249 fstat(3, {st_mode=S_IFREG|0755, st_size=1868984, ...}) = 0
5249 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f31a1117000
5249 mmap(NULL, 3971488, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f31a0b42000
5249 mprotect(0x7f31a0d02000, 2097152, PROT_NONE) = 0
5249 mmap(0x7f31a0f02000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c0000) = 0x7f31a0f02000
5249 mmap(0x7f31a0f08000, 14752, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f31a0f08000
5249 close(3) = 0
5249 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f31a1116000
5249 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f31a1115000
5249 arch_prctl(ARCH_SET_FS, 0x7f31a1116700) = 0
5249 mprotect(0x7f31a0f02000, 16384, PROT_READ) = 0
5249 mprotect(0x600000, 4096, PROT_READ) = 0
5249 mprotect(0x7f31a1131000, 4096, PROT_READ) = 0
5249 munmap(0x7f31a1118000, 98358) = 0
5249 open("/dev/null", O_RDWR|O_CREAT, 03777762203636510) = 3
5249 fcntl(3, F_SETLKW, {l_type=F_WRLCK, l_whence=SEEK_SET, l_start=0, l_len=0}) = ? ERESTARTSYS (To be restarted if SA_RESTART is set)
5249 --- SIGINT {si_signo=SIGINT, si_code=SI_KERNEL} ---
5249 +++ killed by SIGINT +++









share|improve this question
















A minimal working example, but only for my machine:



int main(int argc, char* argv) {


int fd = open ("/dev/null", O_RDWR|O_CREAT);
if (fd < 0) {
printf("Failed to open filen");
}

struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;

int res = fcntl(fd, F_SETLKW,&lock); // this hangs
if (res < 0) {
printf("Failed to lockn");
}
close (fd);
return (0);
}


The program above hangs only on my machine, and completed instantly on 7 other machines. Is there anything that I can look into to investigate this problem?



strace shows that fcntl was getting stuck when the program (./t) was killed by ^C.



5249  execve("./t", ["./t"], [/* 23 vars */]) = 0
5249 brk(NULL) = 0x1cf6000
5249 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
5249 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
5249 open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
5249 fstat(3, {st_mode=S_IFREG|0644, st_size=98358, ...}) = 0
5249 mmap(NULL, 98358, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f31a1118000
5249 close(3) = 0
5249 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
5249 open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
5249 read(3, "177ELF21133>1Pt2"..., 832) = 832
5249 fstat(3, {st_mode=S_IFREG|0755, st_size=1868984, ...}) = 0
5249 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f31a1117000
5249 mmap(NULL, 3971488, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f31a0b42000
5249 mprotect(0x7f31a0d02000, 2097152, PROT_NONE) = 0
5249 mmap(0x7f31a0f02000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c0000) = 0x7f31a0f02000
5249 mmap(0x7f31a0f08000, 14752, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f31a0f08000
5249 close(3) = 0
5249 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f31a1116000
5249 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f31a1115000
5249 arch_prctl(ARCH_SET_FS, 0x7f31a1116700) = 0
5249 mprotect(0x7f31a0f02000, 16384, PROT_READ) = 0
5249 mprotect(0x600000, 4096, PROT_READ) = 0
5249 mprotect(0x7f31a1131000, 4096, PROT_READ) = 0
5249 munmap(0x7f31a1118000, 98358) = 0
5249 open("/dev/null", O_RDWR|O_CREAT, 03777762203636510) = 3
5249 fcntl(3, F_SETLKW, {l_type=F_WRLCK, l_whence=SEEK_SET, l_start=0, l_len=0}) = ? ERESTARTSYS (To be restarted if SA_RESTART is set)
5249 --- SIGINT {si_signo=SIGINT, si_code=SI_KERNEL} ---
5249 +++ killed by SIGINT +++






files






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday







qsp

















asked yesterday









qspqsp

93110




93110













  • First thing to look into would be strace.

    – Jos
    yesterday











  • @Jos Thanks, I added strace output, what else I can look into?

    – qsp
    yesterday






  • 1





    Why do you want a lock on /dev/null?

    – vidarlo
    yesterday











  • You are asking fnctl to obtain an exclusive (write) lock l_type=F_WRLCK, where (according to man fnctl) F_SETLKW means "... but if a conflicting lock is held on the file, then wait for that lock to be released". You may be able to see why there is a conflicting lock using lslocks - see How to list processes locking file?. If you don't want fnctl to wait in the case of conflict, then try F_SETLK in place of F_SETLKW. Regardless, @vidarlo 's point applies - it doesn't seem like a good idea to be write-locking /dev/null.

    – steeldriver
    13 hours ago





















  • First thing to look into would be strace.

    – Jos
    yesterday











  • @Jos Thanks, I added strace output, what else I can look into?

    – qsp
    yesterday






  • 1





    Why do you want a lock on /dev/null?

    – vidarlo
    yesterday











  • You are asking fnctl to obtain an exclusive (write) lock l_type=F_WRLCK, where (according to man fnctl) F_SETLKW means "... but if a conflicting lock is held on the file, then wait for that lock to be released". You may be able to see why there is a conflicting lock using lslocks - see How to list processes locking file?. If you don't want fnctl to wait in the case of conflict, then try F_SETLK in place of F_SETLKW. Regardless, @vidarlo 's point applies - it doesn't seem like a good idea to be write-locking /dev/null.

    – steeldriver
    13 hours ago



















First thing to look into would be strace.

– Jos
yesterday





First thing to look into would be strace.

– Jos
yesterday













@Jos Thanks, I added strace output, what else I can look into?

– qsp
yesterday





@Jos Thanks, I added strace output, what else I can look into?

– qsp
yesterday




1




1





Why do you want a lock on /dev/null?

– vidarlo
yesterday





Why do you want a lock on /dev/null?

– vidarlo
yesterday













You are asking fnctl to obtain an exclusive (write) lock l_type=F_WRLCK, where (according to man fnctl) F_SETLKW means "... but if a conflicting lock is held on the file, then wait for that lock to be released". You may be able to see why there is a conflicting lock using lslocks - see How to list processes locking file?. If you don't want fnctl to wait in the case of conflict, then try F_SETLK in place of F_SETLKW. Regardless, @vidarlo 's point applies - it doesn't seem like a good idea to be write-locking /dev/null.

– steeldriver
13 hours ago







You are asking fnctl to obtain an exclusive (write) lock l_type=F_WRLCK, where (according to man fnctl) F_SETLKW means "... but if a conflicting lock is held on the file, then wait for that lock to be released". You may be able to see why there is a conflicting lock using lslocks - see How to list processes locking file?. If you don't want fnctl to wait in the case of conflict, then try F_SETLK in place of F_SETLKW. Regardless, @vidarlo 's point applies - it doesn't seem like a good idea to be write-locking /dev/null.

– steeldriver
13 hours ago












0






active

oldest

votes












Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1131547%2ffcntl-couldnt-lock-dev-null-and-hang%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1131547%2ffcntl-couldnt-lock-dev-null-and-hang%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

日野市

GameSpot

Tu-95轟炸機