open() function with denied permission












0















I am trying to create a 'test.txt' file in root directory. Currently I am not a root user. my code is as follows:



#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(){
int fd;
fd=open("/test.txt",O_CREAT|O_RDWR|O_TRUNC,0777);
perror("error: ");
close(fd);

return 0;
}


however, when I compile and execute it, I get the error:



error: : Permission denied


how can I make my code to have the permission to create a file in root directory?



P.S. I am trying to make this work so that I can use it later on to apply this method to my linux daemon program.










share|improve this question




















  • 11





    Your program has to be run as a user which have permission to write in the asked directory. I suggest you rethink where to place your daemon files, have a separate folder (in e.g. /var/lib or similar) owned by a special user created just for your daemon. That way you don't have to run the daemon as root and sidestep a big security risk.

    – Some programmer dude
    Sep 6 '14 at 14:01













  • you should run your program as a admin/root

    – µtex
    Sep 6 '14 at 14:05






  • 1





    @user1234 no, not really.

    – The Paramagnetic Croissant
    Sep 6 '14 at 15:08











  • @JoachimPileborg thanks I think you're right

    – kwagjj
    Sep 6 '14 at 18:12
















0















I am trying to create a 'test.txt' file in root directory. Currently I am not a root user. my code is as follows:



#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(){
int fd;
fd=open("/test.txt",O_CREAT|O_RDWR|O_TRUNC,0777);
perror("error: ");
close(fd);

return 0;
}


however, when I compile and execute it, I get the error:



error: : Permission denied


how can I make my code to have the permission to create a file in root directory?



P.S. I am trying to make this work so that I can use it later on to apply this method to my linux daemon program.










share|improve this question




















  • 11





    Your program has to be run as a user which have permission to write in the asked directory. I suggest you rethink where to place your daemon files, have a separate folder (in e.g. /var/lib or similar) owned by a special user created just for your daemon. That way you don't have to run the daemon as root and sidestep a big security risk.

    – Some programmer dude
    Sep 6 '14 at 14:01













  • you should run your program as a admin/root

    – µtex
    Sep 6 '14 at 14:05






  • 1





    @user1234 no, not really.

    – The Paramagnetic Croissant
    Sep 6 '14 at 15:08











  • @JoachimPileborg thanks I think you're right

    – kwagjj
    Sep 6 '14 at 18:12














0












0








0


1






I am trying to create a 'test.txt' file in root directory. Currently I am not a root user. my code is as follows:



#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(){
int fd;
fd=open("/test.txt",O_CREAT|O_RDWR|O_TRUNC,0777);
perror("error: ");
close(fd);

return 0;
}


however, when I compile and execute it, I get the error:



error: : Permission denied


how can I make my code to have the permission to create a file in root directory?



P.S. I am trying to make this work so that I can use it later on to apply this method to my linux daemon program.










share|improve this question
















I am trying to create a 'test.txt' file in root directory. Currently I am not a root user. my code is as follows:



#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(){
int fd;
fd=open("/test.txt",O_CREAT|O_RDWR|O_TRUNC,0777);
perror("error: ");
close(fd);

return 0;
}


however, when I compile and execute it, I get the error:



error: : Permission denied


how can I make my code to have the permission to create a file in root directory?



P.S. I am trying to make this work so that I can use it later on to apply this method to my linux daemon program.







c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 6:23









Community

11




11










asked Sep 6 '14 at 13:55









kwagjjkwagjj

281317




281317








  • 11





    Your program has to be run as a user which have permission to write in the asked directory. I suggest you rethink where to place your daemon files, have a separate folder (in e.g. /var/lib or similar) owned by a special user created just for your daemon. That way you don't have to run the daemon as root and sidestep a big security risk.

    – Some programmer dude
    Sep 6 '14 at 14:01













  • you should run your program as a admin/root

    – µtex
    Sep 6 '14 at 14:05






  • 1





    @user1234 no, not really.

    – The Paramagnetic Croissant
    Sep 6 '14 at 15:08











  • @JoachimPileborg thanks I think you're right

    – kwagjj
    Sep 6 '14 at 18:12














  • 11





    Your program has to be run as a user which have permission to write in the asked directory. I suggest you rethink where to place your daemon files, have a separate folder (in e.g. /var/lib or similar) owned by a special user created just for your daemon. That way you don't have to run the daemon as root and sidestep a big security risk.

    – Some programmer dude
    Sep 6 '14 at 14:01













  • you should run your program as a admin/root

    – µtex
    Sep 6 '14 at 14:05






  • 1





    @user1234 no, not really.

    – The Paramagnetic Croissant
    Sep 6 '14 at 15:08











  • @JoachimPileborg thanks I think you're right

    – kwagjj
    Sep 6 '14 at 18:12








11




11





Your program has to be run as a user which have permission to write in the asked directory. I suggest you rethink where to place your daemon files, have a separate folder (in e.g. /var/lib or similar) owned by a special user created just for your daemon. That way you don't have to run the daemon as root and sidestep a big security risk.

– Some programmer dude
Sep 6 '14 at 14:01







Your program has to be run as a user which have permission to write in the asked directory. I suggest you rethink where to place your daemon files, have a separate folder (in e.g. /var/lib or similar) owned by a special user created just for your daemon. That way you don't have to run the daemon as root and sidestep a big security risk.

– Some programmer dude
Sep 6 '14 at 14:01















you should run your program as a admin/root

– µtex
Sep 6 '14 at 14:05





you should run your program as a admin/root

– µtex
Sep 6 '14 at 14:05




1




1





@user1234 no, not really.

– The Paramagnetic Croissant
Sep 6 '14 at 15:08





@user1234 no, not really.

– The Paramagnetic Croissant
Sep 6 '14 at 15:08













@JoachimPileborg thanks I think you're right

– kwagjj
Sep 6 '14 at 18:12





@JoachimPileborg thanks I think you're right

– kwagjj
Sep 6 '14 at 18:12












1 Answer
1






active

oldest

votes


















0














    fd=open("/test.txt",O_CREAT|O_RDWR|O_TRUNC,0777);


In this line specify path correctly.(i.e) It is current working directory means, keep (dot) . before / like./test.txt






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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%2fstackoverflow.com%2fquestions%2f25701020%2fopen-function-with-denied-permission%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














        fd=open("/test.txt",O_CREAT|O_RDWR|O_TRUNC,0777);


    In this line specify path correctly.(i.e) It is current working directory means, keep (dot) . before / like./test.txt






    share|improve this answer




























      0














          fd=open("/test.txt",O_CREAT|O_RDWR|O_TRUNC,0777);


      In this line specify path correctly.(i.e) It is current working directory means, keep (dot) . before / like./test.txt






      share|improve this answer


























        0












        0








        0







            fd=open("/test.txt",O_CREAT|O_RDWR|O_TRUNC,0777);


        In this line specify path correctly.(i.e) It is current working directory means, keep (dot) . before / like./test.txt






        share|improve this answer













            fd=open("/test.txt",O_CREAT|O_RDWR|O_TRUNC,0777);


        In this line specify path correctly.(i.e) It is current working directory means, keep (dot) . before / like./test.txt







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 6 '14 at 18:00









        Anbu.SankarAnbu.Sankar

        1,153515




        1,153515
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • 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%2fstackoverflow.com%2fquestions%2f25701020%2fopen-function-with-denied-permission%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

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks