...how to update system time permanently in solaris through c++.
...how can i login into superuser(su) account through c++ code in solaris.

Dani AI

Generated

Short summary and practical next steps (builds on and ): if your GPS gives you an epoch/UTC timestamp, set the kernel clock directly from C/C++ rather than relying on interactive su/password tricks. Use the POSIX time APIs from your program and handle privileges properly; that is the most robust, auditable approach.

A minimal, safe API to call is the realtime-clock setter (for example, clock_settime or settimeofday on Solaris). Convert the GPS time to a UTC seconds + nanoseconds value and call the API; check errors (EPERM/EINVAL) and log them. Example sketch:

#include <time.h>
#include <stdio.h>

int set_system_time(time_t sec, long nsec) {
    struct timespec ts = { .tv_sec = sec, .tv_nsec = nsec };
    if (clock_settime(CLOCK_REALTIME, &ts) == -1) {
        perror("clock_settime");
        return -1;
    }
    return 0;
}

These interfaces are the supported way to update the system clock from a program; they require appropriate privileges. (docs.oracle.com)

On Solaris you cannot silently elevate to root from user code; the process must either run with the kernel privilege to change time (the sys_time privilege / appropriate administrative role) or run in the global zone (non-global zones are restricted unless sys_time is granted). If you get EPERM, verify zone and privilege configuration before assuming a coding bug. (docs.oracle.com)

Operational notes and cautions: do not make broad system binaries setuid root as a shortcut — Solaris’ privilege model and “forced privileges” mean that setuid tricks are risky and often the wrong solution. Prefer a tiny, audited helper that just validates the GPS input and sets the clock, and allow only that helper to run with elevated rights (for example via RBAC or a tightly scoped sudoers entry). For long-term correctness use an NTP/chrony sync strategy or run a zone-aware NTP instance. (blogs.oracle.com)

Troubleshooting: test the helper as root first, check errno on failures, confirm you are in the global zone or have sys_time, and log every GPS sample you use to set the clock so you can audit mis-sets later.

Recommended Answers

All 5 Replies

Short answers,

1. Same as you would in a C application.
2. Ditto - you can't.

Long answers.

1. Why? Call system("ntpdate ntp_server_address") - this will set the system time to the global clock. Guaranteed accurate. If you want to set it to your own value, use system("date date+time_value").
2. The problem here is that you will need to input the password for the root account, and that cannot be done if the process is being run in the background. So, for security reasons, you really can't do that. In a shell, running the command "su -" will request the password for the root account, and if the calling process is backgrounded (no stdin), then it will fail.

Actually i brought date from GPS device now i need to set that date every time when i interact with GPS device in mu project

Well, you can enable the setuid bit on the date command (chmod +s /bin/date), then use the system("date time_val") to set the date+time in your C/C++ program.

idea what u have give worked for me, but slight different i.e at the time of installing OS we need to do chmod +s /usr/bin/date command at super user ...
after doing this, we can change from any user ....

thank you for ur suggestions

how can I change 24hr format of time to 12hr format in Solaris 9 permanently

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.