C 和文件权限
我正在使用 Ubuntu Lucid Lynx
是否可以通过具有只读权限的 C 程序在文件中写入数据。如果不可能,那么有什么方法可以让 sudo 访问 C 程序。
对于没有权限的文件,我会将数据保存在字符串中。然后我会使用写入选项打开文件:
FILE *fp = fopen(file_path,"w")
fputs(string,fp);
fclose(fp);
I am using Ubuntu Lucid Lynx
is it possible to write data in a file through a C program which has read only permission. If it's not possible, then is there any way to give sudo access to the C program.
For files without permissions, I would save the data in a string. Then i would open the file with write option:
FILE *fp = fopen(file_path,"w")
fputs(string,fp);
fclose(fp);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,您不能写入只读文件,这会破坏只读的全部意义。至于给予 ac 程序 root 访问权限,您始终可以以 root 身份或有权修改文件的人身份运行它。
No you cannot write to a read-only file, that would undermine the whole point of read-only. As for giving root access to a c program, you could always run it as root or as someone who has permissions to modify the file.
如果您是文件的所有者,则可以使用 chmod 更改权限。如果您不是所有者,您可以使用 setuid 位来访问该文件,但这确实应该是避免了。
If you are the owner of the file you can change the permissions with chmod. If you are not the owner you can use setuid bit to acces the file, but this really should be avoided.
程序在运行时无法将自身提升到 sudo 状态。
这真是一件好事。
如果可能的话,每种病毒都将有能力接管任何系统。
该程序需要从一开始就以适当的访问权限启动。
It is not possible to have the program elevate itself to sudo status while its running.
And that is a bloody good thing.
If that was possible every virus would have the ability to take over any system.
The program needs to be started with the proper access-rights from the start.
在 Unix 中,通常你想要做的就是使程序的可执行文件由 root 拥有,世界可执行文件,然后在其上设置 SUID 位。
请参阅 chomd 文档 了解这些位的确切含义,但总而言之,这意味着每当有人运行此可执行文件时,他们将可执行进程的用户 ID 设置为所有者(root)。
In Unix generally what you'd want to do is make the program's executable owned by root, world executable, then set the SUID bit on it.
See chomd docs for exactly what the bits mean, but all together this means that whenever someone runs this executable, they get the executable process' user ID set to the owner's (root).
您可以,但不是以标准的跨平台方式。
在 Unix 系统上,使用 chmod。它应该位于
中,请参阅此链接< /a>.
如果您需要 root 权限,请尝试:
You can, but not in a standard cross-platform way.
On Unix systems, use
chmod
. It should be in<sys/stat.h>
See this link.
If you need root privilage, try: